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

static Issue

P: n/a
All of a sudden all my C# apps require the keyword static on all global fields and methods that I create. Even in the simplest of console apps. Can someone tell me what I have inadvertenly set in the IDE or did something else happen to cause this?

I get the following error with the sample code below:

D:\Projects\DotNet\C-Sharp\Test\Class1.cs(12): An object reference is required for the nonstatic field, method, or property 'Test.Class1.matt'

using System;
namespace Test

{

class Class1

{

private string matt;

[STAThread]

static void Main(string[] args)

{

matt = "test";

Console.WriteLine(matt);

}

}

}

Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
The field "matt" is a local data member that requires an instance of the class to be referenced. In this case the Main() procedure is a static, and so all that it references must be static. In order to access "matt" you would have to create an instance of the class inside the Main() procedure ("Class1 localClass = new Class1;") and its constructor and methods could access "matt" -- but the Main() still could not since you declared it private.

-Ken
"Matt" <md*****@sorvive.DONT-SEND-SPAM.com> wrote in message news:uS**************@TK2MSFTNGP10.phx.gbl...
All of a sudden all my C# apps require the keyword static on all global fields and methods that I create. Even in the simplest of console apps. Can someone tell me what I have inadvertenly set in the IDE or did something else happen to cause this?

I get the following error with the sample code below:

D:\Projects\DotNet\C-Sharp\Test\Class1.cs(12): An object reference is required for the nonstatic field, method, or property 'Test.Class1.matt'

using System;
namespace Test

{

class Class1

{

private string matt;

[STAThread]

static void Main(string[] args)

{

matt = "test";

Console.WriteLine(matt);

}

}

}

Nov 16 '05 #2

P: n/a
Matt <md*****@sorvive.DONT-SEND-SPAM.com> wrote:
All of a sudden all my C# apps require the keyword static on all
global fields and methods that I create. Even in the simplest of
console apps. Can someone tell me what I have inadvertenly set in the
IDE or did something else happen to cause this?


No - there's no IDE setting involved. The code you posted would never
have compiled.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

P: n/a
Here you go Matt. Even though the program entry point is located within the class, you cannot directly access its members. The only difference between the entry point being here rather than within another class means that you can successfully instanciate the class EVEN IF you have marked it's constructor as private - this is something that wouldnt work if the entry point was in another class.

using System;
namespace Test

{

class Class1

{

private string matt;
//private Class1(){} //to see what I mean remove comment and this instanciation of Class1 will still work within Main!
[STAThread]

static void Main()
{
{
Class1 c = new Class1();
c.matt = "test";
Console.WriteLine(c.matt);
Console.ReadLine();

}

}

}
Nov 16 '05 #4

P: n/a
"Ken Allen" <ke******@sympatico.ca> a écrit dans le message de news:eF****************@TK2MSFTNGP10.phx.gbl...
-- but the Main() still could not since you declared it private.

-Ken
I disagree with this part.
Even thought matt is private, it does not matter wether or not main is static. You CAN access all member of the class form main method (static members, or instance members with a valid instance of course).

Fred
Nov 16 '05 #5

P: n/a
First, just to correct (or at least clarify your comments), static members can only be accessed through their class in C# (not so in VB which allows instance as well).

Secondly Matt, I would just like to show you another bit of code. Let me just say that the behaviour of what is happening is slightly confused because the Main method is located within the Class itself and so code within is able to "peek" at private members. This kind of breaks the encapsulation rule in a sense BUT it is obvious why it is doing it - and maybe future C# specifications should enforce entry points to be the only member of a class (open to debate), I however quite like this quirk.

If you see the code below you will see that all members of the class are accessible to Main(), however the public and private members *must* be accessed via and instance of the class and the static public and static private members *must* be accessed through the class itself and *cannot* be accessed through an instance.

Just before I show you the code listing though, I advise you not to get too hung up with this scenario and for a better illustration to see how it all works normally it is probably best to put your entry point in a completely separate class.

using System;

namespace Test

{

class Class1

{

public static string MATT = "public static Matt";

private static string Matt = "private static Matt";

private string matt = "private matt";

public string MatT = "public matt";

//private Class1(){} //to see what I mean remove comment and this instanciation of Class1 will still work within Main!

[STAThread]

static void Main()

{

{

// matt = "" //is inaccessible unless reference through instance

Class1 c = new Class1();

Console.WriteLine(c.matt); //is accessible through instance only from within this Main (cos it is private)

Console.WriteLine(c.MatT); //is accessible through instance (cos it is public)
Console.WriteLine(Matt); //is accessible through class only from within this Main (cos it is private)

Console.WriteLine(Matt); //is accessible through class (cos it is public)

Console.ReadLine();

}

}

}

}

--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"Iceman" <fr************************@neuf.fr> wrote in message news:c8**********@apollon.grec.isp.9tel.net...
"Ken Allen" <ke******@sympatico.ca> a écrit dans le message de news:eF****************@TK2MSFTNGP10.phx.gbl...
-- but the Main() still could not since you declared it private.

-Ken
I disagree with this part.
Even thought matt is private, it does not matter wether or not main is static. You CAN access all member of the class form main method (static members, or instance members with a valid instance of course).

Fred
Nov 16 '05 #6

P: n/a
Thanks everyone for your comments. As soon as I posted this I saw the error
of my ways. I have been doing so much OOP and using them in other apps that
when I had to create a quick and dirty exe I totally forgot about class
instantiation.

Thanks to everyone for your answers as I did learn some things I was not
aware of.

Matt

"Matt" <md*****@sorvive.DONT-SEND-SPAM.com> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl...
All of a sudden all my C# apps require the keyword static on all global
fields and methods that I create. Even in the simplest of console apps.
Can someone tell me what I have inadvertenly set in the IDE or did something
else happen to cause this?

I get the following error with the sample code below:

D:\Projects\DotNet\C-Sharp\Test\Class1.cs(12): An object reference is
required for the nonstatic field, method, or property 'Test.Class1.matt'

using System;
namespace Test

{

class Class1

{

private string matt;

[STAThread]

static void Main(string[] args)

{

matt = "test";

Console.WriteLine(matt);

}

}

}
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.