Solved

Namespaced constants?

Posted on 2004-08-30
10
217 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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

636 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