Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Namespaced constants?

Posted on 2004-08-30
10
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

719 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