Personal C Sharp         by    famsoft.org

Home | Demonstrative Examples | PC# Methods | PC# Reference | Links

General - I

                                                                           You are welcomed to use our site for your web search.
                                                                    See the fine products advertised at the bottom of each page too.

EXAMPLE 1: Let us see how to write the simplest program to display
the message "Hello World!" on the system screen.
=========================================================================================
class a:pcs {                  // Your class "a" extends PC# class heirarchy.
  public override void run() { // The method which runs your program.
    cm("fe");                  // Eliminate form to allow System Screen to appear
    os="Hello World!";tm("dl");// call the text method tm() with the text to be displayed
  }
}
=========================================================================================
HOW TO WRITE THE PROGRAM? Use NotePad. Since "a" has been chosen to
be the name of the class, save the program into a file named "a.cs".
 
HOW TO COMPILE IT? From Command mode type: 'pcp a' and hit [ENTER].
The program will be compiled and you will see the new executable
file "a.exe" in your directory.
 
HOW TO RUN THE PROGRAM? Just type 'a' and hit [ENTER]
 
WHAT DOES "pcp" GO FOR: "pc" is for PC# and the last "p" means that
it is a program (not a library file). If you wanted to compile it as
a library file, you should have used the command "pcl" instead which
could have generated the library file "a.dll" instead of the
executable file "a.exe".
 
You could have also combined compiling the program and running it by
using the command "pcpr".
=============================================================================================
TUTORIAL:
(1) Your class extends class "pcs". This allows you to access all PC# classes in addition to the entire ".NET" library.
 
(2) The text method tm() does many tasks one of them is displaying a line of text on the screen. Each task is identified with a string of upto 3 characters. Displaying a line of text is assigned the task code "dl".
 
(3) You may have noticed that all names in PC# include their type code. (os) is a string and tm() is a method. Here are the type identifier for the most common simple types:
 
integer: i or none      double: d       string: s
boolean: b              character: c    byte: y
object: o               method: m
 
(4) Now you may be asking "All variable names I have seen so far are in lower case. Is this a rule?
 
C# is a case sensitive language, however PC# has simplified program writing and reduced typing errors by setting rules for its public variable names (through which your program and PC# communicate with each other) The rules are:
 
  (a) All variable names are made of lower case characters only except arrays.
  (b) All array names are made of upper case characters only.
 
As an example:
     i,jb,kc,os and OS[] are valid public PC# variable names.
 
You need to know here that you do not have to apply this rule to the names which you define and use within your program.
 
(5) You may be also asking "How come we have not defined variable (os) as a string within the program and where are the "using" direcives?
 
In order to simplify programming further all "single character + type" variables have been predefined for you. 
 
The command "pcp" which you have used to compile the program has internally added the "user" directives.
 
For more details See "Personal C# reference".
=============================================================================================
Output of Example 1 is shown below:
 

demo1.jpg

                                  Example 1: How to display text at Command Line screen
You may download the executable file for Example 1 at: http://mysite.verizon.net/vze1l4h6/examples/GI1.exe

EXAMPLE 2: Let us now try two more ways to display the same message.
=========================================================================================
public class a : pcs {
  // Displaying text graphically
  public override void init() {
    base.init();     // Initialize pc# classes
  }
  public override void run() {
    os="Hello World!";gm("ctf");// Create text graphics and draw-fill
  }
}
=========================================================================================
class a : pcs {
  // Displaying text into a dialog box
  public override void init() {
    base.init();     // Initialize pc# classes
  }
  public override void run() {   
    os="Hello World!";cm("d");   // call the components handling method cm()
  }
}
=========================================================================================
HOW TO WRITE, COMPILE AND RUN THE PROGRAM? See Example 1.
=========================================================================================
TUTORIAL:
(1) This time, we have used two new methods to display the text. The "graphics" method
gm() displays the text graphically and the "controls" method cm() displays the text into
a dialog box.
 
(2) You may be wondering why we always use the variable name (os) to
assign the text to before accessing either method. All PC# methods
expect their parameters to be of two character names, the first
character can be i, j, or k and the second character denotes the
type. The "i,j and k" variables are called "the general use
variables" or GUV's.
 
Also the values all PC# methods return are assigned to variable
names with 'o' as their first character.
 
'i' is for "method's input", 'o' is for method's output. 'j,k' are
also used for input.
 
For some methods like tm("dl") or cm("d") which require only one
parameter and returns it unchanged, (os) is used for both input and
output.
 
At the end of each method all the GUV's are reset. Which means ther
are assigned zeros if numeric, assigned "" if strings and assigned
'false' if their types are boolean. They are guaranteed to stay
reset all the time except immediately before you access a method.
When you access a method, you assign them values then call the
method which resets them all again before your program execution is
resumed.
 
(3) In Example 1, we did not include the initialization method init() in order to keep
things simple. This method is necessary when we need to set default values as we'll see
in the next example and it is always a good practice to include it.
 
When you include this method, you must initialize PC# classes with the statement
"base.init();"
=========================================================================================
Output of Example 2 is shown below:

demo3b.jpg

Example 2: Displaying the same message graphically.
You may download the executable file for Example 2 at: http://mysite.verizon.net/vze1l4h6/examples/GI2.exe

demo3.jpg

 
Example 2a: Displaying the same message into a Dialog Box.
You may download the executable file for Example 2a at: http://mysite.verizon.net/vze1l4h6/examples/GI2a.exe

EXAMPLE 3: Let us now use a much better display.
=========================================================================================
class a:pcs {
  public override void init() {
    toa="t";                    // Select "text screen" for txt output
    base.init();                // Initialize pc# classes. Must be the last statement
  }
  public override void run() {   
    os="Hello World!";tm();     // call the text method tm() with the txt to be displayed
  }
}
=========================================================================================
HOW TO WRITE, COMPILE AND RUN THE PROGRAM? See Example 1.
=========================================================================================
TUTORIAL:
(1) In Example 1, the text was displayed on the Console. That was
because we did not tell PC# which text output device to use for
displaying text and the Console is the default. In this example we
have requested making the "text screen" the device to be used.
 
The text screen has been developed by PC#. It displays a scrollable
text to the user and allows the user to enter a text line which it
sends back to your program.
 
The text screen also allows the user several options like searching,
printing and filing of the displayed text.
 
(2) You may be wondering why we accessed method tm() this time
without specifying the mode. The reason is that mode "dl" is the
default mode of this method. So tm("dl") and tm() mean the same
thing to the method.

(3) The symbol for the text output device is (toa) is this another naming method for PC#?
Yes. There are two naming systems:

  (a) "One char + type code" which is used for simple objects like numbers and strings.
  They are predefined for you; so for example you can use the int (n), the double (nd)
  and the string (ns) without type declaration.

  (b) "Two char's + type code" Which is used for general objects. The two char's are
  normally the first char in the object's name + the next non-vowel character. If
  the object name is made of more than one word, the two char's are selected to be the
  first char of the first word and the first char of the second word.
  The type codes are the same as the ones used with the "single char + type" var's except
  that there are some additional ones.

  As an example, the 2-char code for the object (color) is "cl", so we can expect that:
  (clo) is a color object.     (cls) is a string which is related to the color object.
  (cli) represents an int which is related to the color object,...

  Additionally, (clp) means present color object and (cla) means "Applet's color".
  Applet is a name which was borrowd from Java. We mean by this type "a default value for
  the entire program. So (cla) is the default color for your program. similarly,
  (toa) is the default text output device for your program.
=========================================================================================
Output of Example 3 is shown below:

demo2.jpg

                                                    Example 3: Using the "Text Screen"
 You may download the executable file for Example 3 at: http://mysite.verizon.net/vze1l4h6/examples/GI3.exe

EXAMPLE 4: Let us now get into a more serious program. We'll create
a menu which gives the user the choice of displaying the "Hello
World!" message in either red, blue or green colors. Each time the
user selects one color, the message will be displayed in that color
then the menu will appear again to allow him another choice.
=========================================================================================
 
public class a : pcs {
  public override void init() {
    tia=toa="t";         // Select "text screen for both text input & output
    bli=1;               // Requesting to start execution at block 1.
    base.init();         // Must be the last in init()
  }
  public override void run() {
    if (blp==1) {                    // Start of code block 1.
      cls="r0";                      // Set color to "Pure red".
      fns="trb14";                   // Set font to "times roman",bold,size 14.
      os="                                         MENU";
                                     // and display this text.
      tm();                          // Calling method tm() to execute.
      os="";tm();                    // Skip one line
      cls="S9";fns="trb10";          // Change color, font
      os="  (R) Red.";tm();          // Display this line.
      os="  (B) Blue.";tm();         // and this line.
      os="  (G) Green.";tm();        // and this line
      os="  (E) Exit.";tm();         // and this line
      os="";tm();                    // skip one line
      cls="b0";                      // Change Color.
      os="Type your selection into the text field at bottom then hit the [ENTER] key.";tm();
      os="Selection :";bli=2;tm("i");// Get user selection and jump to block 2.
    }
    if (blp==2) {                    // Start of code block 2.
      String message="Hello World."; // Message to be displayed.
      int index="EeRrBbGg".IndexOf(os);// Check selection
      if (index<0){                  // If unexpected char entered
        bli=1;um("b");               // return to block 1.
        return;                     
      }
      else if (index<2) sm("e");// If selection was "E or e" exit program
      else if (index<4) {os=message;cls="r0";tm();}
                                     // If selection was "R or r" display msg in red.
      else if (index<6) {os=message;cls="b0";tm();}
                                     // If selection was "B or b" display msg in blue.
      else if (index<8) {os=message;cls="G2";tm();}
                                     // If selection was "G or g" display msg in green.
      bli=1;um("b");                 // Then return back to block 1 to repeat the menu.
    }
  }
}
=========================================================================================
HOW TO WRITE, COMPILE AND RUN THE PROGRAM? See Example 1.
=========================================================================================
TUTORIAL:
(1) The first thing to notice is that the program is divided into seperate blocks. Each
block is asigned a unique number and in method init(), we have made a request to start
execution at block 1. Now why do we need blocks:

  (a) The utilities method um() allows us to jump from any block to another which greatly
  simplifies programing tasks as you'll see in this program.

  (b) When we come to examples on components, you'll see an additional programming
  simplification.  You do not have to worry about delegates any more. Handling events is
  very easy.

(2) JUMPING BETWEEN BLOCKS: You can do any jump (forward or backward) by assigning the
destination block to (bli) and calling the utilities method at mode "b". There is one
important rule. The jump statement should be the last one to be executed in the original
block. Since this rule is very important, always follow each jump statement with a
(return;) even if it was naturally the last in the block.

(3) GETING DATA BACK FROM THE USER: Method tm() at mode "i" allows the user to "input"
data into your program. In the example, after we have displayed the menu to the user, we
wanted to display the phrase "Selection?" then read back user's response. Method tm() does
the two jobs. It uses (os) for both jobs, which means that it expects our phrase to be
asigned to (os) and it sends us the user respose back asigned also to (os)

There is only one simple problem, your program cannot continue on same block. Method tm()
must send the user response to a different block. So, we supply the method with the number
of the block which we like to continue at, terminate our current block and resume the
program at the new block. Since method tm("i") executes a jump, it must also be followed
with (return;)

(4) Whenever method tm() displays text on the "text screen", it uses applet's color and
font for the display. PC# initializes their values. Whenever you like to change their
values, asign your wanted color and font codes to (cls) and (fns) respectively before
calling the method.

(5) FORMING THE COLOR CODE: Your program can state the color it wants either accurately by
supplying the red, green, blue and opacity components or by using the PC# simplified
method. For text purposes, we use the simplified method only.

In the simplified method, you supply a three character code like "r25" to (cls).

The first char tells the color wanted which is "red" in this case.

The second char is a digit which  tells the shade. It means how much white color or black
color you want to mix your color with. If you want to mix it with white color you make the
first char a lower case one and if you want to mix it with black color you make it an
upper case one. The second character can be in the range 0:9 in either case.

"r0" means pure red and "r5" means half way between red and white. "R0" also means pure
red and "R5" means half the way between red and black.

The third char is for the opacity and it is optional. "0" means fully transparent, "9"
means fully opaque and if eliminated it will be considered to be "9".

(6) FORMING THE FONT CODE: Let us analyze the code "trbiu12" .The first two chars
represent the font name.    tr=Times Roman  cr=Courier  hl=Helvetica.

The number at the end represents the font size. The chars in between the two are optional.
You may add any or all of them. They are: b=bold  i=Italic  u=Underlined  s=Strikeout.

For more information on color and font, see PC# Reference.
=========================================================================================
Output of Example 4 is shown below:

demo4.jpg

                                                       Example 4: Creating a simple menu
You may download the executable file for Example 4 at: http://mysite.verizon.net/vze1l4h6/examples/GI4.exe