Lesson #5 - Methods

This is a discussion on Lesson #5 - Methods within the C# Programming forums, part of the General Programming Boards category; Before looking at the code, since I did not make too many comments on it, I want to cover a ...

  1. #1
    Registered User oval's Avatar
    Join Date
    Jan 2006
    Location
    Texas
    Posts
    10

    Lesson #5 - Methods

    Before looking at the code, since I did not make too many comments on it, I want to cover a few concepts and add a quick note.



    Note * - for some programmers this program may be pointless because you can easily achieve this programs output under the Main(). But not to worry as we will build on this in the future.



    \n,

    This written inside a Console.WriteLine() or Console.Write() will skip a line, so basically you can Console.Write(“Hello \n”) will give you the same output as Console.WriteLine(“Hello”); This is great because it will minimize your typing.



    \t

    This can also be written in Console.WriteLine() or Write and it will have the same effect as hitting the [TAB] key on Notepad or any Word Editor.



    for (i = n; i < x; i++)

    {

    }



    The for loop: The first section of it says “i = n;” where n is any number you wish it to be. You can either declare n as a variable and assign a value up at top or you can just substitute a number such as 2 in the place of n.



    Next you will see “i < x;” which basically says, once this condition is met, stop. Again x acts like n in the first section of the for loop.



    The last section of the for loop we have “i++”. This will continue to occur until the condition is met. That is to say i will continue to increment by one until the condition is met. You can also increment by 5 or 10 or any number you wish or even a variable. Simply type i+=5 or i+=variableName



    Also, in this program you will see that we have two more methods below the Main() method. Which is cool because these are methods that we are going to create. ourselves. These two methods will be executed within the Main() method. Later we will see the advantages to this. However, right off the top, if you declare a variable named varOne in one method, you can use another variable named varOne in another because varOne closes as soon as the method that is using it closes. Pretty neat huh? If you don’t understand this right now, don’t worry. Just practice by making your own methods and running your own programs and become comfortable with it. Also, play with the for loop a little more. If you are a beginner and want to test out methods, you can stick to coding Console.WriteLines()s instead of declaring variables and coding statements.



    Also before we look at the code, the Main() method will first call methodOne which will just give out some output. The second method you will be asked to enter a number that you wish to count to. Then to enter an number for which you would like to count by in increments. So if you enter 100 and then enter 3, you will count to 100 by 3’s.

    Code:
     
     
    using System;
     
     
     
    namespace manyMethods
     
    {
     
     ///<summary>
     
     /// Creating Methods
     
     ///</summary>
     
     class Class1
     
    	 {
     
     ///<summary>
     
     /// This program will create methods and
     
     ///</summary>
     
    		 [STAThread]
     
     static void Main(string[] args)
     
    		 {
     
     //we execute our first custom method
     
    				 methodOne();
     
     //after that one, we do our next one
     
    				 methodTwo();
     
     //then we exit the program
     
     //all we have to do is press enter
     
     //The Console.ReadLine serves as a pause
     
    				 Console.ReadLine();
     
     
     
    		 }//end Main()
     
     
     
     static void methodOne()
     
    		 {
     
     string greeting = "";
     
    				 greeting += "\nThis is an introduction to programming\n";
     
    				 greeting += "more than one method.\n";
     
    				 greeting += "Have fun using your own methods!!";
     
    				 greeting += "\n\n";
     
    				 Console.WriteLine(greeting);
     
     
     
    		 }//end methodOne
     
     
     
     static void methodTwo()
     
    		 {
     
     //Declare our variables
     
     int loopy, skip, i;
     
    
     
    
     
    				 Console.WriteLine("Let's use a for loop.");
     
    				 Console.Write("Enter the number you want to count to: ");
     
     //take in the variable "loopy"
     
    				 loopy = Convert.ToInt32(Console.ReadLine());
     
    				 Console.Write("\nEnter the number you wish to count by (i.e. 2s, 3s, 5s, etc..) ");
     
     //take in the variable "skip"
     
    				 skip = Convert.ToInt32(Console.ReadLine());
     
     
     
     for(i = 0; i <= loopy; i += skip)
     
    				 {
     
    					 Console.Write(i + "\t");
     
     
     
    				 }//end for loop
     
     
     
    		 }//end methodTwo
     
    	 }//end Class1
     
    }//end namespace manyMethods
    Just as a reminder, Visual Studio was used in developing this Console Application.


    This was made by request of microscopic^earthling for verification purposes at Astahost".
    Last edited by oval; 05-04-2006 at 12:54 PM.

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Too bad you didn't include too much info on methods ....

    like why are we writing static void or private void or private string etc ( although i hope you will get into the differences between static , public , private etc later on when you start to work with object etc.. )....

    Therefore:

    a method always has the following

    an "access-identifier" ( don't know the correct word ) - a return type ( can be anything ranging from primitive types to objects to nothing -> void ) - name of the method - ( ) which can have from 0 upto as many paramaters as you like again the type being either primitive types or objects

    A little bit more info on the parameters , they are being passed from where you call the method ( then these parameters are actually copied so you can use them in the scope of the method - meaning a parameter that is being changed inside that method will not effect the value of the argument that was originally passed )....

    for instance

    Code:
    int i = 0;
    this.Increment(i);
    //i will not be incremented by one , the variable that is incremented is x , since i is copied into x when calling the method
    ....
    
    public void Increment(int x) {
      x++;
    }
    The only way to change a variable inside a function and still see the changes applied to the variable that was passed is if you use ref or out ( keywords ).
    Use ref only when the passed variable is already initialized, out can be used if the variable is not yet initialized, for the rest they both do the same.

    Usage of out and ref is being done like this
    Code:
    int i,x;
    this.Increment(out i,outx);
    //i and x will both be  set to  1 
    ....
    public void Increment(out a, out b) {
      a = 1;
      b = 1;
    }
    
    ....
    int y = 1;
    int z = 1;
    this.Decrement(ref y,ref z);
    // y and z will both be set to 0
    public void Decrement(ref a,ref b) {
      a--;
      b--;
    }
    Of course there's alot more that can be said about methods. But I just wanted to add this since it sometimes can be usefull to pass multiple variables and make sure that the changes from inside a methode are being reflected to the passed arguments ( since you cannot return more then one variable in a method ) ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generalising methods to handle multiple weapon types
    By Swarvy in forum Game Programming
    Replies: 2
    Last Post: 05-22-2009, 02:52 AM
  2. Class methods in header or source file?
    By TriKri in forum C++ Programming
    Replies: 13
    Last Post: 09-17-2007, 05:23 AM
  3. Turn Based Stradegy System Methods
    By TylerMoyer in forum Game Programming
    Replies: 2
    Last Post: 07-30-2007, 10:45 PM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  5. Replies: 8
    Last Post: 07-27-2003, 01:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21