i’ve been wondering why my variable is not saving data. this is my code
class MainProg
{
public string name;
static void Main()
{
MainProg m = new MainProg();
m.Start();
}
public void Start()
{
Register rs = new Register();
Register r = (Register)rs;
r.run();
Console.WriteLine(name);
}
}
class Register : MainProg
{
public void run()
{
name = "a";
}
}
Did I forget anything?everytime I try to show the output it shows nothing.Thanks by the way for taking your time.
This will work.
The reason your code fails is that you are creating an instance of Register, which is separate from MainProg even though it inherits from it. You set the variable in the new instance, and then read it from the old.