CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Threaded View

  1. #1
    Join Date
    Dec 2009
    Posts
    5

    total n00b question

    I'm learning c# and having some trouble understanding implementation of classes.

    Below is a simple program i was writing to use classes. it takes your name and age, stores them in a class and displays them.

    Code:
    // Namespace Declaration
    using System;
    public class CTest1
    {
        private string m_name;
        private int m_age;
        public string Name
        {
            get { return m_name; }
            set { m_name = value; }
        }
        public int Age
        {
            get { return m_age; }
            set { m_age = value; }
        }
        
    }
    
    public class CMain
    {
        public static void Main()
        {
            
            CTest1 aTest = new CTest1();
            Console.WriteLine("Please Enter Name");
            aTest.Name = Console.ReadLine();
            Console.WriteLine("Please Enter Age");
            aTest.Age = Int32.Parse(Console.ReadLine());
            Console.Clear();
            //CTest1 aTest = new CTest1();
            Console.WriteLine("Your Name is: {0}", aTest.Name);
            Console.WriteLine("You are: {0} years old.", aTest.Age);
        }
            
    }
    that code works fine.

    so i split it up. i want individual methods to perform the input and out put so i split it up like this:

    Code:
    using System;
    public class CTest1
    {
        private string m_name;
        private int m_age;
        public string Name
        {
            get { return m_name; }
            set { m_name = value; }
        }
        public int Age
        {
            get { return m_age; }
            set { m_age = value; }
        }
        
    }
    
    public class CMain
    {
        public static void Main()
        {
            CTest1 aTest = new CTest1();
            CMain.getInput();
            CMain.showOutput();
            Console.ReadLine();
        }
            
        public static void getInput()
        {
            Console.WriteLine("Please Enter Name");
            aTest.Name = Console.ReadLine();
            Console.WriteLine("Please Enter Age");
            aTest.Age = Int32.Parse(Console.ReadLine());
         }
            
         public static void showOutput()
         {
            Console.Clear();
            Console.WriteLine("Your Name is: {0}", aTest.Name);
            Console.WriteLine("You are: {0} years old.", aTest.Age);
         }
            
    }
    now I get 4 errors, one each where i've referenced my object 'aTest', the error is: 'aTest' does not exist in current context.

    how can i access my object from multiple methods. If i have to keep coming back to Main() to call the properties from my object it seems like it defeats the purpose of OOP.

    I use VB .Net primarily, and once an object is initialized, as long as it's public, it is callable from all sub routines. Can someone help me make this connection please.
    Last edited by PyrexKidd; December 23rd, 2009 at 03:52 PM.

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