I always get tied up with small things like this… I need to access an objects properties created in class ‘Login’ from my welcome page class ‘Default’. When I try to reference the class so that I may access the object, VS 2010 doesn’t list it as available like it normally would, and forcing the request just returns an error.
My Login Class is defined like so:
public abstract class Login : System.Web.UI.UserControl
{
...
private void Login_click(object sender, EventArgs e)
{
MyObject myObject = new MyObject();
myObject.property1 = "something";
}
}
And then i wish to access myObject from my default class, like this:
public class Default : System.Web.UI.Page
{
...
private void Page_load(object sender, System.EventArgs e)
{
string someLocalVar = Login.myObject.property1;
}
}
Where property1 is a property set in the Login class. This does not work, however, and VS doesn’t even recognize the Login class; instead it treats it as a reserved word of some sort. These two files are in the same project, so that shouldn’t be an issue in the using section. I’ve accessed variables in this manner between other classes before, just not this time for some reason.
Thanks!
2 things:
abstractstaticYou can’t instantiate an instance of an
abstractclass. The point of anabstractclass is to create a class with some shared code that other, similar child classes can inherit. Is there a reason your class is abstract?If your property is not
staticyou have to create an instance of your class in order to access the property. (Which, as I describe above, you can’t because it’s abstract). If you make your property static, you could then doLogin.MyObjectwithout creating an instance.In the code you supplied, your variable is local to the
Login_clickmethod, which means even if you created an instance of your class you wouldn’t be able to access it.I suggest you pick up a C# book and read up on the fundamentals.