scope and access

T

tshad

I have a class trying to access some public static variables in another
class and one I can't seem to see.

In the class where they are defined, the look like:

public static object statusOutput = null;
public static string fileInProcess = null;
public static bool serviceRunning = false;

The only difference between them is the type.

temp = FieldNameMapSetup.FieldNameMapSetup.serviceRunning;
stemp = FieldNameMapSetup.FieldNameMapSetup.fileInProcess;
Log.Write(FieldNameMapSetup.FieldNameMapSetup.statusOutput);


But for some reason I can't seem to see the statusOutput variable from the
other class. I can see fileInProcess and serviceRunning.

But statusOutput gives me the error:

'FieldNameMapSetup.FieldNameMapSetup' does not contain a definition for
'statusOutput'

But as you can see from above, it does???? It doesn't show in the
intellisense either.

Also, I have a Textbox in that same class that gives me the same error:

FieldNameMapSetup.FieldNameMapSetup.Status.Text

It says that Status does not exists, but it does as a TextBox.

What do I have to do to access this Textbox and statusOutput?

Thanks,

Tom
 
A

Arne Vajhøj

tshad said:
I have a class trying to access some public static variables in another
class and one I can't seem to see.

In the class where they are defined, the look like:

public static object statusOutput = null;
public static string fileInProcess = null;
public static bool serviceRunning = false;

The only difference between them is the type.

temp = FieldNameMapSetup.FieldNameMapSetup.serviceRunning;
stemp = FieldNameMapSetup.FieldNameMapSetup.fileInProcess;
Log.Write(FieldNameMapSetup.FieldNameMapSetup.statusOutput);


But for some reason I can't seem to see the statusOutput variable from the
other class. I can see fileInProcess and serviceRunning.

But statusOutput gives me the error:

'FieldNameMapSetup.FieldNameMapSetup' does not contain a definition for
'statusOutput'

But as you can see from above, it does???? It doesn't show in the
intellisense either.

Also, I have a Textbox in that same class that gives me the same error:

FieldNameMapSetup.FieldNameMapSetup.Status.Text

It says that Status does not exists, but it does as a TextBox.

What do I have to do to access this Textbox and statusOutput?

Can you post a complete code sample that shows the problem ?

It would make troubleshooting a lot easier !

Arne
 
J

Jeff Winn

Try cleaning the solution and rebuilding, sounds like a problem with the
vshost process screwing things up again.
 
J

Jon Skeet [C# MVP]

What do I have to do to access this Textbox and statusOutput?

As Arne says, a short but complete program would make life a lot
easier. However, a few things:

1) Having a namespace and a class of the same name can be problematic.
Avoid it where possible.

2) Public variables (other than readonly immutable ones) are generally
a very bad idea.

3) Static variables are often a bad idea. Dependency injection is one
technique which avoids it in many situations. Statics may be the right
solution in this particular case, but it's worth thinking about
whether there's a useful encapsulation you could introduce, along the
lines of a StatusMonitor or something similar.

Jon
 
T

tshad

Arne Vajhøj said:
Can you post a complete code sample that shows the problem ?

It would make troubleshooting a lot easier !

I agree, but the 2 files are both really large. I have them together
exactly like the I have shown, however. The question would be why would 2
of them work fine and the 3rd one not work. The only difference appears to
be the fact that one of them is an object - also the TextBox doesn't work,
as I mentioned. The only thing there is that VS sets it up as private and
I changed it to public - so I would have thought I would be able to access
it from my other file.

Thanks,

Tom
 
J

Jon Skeet [C# MVP]

I agree, but the 2 files are both really large.

Arne didn't ask you to post your original code. He asked you to post
complete code which shows the problem. The best way to proceed is to
take a copy of the original code, and start pruning it. Get rid of
everything which isn't strictly necessary just to show the problem. If
possible, get rid of all GUI stuff as that isn't usually necessary but
*is* usually bulky.

In the course of pruning, you may well find the problem. If so, great.
If not, you've then got a situation which we can all reproduce so
we'll be able to help you *much* more easily.

Jon
 
A

Arne Vajhøj

tshad said:
I agree, but the 2 files are both really large. I have them together
exactly like the I have shown, however. The question would be why would 2
of them work fine and the 3rd one not work. The only difference appears to
be the fact that one of them is an object - also the TextBox doesn't work,
as I mentioned. The only thing there is that VS sets it up as private and
I changed it to public - so I would have thought I would be able to access
it from my other file.

Make a copy of the files and cut them down to the minimum needed to
illustrate the problem.

It will make it possible for us to help.

And BTW the process of cutting down the code may very well enable
you to find the problem yourself before even posting the code.

Arne
 

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