blah blah blah is here! blah blah » Close

up1down
link

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.

// 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:

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 answered 8 months ago

1 answers

link

All you need to do is to make 'aTest' a field (i.e. a class level variable) of the CMain class rather than a local variable of the Main() method. It will then be available to all methods of CMain:

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
{
private static CTest1 aTest; // added this private field

public static void Main()
{
aTest = new CTest1(); // now assigning to the field. not a local variable
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);
}

}

Cool Thanks. I knew it was staring me in the face. why is setting aTest to static necessary?

vulpes
12813

It's necessary because all the methods in CMain are themselves static and static methods can't access instance fields of the same class. This is because static members don't know anything about particular instances - they are 'shared' between all instances of the class which is why VB.Net uses the 'Shared' keyword to describe them.

Feedback