Why can't you define a variable as public within the Main() method?

G

garyusenet

The following piece of code appears in an example i'm reading after the
class has been named.

public string userMessage;

However if i move that into my main method, i'm told that the keyword
public is not recognised. Why is this? I imagine that it might be
useful to reference a variable from outside of the Main() method and it
was my understanding that the public keyword allowed you do to this.

As an example:

This works...
-------------------------------------
using System;

namespace constructors
{
class test
{
// point of state data
public string userMessage;

public static void Main()
{

}

}

}
-------------------------------------

But this doesn't
-------------------------------------
using System;

namespace constructors
{
class test
{

public static void Main()
{
// i can't create a public string in the main method

public string userMessage;

}

}

}
 
N

niklas.arbin

A variable declared within a method only "lives" in that method thus
you cannot access it from outside of said method making the public
statement useless.

/niklas

(e-mail address removed) skrev:
 
Z

zhivago

Gary,

It's not called PUBLIC for nothing, Gary. Public means it's PUBLIC to
all methods.
Placing the declaration WITHIN a certain class would make it private to
that class.
In C, never neglect the topic of scoping; that's why placing the
declaration after the
class definition makes it public.

Derek
 
M

Marc Gravell

In your case, I suspect the issue has more to do with "static"... your
"this works" scenario still won't allow Main to talk to userMessage,
as Main is static and userMessage isn't. Simply declare userMessage as
a static string and you should be fine. Note, however, that a: it is
bad practice to make fields public (properties being preferred), and
b: static members should generally be made thread-safe, which yours
probably aren't. The better question would be "what are you trying to
do?" so that we can give a proper answer...

In the more general: method variables are defined *per call* of a
method via the stack. When you consider recursion etc, you could
easily have 15 (random number) active calls to the same method *at the
same time*, even on a single thread... so which one would the "public
variable" refer to? To use a value outside of the method requires a
field or a parameter. Alternatively, note that variables can be lifted
(to fields on a compiler-provided class instance) as "captured
variables" using inline (anonymous) delegates, but this is a slightly
different issue.

Marc
 
N

niklas.arbin

zhivago skrev:
Gary,

It's not called PUBLIC for nothing, Gary. Public means it's PUBLIC to
all methods.

No, public means its visible outside of its own class.

Placing the declaration WITHIN a certain class would make it private to
that class.

?? Private to itself(???)
In C, never neglect the topic of scoping; that's why placing the
declaration after the
class definition makes it public.

No, its the keyword "public" that makes it public.
 
J

Joanna Carter [TeamB]

"Marc Gravell" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Alternatively, note that variables can be lifted
| (to fields on a compiler-provided class instance) as "captured
| variables" using inline (anonymous) delegates, but this is a slightly
| different issue.

Marc, could you reply to this message in another topic, explaining what you
mean by this paragraph?

Joanna
 
D

Dave Sexton

Hi Joanna,

In the following code:

static void Main()
{
int localVariable = 10;

System.Threading.ThreadStart start = delegate()
{
Console.WriteLine(localVariable);
};

start.Invoke();
}

the compiler generates an anonymous class adorned with a "localVariable"
field member. So the local variable actually becomes a field with public
scope.

Compile that code and open it up in Reflector to see.
 
M

Marc Gravell

Saved me a job... the reason I mention it is that of course both
methods are now reading/updating the same "variable" (field). This can
be very bad and confusing in many scenarios (particularly threading),
but very elegant and tidy in others. Use with caution (esp. when
threading), but worth knowing about. I think it would be a lousy
solution to the OP's question, but worth mentioning for completeness.

Marc
 

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