Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Namespaced constants?

Posted on 2004-08-30
10
Medium Priority
?
219 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
8 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 140 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 140 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
 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline

876 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