accessing nonstatic object from static and nonstatic methods

  • Thread starter Thread starter Chris Clement
  • Start date Start date
C

Chris Clement

I have been handed a project that someone else started and most of it was
developed in the VS.NET design mode. For whatever reasons, when I try to
make changes to the controls in VS.NET design mode, I suddenly get a ton of
these errors:

cs(1189): 'class.form.checkedListBox1' denotes a 'field' where a 'class' was
expected

I was not getting any errors until I made a couple of changes within VS.NET.
So I'm trying to understand why these errors are occurring. I need
checkedListBox1 to be nonstatic, but I also need to be able to access it
from a static method. Any clues to this or why the compiler bugs out after
making changes in design mode would be greatly appreciated.

Chris
 
Chris Clement said:
cs(1189): 'class.form.checkedListBox1' denotes a 'field' where a 'class' was
expected

What is the line of code that is causing this error.
I was not getting any errors until I made a couple of changes within VS.NET.
So I'm trying to understand why these errors are occurring. I need
checkedListBox1 to be nonstatic, but I also need to be able to access it
from a static method. Any clues to this or why the compiler bugs out after
making changes in design mode would be greatly appreciated.

You can't without an object reference. A static method needs to know what instance of the class to act apon.
 
Hi Chris,

Based on my understanding, when you use checkedListBox in VS.net design
mode, you get some error.

I think the error you get is a compile time error, which is a general
error. Can you paste some code snippet to reproduce your problem? Without
this we can not find out where the problem is.

Also, I want to inform you that, in a static method, you can see the class
level element of that class, it is not in the level with any instance of
that class. If you really want to an instance member of that class, you
should "new" an instance of that class, then you can manipulate that
instance member.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Here some of the code where the errors are coming from:

for(int i = 0; i<mainForm.checkedListBox1.Items.Count;i++)
{
checkedListBox1.SetItemChecked(i,false);
}

mainForm.checkedListBox1.Items.Count gets this error:

C:\Documents and Settings\cclement\My Documents\Visual Studio
Projects\CBERBU\Form1.cs(1175): An object reference is required for the
nonstatic field, method, or property 'CBERBU.mainForm.checkedListBox1'


checkedListBox1.SetItemChecked(i,false); gets this error:

C:\Documents and Settings\cclement\My Documents\Visual Studio
Projects\CBERBU\Form1.cs(1185): 'CBERBU.mainForm.checkedListBox1' denotes a
'field' where a 'class' was expected


Thanks for the help.
 
Chris Clement said:
Here some of the code where the errors are coming from:

A static method is not associated with the form/class it is contained in in any way. It is just like a global function that has been
put in a particular location for convenience. In a static function if you use the code MyControl.DoSomething it has no idea which
form to use. You need to tell it which form by doing MyForm.MyControl.DoSomething.
 
Hi Chris,

Thanks very much for your feedback.

Oh, I see your concern, and have reproduced out your problem. I think you
may be confused on the static and instance field in a class of C# language.

I think you must have call this code snippet in a static method of mainForm
class. Yes?

For normal member(instance member), each instance of certain type will have
a unique copy of their own.
While for a class member(both field and method), if it is marked with
"static", it means that it belongs to type itself rather than to a specific
object. There is only one copy of static member of all the instances of
certain type. All the instances share this static member. The static member
has no visibility of instance field(Because instance field is instance
level, and belongs to a certain type instance)
To access the instance field(or method) of a type in static method, you
should create an instance of that type, then you can visit the instance
field through the new created instance.

For more information, please refer to:
http://msdn.microsoft.com/library/en-us/csref/html/vclrfStaticPG.asp

For your issue, the "checkedListBox1" field is an instance field, it
belongs to a certain instance of mainForm class. So your static method can
not visit it, and a " 'CBERBU.mainForm.checkedListBox1' denotes a 'field'
where a 'class' was expected" compile-time error message will generate.

I suggest you access "checkedListBox1" in an instance method.

Why you MUST access "checkedListBox1" in a static method? Can you explain
the whole scenario to me? I think I may find a suitable design-time for you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Chris,

Does our replies make sense to you? Do you still have any concern?

Please feel free to feedback, I will help you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top