473,396 Members | 2,013 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,396 software developers and data experts.

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 1291
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.com>
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
}
SomeOtherControlHandle()
{
EditMe.name="ThisWorks" ---I can do this with no problem
}
class dialog: frame
{
SomeControlHandle()
{
EditMe.name="test";--->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.comwrote in message
news:MP************************@msnews.microsoft.c om...
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.com>
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 SomeControlHandle 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
}
SomeOtherControlHandle()
{
EditMe.name="ThisWorks" ---I can do this with no problem
}
class dialog: frame
{
SomeControlHandle()
{
EditMe.name="test";--->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.comwrote in message
news:MP************************@msnews.microsoft.c om...
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.com>
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.com>
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 "MyTestProject" 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()
{
InitializeComponent();
}
public config myConfig = new config();--->Why can I not see this
outside of this form since it is public?

private void button1_Click(object sender, EventArgs e)
{
dialog MyDialog = new dialog(myConfig);
if(MyDialog.ShowDialog() == DialogResult.OK)
{
button2.Visible=true;
}
}
public partial class dialog : Form
{
private config myConfigs;
public dialog(config myConfigs)
{
InitializeComponent();
this.myConfigs = myConfigs;

}

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

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

}

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


"Mark R. Dawson" <Ma*********@discussions.microsoft.comwrote in message
news:31**********************************@microsof t.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 SomeControlHandle 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
}
SomeOtherControlHandle()
{
EditMe.name="ThisWorks" ---I can do this with no problem
}
class dialog: frame
{
SomeControlHandle()
{
EditMe.name="test";--->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.comwrote in message
news:MP************************@msnews.microsoft. 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.com>
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.comwrote in message
news:MP************************@msnews.microsoft.c om...
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.com>
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.com>
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.comwrote in message
news:MP************************@msnews.microsoft.c om...
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.com>
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
Muffin <mu****@NoEmail.localwrote:
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'm not sure what that's meant to tell us, but unless you then specify
that you want to use myConfig in order to get to things, it's not going
to make any odds.
I have quite a few books on introductory C# of which I have read at laest
once. I still get stuck though.
I'd reread them, concentrating specifically on understanding how
objects are referenced. Note that this is *not* an access issue in
terms of public/private.

--
Jon Skeet - <sk***@pobox.com>
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 #11

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

Similar topics

3
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...
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;...
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...
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...
2
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
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...
1
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...
1
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
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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...
0
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...
0
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...
0
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...

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.