Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Anyone to explain this?

    I didn't understand how does it work.

    class Sample
    {
    public static void fun1()
    {
    Console.WriteLine("fun1");
    }
    public void fun2()
    {
    Console.WriteLine("fun2");
    }
    public void fun2(int i)
    {
    Console.WriteLine(i); fun2();
    }
    class MyProgram
    {
    static void Main(string[] args)
    {
    Sample s = new Sample();
    Sample.fun1();
    s.fun2(123);
    }
    }
    }
    }

    Output
    fun1
    123
    fun2

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,758
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Class 'Sample' is doing all the work... the class 'MyProgram' is calling the shots. let's look @ 'MyProgram'
    Code:
    Sample s = new Sample();
    this is creating a new object of the class 'Sample'. Since there is no constructor telling it to do anything, it is just going to sit there and await instruction.

    Code:
    Sample.fun1();
    try to access fun1 via 's'.... you can't (ie s.fun1() will not compile). To access the static method you use the class name.method, in this case "Sample.fun1();"
    and finally
    Code:
    s.fun2(123);
    this is saying use the method "fun2" that is part of the object "s" (remember he is sitting there waiting instruction). The method "fun2" accepts an integer as an argument (fun2( int i) ) and ypu are passing it the int '123'... then after it writes this to the console, it calls another method within the class "fun2()" (notice without any arguments) and then executes that function. IMO that is really sloppy, it shouldn't be on one line like that... again my opinion it should look more like
    Code:
        class MyProgram
        {
            static void Main(string[] args)
            {
                Sample s = new Sample();
                Sample.fun1();
                s.fun2(123);
                
                Console.ReadLine();
            }
        }
    
        class Sample
        {
    
            public static void fun1()
            {
                Console.WriteLine("fun1");
            }
    
            public void fun2()
            {
                Console.WriteLine("fun2");
            }
    
            public void fun2(int i)
            {
                Console.WriteLine(i); 
                fun2();
            }
        }
    }
    See how much easier it is to read? Also note the readline() is so you can see the output.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hai Dave Hawk..

    class Sample
    {
    public static void fun1() \\ Step 3: fun1() function definition.
    {
    Console.WriteLine("fun1"); \\ Step 4: Print the first output:"fun1". then go to main function
    }
    public void fun2() \\ Step 8: fun2() function definition
    {
    Console.WriteLine("fun2"); \\ step 9: print the Third output:"fun2"
    }
    public void fun2(int i) \\ Step 6:fun2(123) function definition.
    {
    Console.WriteLine(i); fun2(); \\ Step 7: Print the value Second output:"123" then callling the fun2() function(passing without parameter)
    }
    class MyProgram
    {
    static void Main(string[] args)
    {
    Sample s = new Sample(); \\ Step 1: create a object s.(i.e class name:Sample object name:s)
    Sample.fun1(); \\ Step 2: calling the fun1() fuction. Calling based on classname
    s.fun2(123); \\ Step 5: objectname(s) s.fun2(123) . calling based on objectname.(passing parameter)
    }
    }
    }
    }

    The following steps are how to the above program executed:

    Step 1: create a object s.(i.e class name:Sample object name:s)
    Step 2: calling the fun1() fuction. Calling based on classname
    Step 3: fun1() function definition.
    Step 4: Print the first output:"fun1". then go to main function
    Step 5: objectname(s) s.fun2(123) . calling based on objectname.(passing parameter)
    Step 6:fun2(123) function definition.
    Step 7: Print the value Second output:"123" then callling the fun2() function(passing without parameter)
    Step 8: fun2() function definition
    step 9: print the Third output:"fun2"

    Output:
    fun1
    123
    fun2


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •