By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,533 Members | 2,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,533 IT Pros & Developers. It's quick & easy.

dumb question.. please help!

P: n/a
Hi peeps

Ok, so I thought I'd have a go at making a console app in VS2k5... I haven't
written any windows apps for years, let alone dos apps (been web programming)
and I've hit a dumb error...

"An object reference is required for the nonstatic field, method or property
[propertyname]"

This is occuring in my main function...

public static void Main(string[] args)
{
DisplayHeader();
}

Display header is defined like so...

public void DisplayHeader()
{
Console.WriteLine("hello!");
}

What's going on - I've tried making everything static, but I get errors.
I've tried making main nonstatic, but it didn't like that either. What am I
missing? This also happens on class-wide variables as well!?

Help!

Dan
Nov 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
You either need to make the method 'DisplayHeader' static or create an
instance of the class:

e.g.
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 instance = new Class1();
instance.DisplayHeader();
}

public void DisplayHeader()
{
Console.WriteLine("hello!");
}
}
or

// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
DisplayHeader();
}
public static void DisplayHeader()
{
Console.WriteLine("hello!");
}
}

HTH

Ollie Riches

an@musoswire.com> wrote in message
news:60**********************************@microsof t.com...
Hi peeps

Ok, so I thought I'd have a go at making a console app in VS2k5... I
haven't
written any windows apps for years, let alone dos apps (been web
programming)
and I've hit a dumb error...

"An object reference is required for the nonstatic field, method or
property
[propertyname]"

This is occuring in my main function...

public static void Main(string[] args)
{
DisplayHeader();
}

Display header is defined like so...

public void DisplayHeader()
{
Console.WriteLine("hello!");
}

What's going on - I've tried making everything static, but I get errors.
I've tried making main nonstatic, but it didn't like that either. What am
I
missing? This also happens on class-wide variables as well!?

Help!

Dan

Nov 17 '05 #2

P: n/a
Forgive me... I don't understand why I have to do that when the 2 functions
are in the same class? Surely the fact that main() is executing means that
there must be an instance of that class already?!

"Ollie Riches" wrote:
You either need to make the method 'DisplayHeader' static or create an
instance of the class:

e.g.
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 instance = new Class1();
instance.DisplayHeader();
}

public void DisplayHeader()
{
Console.WriteLine("hello!");
}
}
or

// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
DisplayHeader();
}
public static void DisplayHeader()
{
Console.WriteLine("hello!");
}
}

HTH

Ollie Riches

an@musoswire.com> wrote in message
news:60**********************************@microsof t.com...
Hi peeps

Ok, so I thought I'd have a go at making a console app in VS2k5... I
haven't
written any windows apps for years, let alone dos apps (been web
programming)
and I've hit a dumb error...

"An object reference is required for the nonstatic field, method or
property
[propertyname]"

This is occuring in my main function...

public static void Main(string[] args)
{
DisplayHeader();
}

Display header is defined like so...

public void DisplayHeader()
{
Console.WriteLine("hello!");
}

What's going on - I've tried making everything static, but I get errors.
I've tried making main nonstatic, but it didn't like that either. What am
I
missing? This also happens on class-wide variables as well!?

Help!

Dan


Nov 17 '05 #3

P: n/a

dhnriverside wrote:
Forgive me... I don't understand why I have to do that when the 2 functions
are in the same class? Surely the fact that main() is executing means that
there must be an instance of that class already?!
No, because main() is declared static!

"Ollie Riches" wrote:
You either need to make the method 'DisplayHeader' static or create an
instance of the class:

e.g.
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 instance = new Class1();
instance.DisplayHeader();
}

public void DisplayHeader()
{
Console.WriteLine("hello!");
}
}
or

// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
DisplayHeader();
}
public static void DisplayHeader()
{
Console.WriteLine("hello!");
}
}

HTH

Ollie Riches

an@musoswire.com> wrote in message
news:60**********************************@microsof t.com...
Hi peeps

Ok, so I thought I'd have a go at making a console app in VS2k5... I
haven't
written any windows apps for years, let alone dos apps (been web
programming)
and I've hit a dumb error...

"An object reference is required for the nonstatic field, method or
property
[propertyname]"

This is occuring in my main function...

public static void Main(string[] args)
{
DisplayHeader();
}

Display header is defined like so...

public void DisplayHeader()
{
Console.WriteLine("hello!");
}

What's going on - I've tried making everything static, but I get errors.
I've tried making main nonstatic, but it didn't like that either. What am
I
missing? This also happens on class-wide variables as well!?

Help!

Dan



Nov 17 '05 #4

P: n/a
The declaration that your DisplayHeader has has made your method an
"instance method". You r calling it from "static function" which is not an
instance of a class, hence cant call instance method from there. As Ollie
said, either declare your function as static, OR make an instance of a class
and call through that instance object variable. Dont think about : "becuz
its inside some class it has to be an instance". In C# all code is written
inside a class.

Ab.

http://joehacker.blogspot.com

"dhnriverside" <da*@musoswire.com> wrote in message
news:55**********************************@microsof t.com...
Forgive me... I don't understand why I have to do that when the 2 functions are in the same class? Surely the fact that main() is executing means that
there must be an instance of that class already?!

"Ollie Riches" wrote:
You either need to make the method 'DisplayHeader' static or create an
instance of the class:

e.g.
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 instance = new Class1();
instance.DisplayHeader();
}

public void DisplayHeader()
{
Console.WriteLine("hello!");
}
}
or

// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
DisplayHeader();
}
public static void DisplayHeader()
{
Console.WriteLine("hello!");
}
}

HTH

Ollie Riches

an@musoswire.com> wrote in message
news:60**********************************@microsof t.com...
Hi peeps

Ok, so I thought I'd have a go at making a console app in VS2k5... I
haven't
written any windows apps for years, let alone dos apps (been web
programming)
and I've hit a dumb error...

"An object reference is required for the nonstatic field, method or
property
[propertyname]"

This is occuring in my main function...

public static void Main(string[] args)
{
DisplayHeader();
}

Display header is defined like so...

public void DisplayHeader()
{
Console.WriteLine("hello!");
}

What's going on - I've tried making everything static, but I get errors. I've tried making main nonstatic, but it didn't like that either. What am I
missing? This also happens on class-wide variables as well!?

Help!

Dan


Nov 17 '05 #5

P: n/a
Thanks for all your help.. I'm with that now!

Although, I've still got a problem. I can't access any variables/classes
from my main() function....

namespace xxxx
{
class xxxx
{
private MyClass str;

public static void main()
{
disp = new MyClass();

That's as far as I get. When I start typing disp, the intellisense doesn't
fire or anything. If I declare the variable as static, it works. It's the
same with variables and classes. Surely I don't have to declare absolutely
everything as static?!!

Why is this

Now then, do I have to declare everything as static?!
"Abubakar" wrote:
The declaration that your DisplayHeader has has made your method an
"instance method". You r calling it from "static function" which is not an
instance of a class, hence cant call instance method from there. As Ollie
said, either declare your function as static, OR make an instance of a class
and call through that instance object variable. Dont think about : "becuz
its inside some class it has to be an instance". In C# all code is written
inside a class.

Ab.

http://joehacker.blogspot.com

"dhnriverside" <da*@musoswire.com> wrote in message
news:55**********************************@microsof t.com...
Forgive me... I don't understand why I have to do that when the 2

functions
are in the same class? Surely the fact that main() is executing means that
there must be an instance of that class already?!

"Ollie Riches" wrote:
You either need to make the method 'DisplayHeader' static or create an
instance of the class:

e.g.
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 instance = new Class1();
instance.DisplayHeader();
}

public void DisplayHeader()
{
Console.WriteLine("hello!");
}
}
or

// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
DisplayHeader();
}
public static void DisplayHeader()
{
Console.WriteLine("hello!");
}
}

HTH

Ollie Riches

an@musoswire.com> wrote in message
news:60**********************************@microsof t.com...
> Hi peeps
>
> Ok, so I thought I'd have a go at making a console app in VS2k5... I
> haven't
> written any windows apps for years, let alone dos apps (been web
> programming)
> and I've hit a dumb error...
>
> "An object reference is required for the nonstatic field, method or
> property
> [propertyname]"
>
> This is occuring in my main function...
>
> public static void Main(string[] args)
> {
> DisplayHeader();
> }
>
> Display header is defined like so...
>
> public void DisplayHeader()
> {
> Console.WriteLine("hello!");
> }
>
> What's going on - I've tried making everything static, but I get errors. > I've tried making main nonstatic, but it didn't like that either. What am > I
> missing? This also happens on class-wide variables as well!?
>
> Help!
>
>
>
> Dan
>
>


Nov 17 '05 #6

P: n/a
Your variables are declared as __instance__ members. That means that they
belong to only instances of your class.

Static variables/methods/etc belong to the class (or you could think of them
as belonging to all members of the class. Here's an example of the
difference (untested code):

class Class1 {
int count = 0;
static int static_count = 0;

public static void main() {

for(int i = 0; i<10; i++) {
Class1 obj = new Class1();
obj.count++;
Class1.static_count++;
Console.WriteLine("Instance count: {0}", obj.count);
Console.WriteLine("Static count: {0}", Class1.static_count);
}
}
}

Notice that static members can be called from _any_ class (as long as they
are public) using the ClassName.variable or ClassName.method() syntax,
whereas instance variables can only be called once an instance has been
created by calling new ClassName();

Maybe microsoft can do a better job explaining this:
http://msdn.microsoft.com/library/de...rfstaticpg.asp

Scott

"dhnriverside" <da*@musoswire.com> wrote in message
news:FD**********************************@microsof t.com...
Thanks for all your help.. I'm with that now!

Although, I've still got a problem. I can't access any variables/classes
from my main() function....

namespace xxxx
{
class xxxx
{
private MyClass str;

public static void main()
{
disp = new MyClass();

That's as far as I get. When I start typing disp, the intellisense doesn't
fire or anything. If I declare the variable as static, it works. It's the
same with variables and classes. Surely I don't have to declare absolutely
everything as static?!!

Why is this

Now then, do I have to declare everything as static?!
"Abubakar" wrote:
The declaration that your DisplayHeader has has made your method an
"instance method". You r calling it from "static function" which is not
an
instance of a class, hence cant call instance method from there. As Ollie
said, either declare your function as static, OR make an instance of a
class
and call through that instance object variable. Dont think about : "becuz
its inside some class it has to be an instance". In C# all code is
written
inside a class.

Ab.

http://joehacker.blogspot.com

"dhnriverside" <da*@musoswire.com> wrote in message
news:55**********************************@microsof t.com...
> Forgive me... I don't understand why I have to do that when the 2

functions
> are in the same class? Surely the fact that main() is executing means
> that
> there must be an instance of that class already?!
>
> "Ollie Riches" wrote:
>
> > You either need to make the method 'DisplayHeader' static or create
> > an
> > instance of the class:
> >
> > e.g.
> >
> >
> > /// <summary>
> > /// Summary description for Class1.
> > /// </summary>
> > class Class1
> > {
> > /// <summary>
> > /// The main entry point for the application.
> > /// </summary>
> > [STAThread]
> > static void Main(string[] args)
> > {
> > Class1 instance = new Class1();
> > instance.DisplayHeader();
> > }
> >
> > public void DisplayHeader()
> > {
> > Console.WriteLine("hello!");
> > }
> > }
> >
> >
> > or
> >
> > // <summary>
> > /// Summary description for Class1.
> > /// </summary>
> > class Class1
> > {
> > /// <summary>
> > /// The main entry point for the application.
> > /// </summary>
> > [STAThread]
> > static void Main(string[] args)
> > {
> > DisplayHeader();
> > }
> > public static void DisplayHeader()
> > {
> > Console.WriteLine("hello!");
> > }
> > }
> >
> > HTH
> >
> > Ollie Riches
> >
> >
> >
> > an@musoswire.com> wrote in message
> > news:60**********************************@microsof t.com...
> > > Hi peeps
> > >
> > > Ok, so I thought I'd have a go at making a console app in VS2k5...
> > > I
> > > haven't
> > > written any windows apps for years, let alone dos apps (been web
> > > programming)
> > > and I've hit a dumb error...
> > >
> > > "An object reference is required for the nonstatic field, method or
> > > property
> > > [propertyname]"
> > >
> > > This is occuring in my main function...
> > >
> > > public static void Main(string[] args)
> > > {
> > > DisplayHeader();
> > > }
> > >
> > > Display header is defined like so...
> > >
> > > public void DisplayHeader()
> > > {
> > > Console.WriteLine("hello!");
> > > }
> > >
> > > What's going on - I've tried making everything static, but I get

errors.
> > > I've tried making main nonstatic, but it didn't like that either.
> > > What

am
> > > I
> > > missing? This also happens on class-wide variables as well!?
> > >
> > > Help!
> > >
> > >
> > >
> > > Dan
> > >
> > >
> >
> >
> >


Nov 17 '05 #7

P: n/a
You havnt declared the "disp" anywhere. Its the "str" variable that u r
trying to access here. Try that. And also try to follow the code that Ollie
wrote for you in his earlier reply.
Goto msdn and search for tutorials on C# language, that'll help a lot.

Ab.
"dhnriverside" <da*@musoswire.com> wrote in message
news:FD**********************************@microsof t.com...
Thanks for all your help.. I'm with that now!

Although, I've still got a problem. I can't access any variables/classes
from my main() function....

namespace xxxx
{
class xxxx
{
private MyClass str;

public static void main()
{
disp = new MyClass();

That's as far as I get. When I start typing disp, the intellisense doesn't
fire or anything. If I declare the variable as static, it works. It's the
same with variables and classes. Surely I don't have to declare absolutely
everything as static?!!

Why is this

Now then, do I have to declare everything as static?!
"Abubakar" wrote:
The declaration that your DisplayHeader has has made your method an
"instance method". You r calling it from "static function" which is not an instance of a class, hence cant call instance method from there. As Ollie said, either declare your function as static, OR make an instance of a class and call through that instance object variable. Dont think about : "becuz its inside some class it has to be an instance". In C# all code is written inside a class.

Ab.

http://joehacker.blogspot.com

"dhnriverside" <da*@musoswire.com> wrote in message
news:55**********************************@microsof t.com...
Forgive me... I don't understand why I have to do that when the 2

functions
are in the same class? Surely the fact that main() is executing means that there must be an instance of that class already?!

"Ollie Riches" wrote:

> You either need to make the method 'DisplayHeader' static or create an > instance of the class:
>
> e.g.
>
>
> /// <summary>
> /// Summary description for Class1.
> /// </summary>
> class Class1
> {
> /// <summary>
> /// The main entry point for the application.
> /// </summary>
> [STAThread]
> static void Main(string[] args)
> {
> Class1 instance = new Class1();
> instance.DisplayHeader();
> }
>
> public void DisplayHeader()
> {
> Console.WriteLine("hello!");
> }
> }
>
>
> or
>
> // <summary>
> /// Summary description for Class1.
> /// </summary>
> class Class1
> {
> /// <summary>
> /// The main entry point for the application.
> /// </summary>
> [STAThread]
> static void Main(string[] args)
> {
> DisplayHeader();
> }
> public static void DisplayHeader()
> {
> Console.WriteLine("hello!");
> }
> }
>
> HTH
>
> Ollie Riches
>
>
>
> an@musoswire.com> wrote in message
> news:60**********************************@microsof t.com...
> > Hi peeps
> >
> > Ok, so I thought I'd have a go at making a console app in VS2k5... I > > haven't
> > written any windows apps for years, let alone dos apps (been web
> > programming)
> > and I've hit a dumb error...
> >
> > "An object reference is required for the nonstatic field, method or > > property
> > [propertyname]"
> >
> > This is occuring in my main function...
> >
> > public static void Main(string[] args)
> > {
> > DisplayHeader();
> > }
> >
> > Display header is defined like so...
> >
> > public void DisplayHeader()
> > {
> > Console.WriteLine("hello!");
> > }
> >
> > What's going on - I've tried making everything static, but I get

errors.
> > I've tried making main nonstatic, but it didn't like that either.
What am
> > I
> > missing? This also happens on class-wide variables as well!?
> >
> > Help!
> >
> >
> >
> > Dan
> >
> >
>
>
>


Nov 17 '05 #8

P: n/a
The class that contains the Main() method is always a bit confusing.
There is a way of using the Main method that may help you along here.

First, some background information. The "world" inside your class
definition is divided into two separate parts: "instance things" and
"static things". Instance things (fields, methods, etc) exist
separately for each object that you create from the class. So, if you
declare an instance member:

class Class1 {
int count = 0;
...

then every time you create a "new Class1()", you will get a new "count"
along with it. You can work with "count": read it, manipulate it... but
only from within _instance methods_. That is, only from methods and
properties that belong to this "instance world" within your class. It's
a good habit to get into to always refer to your instance fields by
preceding the reference with "this.", like this:

this.count += 1;

The other world is "static stuff". Static fields, methods, and
properties exist once for the class as a whole. So, for example, if you
were to declare:

public class Class1 {
static int static_count = 0;
...

there is only one static_count shared amongst all instances of your
Class1. Every Class1 object can see the same static_count. You refer to
static fields, properties, etc. by preceding them by the class name:

Class1.static_count += 1;

Now, if you don't indicate to the compiler whether you want an instance
member or a static member (that is, if you mention the name of
something without specifying "this." or "Class1.", then the compiler
will simply find the item by its name, and deal with it accordingly.
This is very easy for the compiler, but difficult for human readers, so
I prefer to use "this." and "Class1." all over the place to make it
clear.

Now, here's the rub: the individual objects that you instantiate from
your class can all see static members: all they have to say is
Class1.static_count and they find the field. _However_, (and this is
the important part) static methods and properties _can't_ see instance
members: if you're in your "static void Main" method, and you say
"this.count", which instance are you talking about? There's only one
Main method, shared amongst all instances. If you make 500 instance of
your object, and in your Main method you say "this.count", which one of
the 500 counts out there are you referring to? Of course, if you have
an object reference, you can certainly do it:

public static void Main()
{
Class1 c1 = new Class1();
c1.count += 1;
}

but here you've specifically said "c1's count". You can't say
"this.count" because in a static method, there is no instance... no
"this".

So, here is the advice about structuring Main methods: don't do
anything in your Main method. You can just instantiate the object that
contains your program, perhaps call some sort of Run() method or
something, and that's it. Here's an example:

public Class1
{
public Class1()
{
... set up your class...
}

public void Run()
{
... do whatever your program needs to do...
}

// Very simple Main method that just creates an instance of the
class and kicks off the program.
public static void Main()
{
Class1 myProgram = new Class1();
myProgram.Run();
}
}

This gets you out of the "static" world and into the instance world as
soon as possible. Certainly it's debatable practice: many people prefer
to write most of their logic in Main(), but if you're going to have
both a static Main and an instance, I'd rather keep everything on one
side of the fence, so to speak, rather than have one foot in each camp
and try to keep it all straight.

Particularly while you're learning the language and the concepts, you
might start off this way to keep things simpler.

Nov 17 '05 #9

P: n/a
Main is just the entry point for your application. One could argue that
every non
trivial class outside of the application class should have a Main that
is used to
test, stress the class methods. You can tell the IDE what Main to use as
the entry
point at runtime in the "Startup Object" property.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #10

P: n/a
Good job, Bruce. Your answer was much more complete (and illustrative) than
mine was.

Scott
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
The class that contains the Main() method is always a bit confusing.
There is a way of using the Main method that may help you along here.

First, some background information. The "world" inside your class
definition is divided into two separate parts: "instance things" and
"static things". Instance things (fields, methods, etc) exist
separately for each object that you create from the class. So, if you
declare an instance member:

class Class1 {
int count = 0;
...

then every time you create a "new Class1()", you will get a new "count"
along with it. You can work with "count": read it, manipulate it... but
only from within _instance methods_. That is, only from methods and
properties that belong to this "instance world" within your class. It's
a good habit to get into to always refer to your instance fields by
preceding the reference with "this.", like this:

this.count += 1;

The other world is "static stuff". Static fields, methods, and
properties exist once for the class as a whole. So, for example, if you
were to declare:

public class Class1 {
static int static_count = 0;
...

there is only one static_count shared amongst all instances of your
Class1. Every Class1 object can see the same static_count. You refer to
static fields, properties, etc. by preceding them by the class name:

Class1.static_count += 1;

Now, if you don't indicate to the compiler whether you want an instance
member or a static member (that is, if you mention the name of
something without specifying "this." or "Class1.", then the compiler
will simply find the item by its name, and deal with it accordingly.
This is very easy for the compiler, but difficult for human readers, so
I prefer to use "this." and "Class1." all over the place to make it
clear.

Now, here's the rub: the individual objects that you instantiate from
your class can all see static members: all they have to say is
Class1.static_count and they find the field. _However_, (and this is
the important part) static methods and properties _can't_ see instance
members: if you're in your "static void Main" method, and you say
"this.count", which instance are you talking about? There's only one
Main method, shared amongst all instances. If you make 500 instance of
your object, and in your Main method you say "this.count", which one of
the 500 counts out there are you referring to? Of course, if you have
an object reference, you can certainly do it:

public static void Main()
{
Class1 c1 = new Class1();
c1.count += 1;
}

but here you've specifically said "c1's count". You can't say
"this.count" because in a static method, there is no instance... no
"this".

So, here is the advice about structuring Main methods: don't do
anything in your Main method. You can just instantiate the object that
contains your program, perhaps call some sort of Run() method or
something, and that's it. Here's an example:

public Class1
{
public Class1()
{
... set up your class...
}

public void Run()
{
... do whatever your program needs to do...
}

// Very simple Main method that just creates an instance of the
class and kicks off the program.
public static void Main()
{
Class1 myProgram = new Class1();
myProgram.Run();
}
}

This gets you out of the "static" world and into the instance world as
soon as possible. Certainly it's debatable practice: many people prefer
to write most of their logic in Main(), but if you're going to have
both a static Main and an instance, I'd rather keep everything on one
side of the fence, so to speak, rather than have one foot in each camp
and try to keep it all straight.

Particularly while you're learning the language and the concepts, you
might start off this way to keep things simpler.

Nov 17 '05 #11

P: n/a
> One could argue that every non-trivial class outside of the application class should have a Main that is used to test, stress the class methods.

NUnit is a better option for unit testing:

http://www.nunit.org/

Nov 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.