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

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

P: n/a
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;

}

}

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

Many Thanks,

Gary-

Dec 20 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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

ga********@myway.com skrev:
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;

}

}

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

Many Thanks,

Gary-
Dec 20 '06 #2

P: n/a

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

On Dec 20, 11:32 am, garyuse...@myway.com wrote:
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;

}

}

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

Many Thanks,

Gary-
Dec 20 '06 #3

P: n/a
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
Dec 20 '06 #4

P: n/a
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.
>
Derek
Dec 20 '06 #5

P: n/a
"Marc Gravell" <ma**********@gmail.coma écrit dans le message de news:
u8**************@TK2MSFTNGP06.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 20 '06 #6

P: n/a
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.

--
Dave Sexton

"Joanna Carter [TeamB]" <jo****@not.for.spamwrote in message
news:ub**************@TK2MSFTNGP02.phx.gbl...
"Marc Gravell" <ma**********@gmail.coma écrit dans le message de news:
u8**************@TK2MSFTNGP06.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer


Dec 20 '06 #7

P: n/a
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
Dec 20 '06 #8

P: n/a
Hi Marc,

Agreed :)

--
Dave Sexton

"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2*****************@TK2MSFTNGP04.phx.gbl...
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

Dec 20 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.