static Issue

M

Matt

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);

}

}

}
 
K

Ken Allen

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
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);

}

}

}
 
J

Jon Skeet [C# MVP]

Matt said:
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.
 
M

Mark Broadbent

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();

}

}

}
 
I

Iceman

"Ken Allen" <[email protected]> a écrit dans le message de -- 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
 
M

Mark Broadbent

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
=============
"Ken Allen" <[email protected]> a écrit dans le message de -- 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
 
M

Matt

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

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);

}

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top