473,793 Members | 2,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.che ckedListBox1' 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 2523
"Chris Clement" <no*****@please .com> wrote in message news:ed******** ******@TK2MSFTN GP11.phx.gbl...
cs(1189): 'class.form.che ckedListBox1' 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.chec kedListBox1.Ite ms.Count;i++)
{
checkedListBox1 .SetItemChecked (i,false);
}

mainForm.checke dListBox1.Items .Count gets this error:

C:\Documents and Settings\ccleme nt\My Documents\Visua l Studio
Projects\CBERBU \Form1.cs(1175) : An object reference is required for the
nonstatic field, method, or property 'CBERBU.mainFor m.checkedListBo x1'
checkedListBox1 .SetItemChecked (i,false); gets this error:

C:\Documents and Settings\ccleme nt\My Documents\Visua l Studio
Projects\CBERBU \Form1.cs(1185) : 'CBERBU.mainFor m.checkedListBo x1' denotes a
'field' where a 'class' was expected
Thanks for the help.
""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message
news:zc******** *****@cpmsftngx a06.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******** *****@TK2MSFTNG P10.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.DoSom ething it has no idea which
form to use. You need to tell it which form by doing MyForm.MyContro l.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 "checkedListBox 1" 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.mainFor m.checkedListBo x1' denotes a 'field'
where a 'class' was expected" compile-time error message will generate.

I suggest you access "checkedListBox 1" in an instance method.

Why you MUST access "checkedListBox 1" 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
2971
by: Jinesh | last post by:
I illustrate the compiler error I get using the following example. --------------------------------------------------------------- Class ClassName { private: static const int constVarName = 100; void functionName(int parameterName) }; void ClassName::functionName(int parameterName=constVarName)
2
2315
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 in Rectangle, the compiler tells me Point::x is protected. I would have expected Rectangle to see the protected members of any Point. Compiling the following code give me this error: g++ -o rectangle main.cc main.cc: In member function `size_t...
2
1297
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
395
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 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...
2
11246
by: Beffmans | last post by:
Hi When I run this code: using System; namespace DelegateProject { public delegate void MyDelegate(string s);
7
15015
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 error/warning: "error: invalid use of nonstatic data member " However, he did NOT mention this error in the book explicitly.It happens always in the constructor when you try to initialize some data members in the constructor and try to accsess other data...
10
1315
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 I try to access that object to set a property. I get a non static error. I can change my object to static, which works fine. Static objects are acting as I expect. Non static objects are not. I do not understand why this is happening. I have...
1
1992
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
2215
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 pointer *b. class Bar { public: private:
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10212
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.