public class MyClass
{
public int x;
}
public class MyClass2: MyClass
{
x=1;
}
I'm trying to access variable x from base class but I get an error saying that the name x does not exists in current context.
public class MyClass
{
public int x;
}
public class MyClass2: MyClass
{
x=1;
}
I'm trying to access variable x from base class but I get an error saying that the name x does not exists in current context.
You can access it from inside a method like this:
public class MyClass2: MyClass
{
private void MyMethod()
{
x=1;
}
}
Or from a different class:
var test = new MyClass2();
test.x = 1;
If you intend for your x
field to be public, you should consider changing it to a property and capitalizing it, like this:
public int X { get; set; }
You can read about the difference between fields and properties here. If you do make it a property, be sure to follow Microsoft design guidelines and use PascalCase. See here.
Try to make your variables public and if this doesn't work try to initialize your variable inside the MyClass and not in the MyClass2
Also happy new year
(sorry for my bad english)