473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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
8 1587
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

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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: surrealtrauma | last post by:
the requirement is : Create a class called Rational (rational.h) for performing arithmetic with fractions. Write a program to test your class. Use Integer variables to represent the private data...
21
by: vmsgman | last post by:
Here is a code sample ... int blah = ReadFile( defArray, defFileName, w, h); // Read File Contents into memory array and return for processing public int ReadFile( ref ushort nArray, string...
14
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered...
5
by: Just Me | last post by:
Given a button name Btn_5 and Index=5 I want to do something like dim zz as string = Btn_??Index??.Text or given an array of buttons, do:
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
6
by: Pucca | last post by:
I have a program that originally compiles into a exe file. I changed the compile option to generate dll file. This program calls a com component. Can I use pinvoke in C# to call it? The...
6
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient...
2
by: JH Trauntvein | last post by:
I recently tried an experiment to see if I could define a functor within a class method as shown here: #include <list> #include <algorithm> #include <iostream> class MyClass {
24
by: Paul | last post by:
I am taking over an existing app and having trouble understanding their references. They have encapsulated Pear packages in the directory structure like: /site /site/html/Pear.php...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.