469,282 Members | 1,744 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

help with public field access

Hello, would you please consider the following small piece of code.

using System;

class Constants
{
public const int A = B;
public const int B = 2;
}

class Test
{
static void Main()
{
Console.WriteLine("{0}", A);
}
}

I am getting an error that 'A does not exist in the current context'
for Console.WriteLine(...A) above.

I had thought that by declaring A as a PUBLIC field of the Constants
class that it would be accessable by my Test class.

Can someone please explain the error that I have made.

Indebted for your time,

GM.

Oct 12 '06 #1
2 1091
Try this:

Console.WriteLine("{0}", Constants.A);

gw********@hotmail.com wrote:
Hello, would you please consider the following small piece of code.

using System;

class Constants
{
public const int A = B;
public const int B = 2;
}

class Test
{
static void Main()
{
Console.WriteLine("{0}", A);
}
}

I am getting an error that 'A does not exist in the current context'
for Console.WriteLine(...A) above.

I had thought that by declaring A as a PUBLIC field of the Constants
class that it would be accessable by my Test class.

Can someone please explain the error that I have made.

Indebted for your time,

GM.
Oct 12 '06 #2
gw********@hotmail.com wrote:
Hello, would you please consider the following small piece of code.

using System;

class Constants
{
public const int A = B;
public const int B = 2;
}

class Test
{
static void Main()
{
Console.WriteLine("{0}", A);
}
}

I am getting an error that 'A does not exist in the current context'
for Console.WriteLine(...A) above.

I had thought that by declaring A as a PUBLIC field of the Constants
class that it would be accessable by my Test class.

Can someone please explain the error that I have made.

Indebted for your time,

GM.
Hi GM,

You need to specify which class A is coming from. It's perfectly valid to
have two public constants of the same name, provided they are in different
types. You distinguish between them by qualifying the typename, like this:

///
Console.WriteLine( "{0}", Constants.A );
///

--
Hope this helps,
Tom Spink

Google first, ask later.
Oct 12 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by David Gray | last post: by
9 posts views Thread by hope | last post: by
2 posts views Thread by Chris Millar | last post: by
10 posts views Thread by darrel | last post: by
2 posts views Thread by I am Sam | last post: by
2 posts views Thread by Sam Samnah | last post: by
32 posts views Thread by robert d via AccessMonster.com | last post: by
9 posts views Thread by pic078 via AccessMonster.com | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.