473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

accessing nonstatic object from static and nonstatic methods

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
Nov 16 '05 #1
7 2464
"Chris Clement" <no*****@please.com> wrote in message news:ed**************@TK2MSFTNGP11.phx.gbl...
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.

--
Michael Culley
Nov 16 '05 #2
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.

Nov 16 '05 #3
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.
""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:zc*************@cpmsftngxa06.phx.gbl...
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.

Nov 16 '05 #4
"Chris Clement" <no*****@please.com> wrote in message news:#K*************@TK2MSFTNGP10.phx.gbl...
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.
--
Michael Culley
Nov 16 '05 #5
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...rfStaticPG.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.

Nov 16 '05 #6
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.

Nov 16 '05 #7
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.

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jinesh | last post by:
I illustrate the compiler error I get using the following example. --------------------------------------------------------------- Class ClassName { private: static const int constVarName = 100;...
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
2
by: Dan | last post by:
I am declaring a property in a form - simplified example below. public class frmMain : System.Windows.Forms.Form { private int x; public int X { get { return x;
7
by: Chris Clement | last post by:
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...
2
by: Beffmans | last post by:
Hi When I run this code: using System; namespace DelegateProject { public delegate void MyDelegate(string s);
7
by: The|Godfather | last post by:
Hi everybody, I read Scotte Meyer's "Effective C++" book twice and I know that he mentioned something specific about constructors and destructors that was related to the following...
10
by: Muffin | last post by:
I am a little new to C# and an have a hard time understanding why I get a nonstatic error. I create an object in my main form that has member properties by using a control. From another form/dialog...
1
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
I am getting the above error with this Class Library code: namespace ClassLibrary1 { public class Class1 { private string FoundPathKey; public string LastError { get { return FoundPathKey; }
6
by: fl | last post by:
Hi, There is a question about nonstatic member. C++ primer says: A nonstatic member is restricted to being declared as a pointer or reference to an object of its class. It only gives an example of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.