Scope in Winforms problem

G

Guest

I want to access an object that is instantiated in the frmMain method from within a static method. When I try to access this object, I get errors that state "Object reference not set to an instance of an object."

I know this is a scope problem, but I don't know how to solve it. My application is a winforms app and I'm instatiating the object from frmMain(). The object is declared:

public static DataAccess myDataAccessor;

What do I need to do? Why isn't this public object accessible throughtout my application? Is there a better place than frmMain where objects that should be accesssible throughout an application reside? I'd like to avoid passing objects that already exist as arguments if I'm creating objects that are meant to be accessible througout my app.

Thanks!
 
J

Jon Skeet [C# MVP]

Patrick said:
I want to access an object that is instantiated in the frmMain method
from within a static method. When I try to access this object, I get
errors that state "Object reference not set to an instance of an
object."

I know this is a scope problem, but I don't know how to solve it. My
application is a winforms app and I'm instatiating the object from
frmMain(). The object is declared:

public static DataAccess myDataAccessor;

What do I need to do? Why isn't this public object accessible
throughtout my application? Is there a better place than frmMain
where objects that should be accesssible throughout an application
reside? I'd like to avoid passing objects that already exist as
arguments if I'm creating objects that are meant to be accessible
througout my app.

That doesn't sound like a scope issue at all. It sounds like you've not
actually instantiated it properly. My guess is that either the static
method isn't being called, or within the static method you're declaring
a local variable with the same name instead of using the static
variable.
 
J

Jon Skeet [C# MVP]

Patrick said:
My code looks like this:

public class FrmMain : System.Windows.Forms.Form
{
.
.
public DataAccess myDataAccessor;

.
.
public FrmMain()
{
VerifyDelete = true;
DataAccess myDataAccessor = new DataAccess();
Event myEvent = new Event();
InitializeComponent();
InitializeForm();
.
.
.
}

And InitializeForm has the following signature:

public void InitializeForm()

When I'm in frmMain, after the myDataAccessor and myEvent objects are
instatiated, the objects are accessible. As soon as I enter
InitializeForm, the objects report <undefined value> in the watch
window.

Does this give further clarification? I don't see anywhere I'm
misusing the functions.

Again, it's not an accessibility problem. The problem (as I suggested
in my last post) is that you've redeclared your myDataAccessor
variable.

Instead of

DataAccess myDataAccessor = new DataAccess();

(which is declaring a new local variable and assigning it a value) you
should have

myDataAccessor = new DataAccess();

(which sets the value of the *instance* variable myDataAccessor).

Same for myEvent.
 
G

Guest

Now I understand. I didn't notice the subtle syntactial difference that was making it a local variable. Thanks Jon. I'm rocking again.
 
S

SP

My suggestion would be to implement the DataAccess as a singleton. It would
then be globally available and would instansiate itself.
SP

Patrick said:
Now I understand. I didn't notice the subtle syntactial difference that
was making it a local variable. Thanks Jon. I'm rocking again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top