473,794 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NonStatic erorr

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
made it public and am in the same name space for my project. It seem that I
can only use an object in the same class that I created it in. In this case
I created my object in my main form class and can access easly from any
control in that class and set any property, however if I try to access this
object from another form/dialog class I recieve a non static error.

Thx,
Jan 6 '07 #1
10 1315
Hi Muffin,
do you have a simple code example of what you are trying to do?

Mark.
--
http://www.markdawson.org
"Muffin" wrote:
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
made it public and am in the same name space for my project. It seem that I
can only use an object in the same class that I created it in. In this case
I created my object in my main form class and can access easly from any
control in that class and set any property, however if I try to access this
object from another form/dialog class I recieve a non static error.

Thx,
Jan 6 '07 #2
Muffin <mu****@NoEmail .localwrote:
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
made it public and am in the same name space for my project. It seem that I
can only use an object in the same class that I created it in. In this case
I created my object in my main form class and can access easly from any
control in that class and set any property, however if I try to access this
object from another form/dialog class I recieve a non static error.
Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Bear in mind that there's no such thing as a "static object" or "non-
static object" - but if you try to access an instance *variable* from
within a static method, you'll need to specify which instance you're
talking about.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 6 '07 #3
It is really simple what I am try to do.

I create a class with a member variable

public class MyObject
{
public string name;
}

Them from within the main frame I create an instance of the object. Then I
have a control that launches a dialog. From within that dialog I try to
update the object (EditMe.name)

class Frame1: frame
{
MyObject EditMe = new MyObject(); create instance when frame is launched

SomeEventHandle ()
{
just create an instance of dialog class
}
SomeOtherContro lHandle()
{
EditMe.name="Th isWorks" ---I can do this with no problem
}
class dialog: frame
{
SomeControlHand le()
{
EditMe.name="te st";--->I can not edit this
}
}

I do not understand why I can not see EditMe from within an instance of
dialog or another class for that matter.

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Muffin <mu****@NoEmail .localwrote:
>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
made it public and am in the same name space for my project. It seem that
I
can only use an object in the same class that I created it in. In this
case
I created my object in my main form class and can access easly from any
control in that class and set any property, however if I try to access
this
object from another form/dialog class I recieve a non static error.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Bear in mind that there's no such thing as a "static object" or "non-
static object" - but if you try to access an instance *variable* from
within a static method, you'll need to specify which instance you're
talking about.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 6 '07 #4
The problem you are having is that the MyObject instance, EditMe, is defined
inside the Frame1 class and you are trying to refer to that instance from the
dialog class, but you have no reference to that instance, by declarig the
EditMe instance it is NOT a global variable, you need to acquire a reference
to it. So for example you need to expose it from inside the Frame1 class
(Since at the moment in your code it is declared private, so nothing outside
of the class can see it) and then inside your SomeControlHand le method you
need to have a reference to an instance of the Frame1 class i.e.

class Frame1 : Frame
{
private MyObject editMe = new MyObject();

public MyObject Foo
{
get
{
return this.editMe;
}
}
}

class Dialog : Frame
{
private Frame1 frame;

//Need a reference to the Frame1 instance we want to use
public Dialog(Frame1 frame)
{
this.frame = frame;
}

public void ControlHandle()
{
this.frame.Foo. name = "bob";
}
}

//Then somewhere you are going to do;
void Bar()
{
Frame1 f = new Frame1();
Dialog d = new Dialog(f);
d.ControlHandle ();
}

Hope that helps
Mark.
--
http://www.markdawson.org
http://themightycoder.spaces.live.com
"Muffin" wrote:
It is really simple what I am try to do.

I create a class with a member variable

public class MyObject
{
public string name;
}

Them from within the main frame I create an instance of the object. Then I
have a control that launches a dialog. From within that dialog I try to
update the object (EditMe.name)

class Frame1: frame
{
MyObject EditMe = new MyObject(); create instance when frame is launched

SomeEventHandle ()
{
just create an instance of dialog class
}
SomeOtherContro lHandle()
{
EditMe.name="Th isWorks" ---I can do this with no problem
}
class dialog: frame
{
SomeControlHand le()
{
EditMe.name="te st";--->I can not edit this
}
}

I do not understand why I can not see EditMe from within an instance of
dialog or another class for that matter.

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Muffin <mu****@NoEmail .localwrote:
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
made it public and am in the same name space for my project. It seem that
I
can only use an object in the same class that I created it in. In this
case
I created my object in my main form class and can access easly from any
control in that class and set any property, however if I try to access
this
object from another form/dialog class I recieve a non static error.
Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Bear in mind that there's no such thing as a "static object" or "non-
static object" - but if you try to access an instance *variable* from
within a static method, you'll need to specify which instance you're
talking about.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Jan 7 '07 #5
Muffin <mu****@NoEmail .localwrote:
It is really simple what I am try to do.

I create a class with a member variable
<snip>
I do not understand why I can not see EditMe from within an instance of
dialog or another class for that matter.
Because dialog doesn't have a member called "EditMe". You've declared
it in a completely different class. How is it meant to know which
instance of MyObject to look at?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 7 '07 #6
Thx for samples. I tryed something little different but in line with your
example. I passed the object to the dialog constructor directly. I am still
kinda confused why I really need to do that if I made the object "public' in
the first place. I am wondering if I truely understand what public really
means.
If I have a name space of lets say "MyTestProj ect" and all the objects
(class, emulation ect) I create are inside of it. Those that are public can
be seen from all of those objects .... right?

Thx,

//////////////////////////////////////////////////////////////////
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}
public config myConfig = new config();--->Why can I not see this
outside of this form since it is public?

private void button1_Click(o bject sender, EventArgs e)
{
dialog MyDialog = new dialog(myConfig );
if(MyDialog.Sho wDialog() == DialogResult.OK )
{
button2.Visible =true;
}
}
public partial class dialog : Form
{
private config myConfigs;
public dialog(config myConfigs)
{
InitializeCompo nent();
this.myConfigs = myConfigs;

}

private void button1_Click(o bject sender, EventArgs e)
{
myConfigs.name= "testit";
}
}

////////////////////////////////////////////////////////////
public partial class dialog : Form
{
private config myConfigs;
public dialog(config myConfigs)
{
InitializeCompo nent();
this.myConfigs = myConfigs;

}

private void button1_Click(o bject sender, EventArgs e)
{
myConfigs.name= "testit";
}
}
/////////////////////////////////////////////////////////////////


"Mark R. Dawson" <Ma*********@di scussions.micro soft.comwrote in message
news:31******** *************** ***********@mic rosoft.com...
The problem you are having is that the MyObject instance, EditMe, is
defined
inside the Frame1 class and you are trying to refer to that instance from
the
dialog class, but you have no reference to that instance, by declarig the
EditMe instance it is NOT a global variable, you need to acquire a
reference
to it. So for example you need to expose it from inside the Frame1 class
(Since at the moment in your code it is declared private, so nothing
outside
of the class can see it) and then inside your SomeControlHand le method you
need to have a reference to an instance of the Frame1 class i.e.

class Frame1 : Frame
{
private MyObject editMe = new MyObject();

public MyObject Foo
{
get
{
return this.editMe;
}
}
}

class Dialog : Frame
{
private Frame1 frame;

//Need a reference to the Frame1 instance we want to use
public Dialog(Frame1 frame)
{
this.frame = frame;
}

public void ControlHandle()
{
this.frame.Foo. name = "bob";
}
}

//Then somewhere you are going to do;
void Bar()
{
Frame1 f = new Frame1();
Dialog d = new Dialog(f);
d.ControlHandle ();
}

Hope that helps
Mark.
--
http://www.markdawson.org
http://themightycoder.spaces.live.com
"Muffin" wrote:
>It is really simple what I am try to do.

I create a class with a member variable

public class MyObject
{
public string name;
}

Them from within the main frame I create an instance of the object. Then
I
have a control that launches a dialog. From within that dialog I try to
update the object (EditMe.name)

class Frame1: frame
{
MyObject EditMe = new MyObject(); create instance when frame is launched

SomeEventHandl e()
{
just create an instance of dialog class
}
SomeOtherContr olHandle()
{
EditMe.name="T hisWorks" ---I can do this with no problem
}
class dialog: frame
{
SomeControlHan dle()
{
EditMe.name="t est";--->I can not edit this
}
}

I do not understand why I can not see EditMe from within an instance of
dialog or another class for that matter.

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******* *************** **@msnews.micro soft.com...
Muffin <mu****@NoEmail .localwrote:
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
made it public and am in the same name space for my project. It seem
that
I
can only use an object in the same class that I created it in. In
this
case
I created my object in my main form class and can access easly from
any
control in that class and set any property, however if I try to access
this
object from another form/dialog class I recieve a non static error.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Bear in mind that there's no such thing as a "static object" or "non-
static object" - but if you try to access an instance *variable* from
within a static method, you'll need to specify which instance you're
talking about.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too



Jan 7 '07 #7
I am not sure if I understand what public means. I thought if I create an
instance that is public I could see it across class within my project name
space.

Thx,

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Muffin <mu****@NoEmail .localwrote:
>It is really simple what I am try to do.

I create a class with a member variable

<snip>
>I do not understand why I can not see EditMe from within an instance of
dialog or another class for that matter.

Because dialog doesn't have a member called "EditMe". You've declared
it in a completely different class. How is it meant to know which
instance of MyObject to look at?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 7 '07 #8
Muffin <mu****@NoEmail .localwrote:
I am not sure if I understand what public means. I thought if I create an
instance that is public I could see it across class within my project name
space.
Objects aren't public, members are. If a member is public, that means
you've got access to it from any assembly. However, if it's an instance
member, you still need to know *which* object you're talking about.

For instance, if I have a Sofa class, it might have a Color property
which could be public - but I can't just say "Sofa.Color " and expect to
find out the color of a particular sofa - I have to specify *which*
sofa I'm talking about.

I strongly suggest you read a book on the introductory concepts of
object orientation (preferably in a C# book) and stick to simple
examples which don't have GUIs to start with. It's better to start
slowly and get a good firm grasp on the concepts than to rush ahead and
get stuck later on, in my experience.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 7 '07 #9
>If a member is public, that means
you've got access to it from any assembly. However, if it's an instance
member, you still need to know *which* object you're talking about
public config myConfig = new config();

I have quite a few books on introductory C# of which I have read at laest
once. I still
get stuck though.

Thx

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Muffin <mu****@NoEmail .localwrote:
>I am not sure if I understand what public means. I thought if I create an
instance that is public I could see it across class within my project
name
space.

Objects aren't public, members are. If a member is public, that means
you've got access to it from any assembly. However, if it's an instance
member, you still need to know *which* object you're talking about.

For instance, if I have a Sofa class, it might have a Color property
which could be public - but I can't just say "Sofa.Color " and expect to
find out the color of a particular sofa - I have to specify *which*
sofa I'm talking about.

I strongly suggest you read a book on the introductory concepts of
object orientation (preferably in a C# book) and stick to simple
examples which don't have GUIs to start with. It's better to start
slowly and get a good firm grasp on the concepts than to rush ahead and
get stuck later on, in my experience.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 7 '07 #10

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

Similar topics

3
4731
by: Hiusing Ngai | last post by:
Hello, I'm porting some C code to VC++ 7. A line of C code is: int (*v); and the C code allocate memory: v = calloc (width*5, sizeof **v); The VC++ 7 has the following erorr when I compile the program. error C2440: '=' : cannot convert from 'void *' to 'int (*)'
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)
7
2523
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...
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...
2
2248
by: chitra lekhaa | last post by:
#include<stdio.h> #include<conio.h> these two are important for my program but this only are giving erorr for me the file is not opening
3
1401
by: fard | last post by:
Hi Everyone i have a table with tow column "Err Number" And "Custom Message" now! Please help me that how can i use the onErorr event to change the default message with my "Custom Message" by using this table. i now that i can use this metode for one message. if DataErr = 0000 then msgbox "xxxxxxxxxx" Response=acDataErrContinue end if
1
1168
by: preeti13 | last post by:
i am trying to dispaly a datagrid but getting the erorr please any one help me with this. private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here SqlConnection oConn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings); SqlDataAdapter da=new SqlDataAdapter("up_reason", oConn); DataSet ds=new DataSet();
1
1433
by: RAJAKUMARMCA | last post by:
what are the erorr in c how to use 1bit for int data type, so what is its range?
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
9671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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
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...
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
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.