Need Help With a Simple C# Program!

TΞΞNSTAR™

Member
Mar 19, 2008
15,866
42
0
StuCk In My AnGelZ HeArT!!!
Guys ..

Im a newbie to C# and I need some guidance to rectify this confusion I'm facing.

What I wanted to create is a simple calculation and I can do it in the Main method it self. But I Created a Class named "add" so that I can call it to in the main method.

I initialized int i and j in the main program and why cant I use it in the add class?

Below is my code snippet

Code:
using System;


namespace AddMinusDivideMultiply
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.Write("Please Enter The First Number  :");
            string temp = Console.ReadLine();
            int i = Int32.Parse(temp);

            Console.Write("Please Enter The Second Number :");
            temp = Console.ReadLine();
            int j = Int32.Parse(temp);

            Addition.Add();
        }
    }

    class Addition
    {
        public static void Add()
        {
            int add;
            add = i + j;
            Console.WriteLine("The Addition Of The First and The Second Number is {0}", add);
        }
    }
}
Error which I'm getting is

Error 1 The name 'i' does not exist in the current context
Error 1 The name j' does not exist in the current context
Please explain.. Thank you in advance...
 

*danushka*

Well-known member
  • Oct 15, 2007
    13,922
    1,098
    113
    Hali-ela
    TΞΞNSTAR™;6833255 said:
    Guys ..

    Im a newbie to C# and I need some guidance to rectify this confusion I'm facing.

    What I wanted to create is a simple calculation and I can do it in the Main method it self. But I Created a Class named "add" so that I can call it to in the main method.

    I initialized int i and j in the main program and why cant I use it in the add class?

    Below is my code snippet

    Code:
    using System;
    
    
    namespace AddMinusDivideMultiply
    {
        class Program
        {
            public static void Main(string[] args)
            {
                Console.Write("Please Enter The First Number  :");
                string temp = Console.ReadLine();
                int i = Int32.Parse(temp);
    
                Console.Write("Please Enter The Second Number :");
                temp = Console.ReadLine();
                int j = Int32.Parse(temp);
    
                Addition.Add();
            }
        }
    
        class Addition
        {
            public static void Add()
            {
                int add;
                add = i + j;
                Console.WriteLine("The Addition Of The First and The Second Number is {0}", add);
            }
        }
    }
    Error which I'm getting is

    Please explain.. Thank you in advance...

    machan register this site & ask your questions,u can get lot of help here.....it's free helping form....:):):)
    http://www.vbforums.com/forumdisplay.php?f=30
     

    3.5G

    Well-known member
  • Jan 4, 2009
    1,653
    162
    63
    404 - Not Found
    I am not an expert in C# but I know JAVA

    try creating an object of Addition class and call the Add()
    and also use some parameters like Add(i,j)
     

    ¤--bACarDi--¤

    Well-known member
  • Jan 9, 2009
    12,130
    288
    83
    124.43.xxx.xxx
    Code:
    namespace AddMinusDivideMultiply
    {
        class Program
        {
            public static void Main(string[] args)
            {
                Console.Write("Please Enter The First Number  :");
                string temp = Console.ReadLine();
                int i = Int32.Parse(temp);
    
                Console.Write("Please Enter The Second Number :");
                temp = Console.ReadLine();
                int j = Int32.Parse(temp);
    
                Addition.Add(j , i);
            }
        }
    
        class Addition
        {
            public static void Add(int j , int i)
            {
                int add = i + j;
                Console.WriteLine("The Addition Of The First and The Second Number is {0}", add);
            }
        }
    }


    try this. can't compile VS isn't installed :baffled:
     

    koondeGoda

    Member
    Nov 28, 2007
    4,892
    35
    0
    ₪ Hyper_Cube ₪
    TΞΞNSTAR™;6833342 said:
    Thank you bro :)

    And Can you explain me widely with the code snippet which I should write to what you told me !

    itz simple approach ! for you hav used static methods , pass the specified values to the redfinied Add method

    Code:
    namespace ConsoleApplication1
    {
        class Program
        {
            
    
            public static void Main(string[] args)
            {
                Console.Write("Please Enter The First Number  :");
                string temp = Console.ReadLine();
                int i = Int32.Parse(temp);
    
                Console.Write("Please Enter The Second Number :");
                temp = Console.ReadLine();
                int j = Int32.Parse(temp);
    
                [COLOR="Red"]Addition.Add(i,j);[/COLOR]
            }
        }
    
        class Addition
        {
            public static void Add([COLOR="#ff0000"]int i , int j[/COLOR])
            {
                int add;
                add = i + j;
                Console.WriteLine("The Addition Of The First and The Second Number is {0}", add);
            }
        }
    }
     

    koondeGoda

    Member
    Nov 28, 2007
    4,892
    35
    0
    ₪ Hyper_Cube ₪
    Code:
    namespace AddMinusDivideMultiply
    {
        class Program
        {
            public static void Main(string[] args)
            {
                Console.Write("Please Enter The First Number  :");
                string temp = Console.ReadLine();
                int i = Int32.Parse(temp);
    
                Console.Write("Please Enter The Second Number :");
                temp = Console.ReadLine();
                int j = Int32.Parse(temp);
    
                Addition.Add(j , i);
            }
        }
    
        class Addition
        {
            public static void Add(int j , int i)
            {
                int add = i + j;
                Console.WriteLine("The Addition Of The First and The Second Number is {0}", add);
            }
        }
    }


    try this. can't compile VS isn't installed :baffled:

    same thing i said! cant you just suggest a new OO approach? :P

    my C# is not that good these days!
     

    koondeGoda

    Member
    Nov 28, 2007
    4,892
    35
    0
    ₪ Hyper_Cube ₪
    This method also works:cool: using global static variables

    Code:
    namespace ConsoleApplication1
    {
        class Program
        {
            [COLOR="Red"]public static int i, j;[/COLOR]
    
            public static void Main(string[] args)
            {
                Console.Write("Please Enter The First Number  :");
                string temp = Console.ReadLine();
                 i = Int32.Parse(temp);
    
                Console.Write("Please Enter The Second Number :");
                temp = Console.ReadLine();
                 j = Int32.Parse(temp);
    
                Addition.Add();
            }
        }
    
        class Addition
        {
            public static void Add()
            {
                int add;
                add = [COLOR="#ff0000"]Program.i + Program.j;[/COLOR]
                Console.WriteLine("The Addition Of The First and The Second Number is {0}", add);
            }
        }
    }
     

    ¤--bACarDi--¤

    Well-known member
  • Jan 9, 2009
    12,130
    288
    83
    124.43.xxx.xxx
    same thing 1/3 codes :cool: can anyone run it and tell me whether its working or not :rofl:

    Code:
    namespace ConsoleApplication1
    {
        class Program
        {
            
    
            public static void Main(string[] args)
            {
                Console.Write("Please Enter The First Number  :");
                int i = Int32.Parse(Console.ReadLine());
                Console.Write("Please Enter The Second Number :");
                int j = Int32.Parse(Console.ReadLine());
                Console.WriteLine("The Addition Of The First and The Second Number is {0}", Add(i , j));
              
            }
        }
    
        class Addition
        {
            public static int Add(int i , int j)
            {
                return (i+j);
                
            }
        }
    }
     
    Last edited:

    nadun07

    Member
    Mar 17, 2008
    10,852
    686
    0
    http://
    same thing 1/3 codes :cool: can anyone run it and tell me whether its working or not :rofl:

    Code:
    namespace ConsoleApplication1
    {
        class Program
        {
            
    
            public static void Main(string[] args)
            {
                Console.Write("Please Enter The First Number  :");
                int i = Int32.Parse(Console.ReadLine());
                Console.Write("Please Enter The Second Number :");
                int j = Int32.Parse(Console.ReadLine());
                Console.WriteLine("The Addition Of The First and The Second Number is {0}", Add(i , j));
              
            }
        
    
       [COLOR="Red"] //class Addition
        //{[/COLOR]
            public static int Add(int i , int j)
            {
                return (i+j);
                
            }
        }
    [COLOR="red"]//}[/COLOR]
    }

    this works :cool: hehe..