Solved

Namespaced constants?

Posted on 2004-08-30
10
210 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

19 Experts available now in Live!

Get 1:1 Help Now