Personal C Sharp                                                         By  famsoft.org
HomeHow To StartExamples-DesktopExamples-WebPC# MethodsReference-DesktopReference-Web


HOW CAN OBJECT ORIENTED PROGRAMMING AND PC# WORK TOGETHER ========================================================= Object Oriented Programming is considered to be essential for business. Some believe that using Personal C Sharp and object oriented programming cannot go together. This is incorrect. The fact that the classes which we have used for all of the illustrative examples extend class (pcs) does not mean that you need to extend (pcs) by all your classes. You like to create hundreds of classes which extend some ".NET" classes, extend one another, implement some interfaces and instantiate one another. How could that be done while they are able to access Personal C Sharp class methods? To use an instance of class (pcs) into every class you have, will probably be a waste of resources. One answer is internal classes. Divide your classes into groups. Create a container class for each group which extends class (pcs), then create a static object of the container class and use it to access PC# methods by all the classes inside the container class. Let us see an example. In the first example of "Networking" we have showed how to obtain the stock price of one stock using CNN's financial website. Let us make it general. We need to write a group of classes which work to gether to allow the user to get the stock price for any stock symbol which he provides. The classes will be accessing PC# methods to do their job without extending (pcs) This little project requires two types of classes: (1) Library classes to do all the important operations. (2) One executable class which will be very simple. The executable class will use the other classes to do most of the job then will display the results to the user. This class will be good to extend PC# if you like to have a nice looking display without much of work. However, you can also do it all by yourself. The job will be shared by 3 library classes which will be contained into a class named "Library" which extends (pcs) Class "Library" will be compiled to a "dll" file with the tool "pcl". =============================================================================================== Example 1: Write a library of classes to get a stock symbol from the user then obtain the latest stock price for the symbol. Write also an executable class to display the results to the user. =============================================================================================== The Library Class: ------------------ public class Library:pcs { // The class extends (pcs) static new Library l=new Library(); // (l) is a static object of the Library // class. The "new" word is to prevent // conflict with the int (l) public override void init() { l=this; // This makes (l) = the same instance which base.init(); // the container class is running } public class GetSymbol { // First class public string getSymbol () { // Method of first class l.os="Enter Stock Symbol ";l.tm("i"); // Get symbol using PC# method tm() return l.os; // and return it to caller. } } public class GetPage { // This class is used internally by GetPrice public string getPage(string sy) { // Method receives the symbol l.urs="http://money.cnn.com/quote/quote.html?symb="+sy; l.jb=true;l.kb=true;l.nm("hg"); // and retrieves the web page using PC# return l.os; // method nm() then returns the page body } } public class GetPrice { // Third class public string getPrice(string pg) { // Method of 3rd class, receives the page l.txs=pg; // Assigns it to searchable string (txs) l.js=":MSFT)";l.ks="As of";l.tm("s"); // Get the string bet. js & ks. Assign it to (os) int c; for(c=l.os.Length-1;c>-1;c--) { // Scan (os) backward. Stop at first numeric char if("0123456789.,".IndexOf(l.os.Substring(c,1))>-1) break; } l.os=l.os.Substring(0,c+1); // Remove all chars after that one from (os) //------ Remove chars before the price ------ for(c=l.os.Length-1;c>-1;c--) { // Scan (os) char's again. Stop at first char which if("0123456789.,".IndexOf(l.os.Substring(c,1))<0) break; } // is not a digit, decimal point or comma. l.os=l.os.Substring(c+1); // Remove all char's upto that one from (os) return l.os; // and returning the price. } } } The Executable Class: --------------------- //assembly Library.dll; // Where to find refrenced objects of the // class. (See PC# Reference) Notice that // "Library.dll" is all we need to list here public class a:pcs { // Class a extends (pcs) public override void run() { cm("fe"); // Eliminate form Library.GetSymbol gs=new Library.GetSymbol(); // Creating object of GetSymbol class Library.GetPage gp=new Library.GetPage(); // Creating object of GetPage class Library.GetPrice gpr=new Library.GetPrice(); // Creating object of GetPrice class os="When finished Push [ENTER]";tm();os="";tm();// Display instruction while (true) { // Start of endless loop: ss=gs.getSymbol(); // Call method of 1st class to get symbol if (ss.Length<1) break; // Exit if [ENTER] pushed without entry ps=gp.getPage(ss); // Call method of 2nd class to get page os=gpr.getPrice(ps); // Call method of 3rd class to get price os="Latest stock price: $"+os;tm(); // Display price. } } }


=============================================================================================== Can we make use of the Container Class: (This requires version 1.6 or higher) ======================================= Since the container class running instance is the same instance which all inner classes use to access PC# methods, the inner classes can for example, draw any object on the container's Graphical Output device or display any text on the container's Text Screen. This is a great advantage which makes it more convenient to eliminate class (a) and use the library class to interface with the user. In the next example, we are going to see how to do this. Additionally class "GetSymbol" will be eliminated and class "GetPrice" will extend class "GetPage" and access its method by itself to get the page before it searches it to obtain the price. Class "GetPrice" will also display the stock price directly on it's container's Text Screen which will make the container's class simpler. For this reason method getPrice() will be of "void" type since no return value will be necessary. The container's class's name is going to be changed from "Library" to "Quotes" and will be an executable class compiled with the tool "pcp". =============================================================================================== Example 2: Modify example 1 in order to make the container class replace class (a) Also make class "GetPrice" extends class "GetPage". Use Text Screen for display and show how it could be accessed from the inner classes. =============================================================================================== public class Quotes:pcs { // Class Quotes extends (pcs) static new Quotes l; // (l) is a static object of the Quotes // class. The "new" keyword is to prevent // conflict with the int (l) of class (pcs) GetPrice gpr=new GetPrice(); // Creating object of GetPrice class public override void init() { l=this; // This makes (l) = Running Quotes instance tia=toa="t"; // Text output device=TextScreen bli=0; // Start at block number 0 base.init(); // Initialize parent class. } public override void run() { if (bli==0) { // Starting block cls="r0"; // Set color to red os=" STOCK QUOTES"; tm();os="";tm(); // Display title then skip one line cls="G4";os="When finished, push the [ENTER] key."; tm(); // Display exit instructions in green bli=1;um("b");return; // Jump to block 1. } else if (bli==1) { // Start of the continuous operation cls="S9"; // Change to black color. Get stock symbol os="Enter Stock Symbol: ";bli=2;tm("i");return; } // from user then move to block 2. else if (bli==2) { if (os.Length<1) sm("e"); // Exit if [ENTER] pushed without data gpr.getPrice(os); // Call method getPrice() with the symbol bli=1;um("b"); // then jump to block 1. } } public class GetPage { // This class is used internally by GetPrice public string getPage(string sy) { // Method receives the symbol l.urs="http://money.cnn.com/quote/quote.html?symb="+sy; l.jb=true;l.kb=true;l.nm("hg"); // and retrieves the web page using PC# return l.os; // method nm() then returns the page body } } public class GetPrice:Quotes.GetPage { // This class extends class "GetPage" public void getPrice(string sy) { // Method is now "void". It receives symbol l.txs=getPage(sy); // sends it to getPage() and assigned the // page received to searchable string (l.txs) l.js=":MSFT)";l.ks="As of";l.tm("s"); // Get the string bet. js & ks. Assign it to (os) int c; for(c=l.os.Length-1;c>-1;c--) { // Scan (os) backward. Stop at first numeric char if("0123456789.,".IndexOf(l.os.Substring(c,1))>-1) break; } l.os=l.os.Substring(0,c+1); // Remove all chars after that one from (os) //------ Remove chars before the price ------ for(c=l.os.Length-1;c>-1;c--) { // Scan (os) char's again. Stop at first char which if("0123456789.,".IndexOf(l.os.Substring(c,1))<0) break; } // is not a digit, decimal point or comma. l.os=l.os.Substring(c+1); // Remove all char's upto that one from (os) l.cls="b0"; // Change color to blue l.os="Latest stock price: $"+l.os;l.tm(); // Display price on container's TextScreen } } } ==============================================================================================