Solved

Namespaced constants?

Posted on 2004-08-30
10
211 Views
Last Modified: 2012-05-05
I have a class writtne in Java with a large number of constants that I am currently porting to C#.  I nested the constants in Java so that the code would be a bit more readable:

    public class Class1
   {
        public static final class CONSTANTS
        {
            public static final String CONST1 = "CONST1";
            public static final class NESTED
            {
                public static final String CONST2 = "CONST2";
            }
        }
    }

I can then access the constants using code like:
   
    String strTemp1 = Class1.CONSTANTS.CONST1;
    String strTemp2 = Class1.CONSTNATS.NESTED.CONST2;

How could I do something similar in C#?  I know that C# supports nested classes, but I also notice you cannot declare the class static.  Does this mean every instance will get a separate copy of the class?

I've got only 70 points, and I'm putting them all up.

Thanks,
   Derek
0
Comment
Question by:derekl
  • 3
  • 2
  • 2
  • +1
10 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 11934533
Hi Derek

You can always do:

public class MyClass
{
     public const static string MyString = "My String";
     
     public class MyInnerClass
     {
           public const static string MyOtherString = "My Other String";
     }
}

and you would call them using

MyClass.MyString
and MyClass.MyInnerClass.MyOtherString

Hope this helps

Tincho
0
 

Author Comment

by:derekl
ID: 11935087
Thanks Tincho!  What are the implications of the fact that I cannot declare the inner class Static.  Does this mean every instance of the class I get will contain an instance of the Constants classes?
0
 
LVL 9

Accepted Solution

by:
tinchos earned 35 total points
ID: 11935209
Hi derekl

If you have an inner class as far as I know, every instance of the base class, will contain an instance of the inner class, but dont forget that static members are not part of the instance of the class, but part of the class itself, so if you have 10 instances of MyClass, each of them will contain it's MyInnerClasses, but there will be only one instance of MyOtherString, as it is defined static.

Hope this helps

Tincho
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 35 total points
ID: 11935398
public class MyClass
{
     public const static string MyString = "My String";
     
     public class MyInnerClass
     {
           public const static string MyOtherString = "My Other String";
     }
}


MyClass.MyInnerClass never creates an instance of the first class ... but either way the answer is no, the inner class is bound to the type, not to the instance.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:derekl
ID: 11935545
I think I understand now.  The definition of class MyInnerClass is associated with MyClass.  I won't ever have an instance of MyInnerClass unless I actuall create either from within MyClass or from without as follows:

    MyClass.MyInnerClass = new MyClass.MyInnerClass();
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11935618
right.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 11935644
That's right
0
 

Expert Comment

by:monkeymole
ID: 11939822
.. and a constuctor marked as private will enforce this.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now