C# method

This is a discussion on C# method within the C# Programming forums, part of the General Programming Boards category; Hello, Been looking into learn C# programming so i can get a career into programming, my question is trying to ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    5

    Question C# method

    Hello,

    Been looking into learn C# programming so i can get a career into programming, my question is trying to wrap my brain around method, if anyone can give me an easy example of a method such as passing a int or string from a created method and moved the parameter to the main method, and explain a little (very easy terms if you can because i am still new). Thank you, my code is below but it is not finished, if you can finish it as an example and explain what i missed that would be helpful to if not its ok.

    Code:
    class Program
        {
            static void second (int number)
            {
                int one;
                string ones;
                Console.WriteLine("please enter first number");
                ones = Console.ReadLine();
                one = Int32.Parse(ones);
                
                
    
            }
            static void Main()
            {
                Console.WriteLine("printing out different method: "+ );
                       
                
                }
    }
    }

  2. #2
    Registered User
    Join Date
    Jul 2008
    Posts
    5

    Question

    anything about methods that can help me understand in your words maybe? trying to help would be appreciated

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I don't really understand the question. Are you saying you want to return a value from a method, that's quite easy using the

    Code:
    static int second(int number)
    {
        return number +14; 
    }
    In simple terms

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    5

    Question

    where did you get return number +14;? indigio?

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Siten

    In this case he is returning whatever number is (see it being passed in the methods argument?) + the number 14 back to whatever called it.

    You can think of a method like a "special" function encapsulated inside a "class". Really to simplify it just think of it as a function. You define what arguments it takes, then in that function/method you tell it what to do with those arguments, and finally you spit something back (unless the return type is void!)

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    See where the 'void' is? That's your return type. That could be an int, string, bool, whatever. Inside the standard brackets you can have whatever data you need be fed in to your method, or even nothing at all. If your method has been defined as anything other than 'void' you must make a return like 'return myResult;'

    Code:
    static void Main()
    {
        Console.WriteLine("Testing this...");
        string myText = MyOutput(4, 8);
        // the following line can also be written as Console.WriteLine(MyOutput(4, 8));
        Console.WriteLine(myText);
    }
    string MyOutput(int numberOne, int numberTwo)
    {
        return numberOne + " x " + numberTwo +" = " (numberOne * numberTwo);
    }
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  7. #7
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Though don't forget, just because you have a void return type doesn't mean you can't return. Returning from void doesn't return a value but "jumps out" of that function and returns back to the line after it was called. It differs from functions that return values by not having what you want to return after the return keyword.

    for example

    Code:
    class Program
    {
    	static void Main(string[] args)
    	{
    		Console.WriteLine("Before Method Call");
    
    		divisByTen(2);
    
    		Console.WriteLine("After Method Call");
    	}
    
    	static void divisByTen(int start)
    	{
    		while(true)
    		{
    			if((start % 10) == 0)
    			{
    				Console.Out.WriteLine(start+" is divisible by 10");
    				return; //jumps out of the method
    			}
    			else
    				start++;
    		}
    	}
    }
    Also as a side note, you can't refer to non-static methods or variables from within static methods. In DanFraser's example it would be an error noting that. Non-static functions can refer to static or non-static members fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 06:50 PM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 04:17 PM
  3. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-07-2008, 11:30 PM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM
  5. Replies: 2
    Last Post: 01-22-2008, 03:22 PM

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