help with public field access

G

gwainguard

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.
 
T

Tom Spink

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

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

Similar Threads


Top