CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2005
    Posts
    98

    Simple C#,Multiple integer

    Hello!

    Code:
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace sepehr
    {
    
        class zarb
        {
            public int i=10;
            int j=100;
            public void zarb1()
            {
                 i = i *j;
            }
    
            public int zarb2
            {
                get { return i; }
            }
    
        }
    
        
        
        class test
        {
            static void Main(string[] args)
            {
               zarb k= new zarb();
                Console.WriteLine(k.zarb2);
                Console.ReadLine();
    
            
            }
    
        }
    }
    1-I expect it shows 1000,but it shows 10,

    why?

    2-I use this :

    Code:
            public int zarb2
            {
                get { return i; }
            }
    
        }

    Is it possible I don't use get{ return i;},instead of that I use

    Code:
        class test
        {
            static void Main(string[] args)
            {
               zarb k= new zarb();
                Console.WriteLine(k.i);
                Console.ReadLine();
    
            
            }

    another question:

    Code:
    
    using System;
    
    class Unary
    {
        public static void Main()
        {
            int unary = 0;
            int preIncrement;
            int preDecrement;
            int postIncrement;
            int postDecrement;
            int positive;
            int negative;
            sbyte bitNot;
            bool logNot;
    
            preIncrement = ++unary;
            Console.WriteLine("pre-Increment: {0}", preIncrement);
    
            preDecrement = --unary;
            Console.WriteLine("pre-Decrement: {0}", preDecrement);
    
            postdecrement = unary--;
            Console.WriteLine("Post-Decrement: {0}", postDecrement);
    why does it use "{0}"?
    Last edited by Abalfazl; May 2nd, 2008 at 09:16 AM.

  2. #2
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: Simple C#,Multiple integer

    You did not call Function zarb1, then how does variable i change value?

    About the second question, you could check msdn help for Console.WriteLine.
    The difficulty is that you have no idea how difficult it is.

    .Net 3.5/VS 2008

  3. #3
    Join Date
    Aug 2007
    Posts
    50

    Re: Simple C#,Multiple integer

    1-I expect it shows 1000,but it shows 10,

    why?
    If you don't intend to call the zarb1() function explicitly, you can make it a constructor:
    Code:
        class zarb
        {
            public int i = 10;
            int j = 100;
            public zarb()
            {
                i = i * j;
            }
    
            public int zarb2
            {
                get { return i; }
            }
    
        }

  4. #4
    Join Date
    Aug 2005
    Posts
    98

    Re: Simple C#,Multiple integer

    Hi

    Did you see my other questions:

    1-Is it possible I don't use get{ return i;},instead of that I use

    Code:
    Code:
    class test { static void Main(string[] args) { zarb k= new zarb(); Console.WriteLine(k.i); Console.ReadLine(); }

    another question:

    Code:
    using System; class Unary { public static void Main() { int unary = 0; int preIncrement; int preDecrement; int postIncrement; int postDecrement; int positive; int negative; sbyte bitNot; bool logNot; preIncrement = ++unary; Console.WriteLine("pre-Increment: {0}", preIncrement); preDecrement = --unary; Console.WriteLine("pre-Decrement: {0}", preDecrement); postdecrement = unary--; Console.WriteLine("Post-Decrement: {0}", postDecrement);
    2-why does it use "{0}"?




    3-Another question :

    What is the usage of string[] args inthis statement:

    static void Main(string[] args)


    4-If I want user enter two numbers,Which command in C# console does that?

  5. #5
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Simple C#,Multiple integer

    1. since you set i to a public variable, then yes, you can just say k.i to access it.
    2. {0} refers to the first "parameter" value after the string that is going to be printed.
    3. it allows you to send parameters to the .exe file. Let's say you wanted to give the user the ability to set i. You could accept a number as a parameter:

    Code:
    sarb.exe 150
    You could then get 150 from the "args" string array, and use it to set i before doing the math.

    4. see #3

  6. #6
    Join Date
    Aug 2005
    Posts
    98

    Re: Simple C#,Multiple integer

    Quote Originally Posted by eclipsed4utoo

    3. it allows you to send parameters to the .exe file. Let's say you wanted to give the user the ability to set i. You could accept a number as a parameter:

    Code:
    sarb.exe 150
    You could then get 150 from the "args" string array, and use it to set i before doing the math.
    please explain more about that

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured