473,320 Members | 1,707 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,320 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 2457
"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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.