Constant Scope

  • Thread starter Thread starter AMP
  • Start date Start date
A

AMP

Hello,
I have a class:
namespace Constant
{
class Constants
{
public const int Mike = 12;
public const int Anna = 14;
}
}

and another:
namespace Constant
{
class ConstantReader
{

public void ConstantReaderMethod()
{
Console.WriteLine(Constants.Anna);
}
}
}

I can read the Constants.Anna value without any reference to the
Constants class, how come?
I would have figured I would need a Constant C = new Constants(), but
I dont.
Any help is always appreciated.
Thanks
Mike
 
Hello,
I have a class:
namespace Constant
{
    class Constants
    {
        public const int Mike = 12;
        public const int Anna = 14;
    }
}
[...]
I can read the Constants.Anna value without any reference to the
Constants class, how come?

Because "const" values are implicitly "static".

Seehttp://msdn.microsoft.com/en-us/library/e6w8fe1b.aspxand  http://msdn.microsoft.com/en-us/library/98f28cdx.aspx

Pete


Pete,
But dont I need a reference to my Constants class somewhere?
I didnt see in either of those articles that a constant has Namespace
scope. Where can I find that?
Thanks
Mike
 
Hello,
I have a class:
namespace Constant
{
    class Constants
    {
        public const int Mike = 12;
        public const int Anna = 14;
    }
}
[...]
I can read the Constants.Anna value without any reference to the
Constants class, how come?
Because "const" values are implicitly "static".

Pete

Pete,
But dont I need a reference to my Constants class somewhere?
I didnt see in either of those articles that a constant has Namespace
scope. Where can I find that?
Thanks
Mike- Hide quoted text -

- Show quoted text -

Holy crap I screwed that up. I fully qualified it right?
 
Yes, essentially.  I was trying to figure out what you were asking, since  
you did "refer" to your Constants class.  :)

Technically, it's not "fully qualified", because you left the namespace  
out (which was implicit), but yes...you qualified the constant with the  
name of the class in which it's found.  That's how you get all static  
members, including constants.  Which, looks like you've figured out at  
this point.  :)

Pete

Thanks for you help!!
 
Back
Top