none
string enum inside a class RRS feed

  • Question

  • hi

    i want to define a public read only list of named strings inside a class, something like this;

    public enum MyEnum
        {
            string str1 = "str1-value",
            string str2 = "str12-value",
            
            // ...
        }
    so i can read any value after i created an instance of the parent class


    • Edited by essamce Tuesday, December 17, 2019 5:03 AM
    Tuesday, December 17, 2019 4:31 AM

Answers

  • Hello essamce,

    See if the following sample code helps to achieve your objective :

        public class myParentClassName
        {
            public class MyStrings
            {
                public const string str1 = "value1";
                public const string str2 = "value2";
            }
        }
    
        public class MyClass
        {
            public void DoTest()
            {
                string test = myParentClassName.MyStrings.str1;
    
                Console.WriteLine(test);
            }
        }
    

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    Tuesday, December 17, 2019 5:09 AM
  • Something like this?

    namespace Test
    {
       class Program
       {
          static void Main()
          {
             Console.WriteLine(MyClass.MyStrings.StringName1);
             Console.WriteLine(MyClass.MyStrings.StringName2);
             Console.WriteLine(MyClass.MyStrings.StringName3);
          }
       }
    
       // Note MyClass does not have to be static, but the strings can be static.
       public class MyClass
       {
          public static StringDefinitions MyStrings = new StringDefinitions();
       }
    
       public class StringDefinitions
       {
          public string StringName1 { get; } = "Banana";
          public string StringName2 { get; } = "Apple";
          public string StringName3 { get; } = "Orange";
       }
    }
    Edit : A simpler way would be to make StringDefinitions static, but I'm not sure if that is what you want.


    Tuesday, December 17, 2019 5:26 AM

All replies

  • Greetings Essamce.

    I'm not sure what you mean. Do you mean something like a dictionary, where the key is "str1" and the value is "value1"?

    Can you show us some code that shows how you want to use the list?

    Tuesday, December 17, 2019 4:37 AM
  • Hello essamce,

    1. It is not possible to define strings in an emum.

    2. But you can define constant strings in a class which are publicly accessible by other classes, e.g. :

    public class MyStrings
        {
            public const string str1 = "value1",
            public const string str2 = "value2",
            
            // ...
        }

    Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    Tuesday, December 17, 2019 4:52 AM
  • hi Bio

    the problem whith the class (MyStrings) it needs to create an instance, and im using a non-static parent class,


    • Edited by essamce Tuesday, December 17, 2019 4:59 AM
    Tuesday, December 17, 2019 4:58 AM
  • hi MiTek

    i need to access my strings like so:

    var x = myParentClassName.MyStrings.StringName1;

    Tuesday, December 17, 2019 5:03 AM
  • Hello essamce,

    See if the following sample code helps to achieve your objective :

        public class myParentClassName
        {
            public class MyStrings
            {
                public const string str1 = "value1";
                public const string str2 = "value2";
            }
        }
    
        public class MyClass
        {
            public void DoTest()
            {
                string test = myParentClassName.MyStrings.str1;
    
                Console.WriteLine(test);
            }
        }
    

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    Tuesday, December 17, 2019 5:09 AM
  • Something like this?

    namespace Test
    {
       class Program
       {
          static void Main()
          {
             Console.WriteLine(MyClass.MyStrings.StringName1);
             Console.WriteLine(MyClass.MyStrings.StringName2);
             Console.WriteLine(MyClass.MyStrings.StringName3);
          }
       }
    
       // Note MyClass does not have to be static, but the strings can be static.
       public class MyClass
       {
          public static StringDefinitions MyStrings = new StringDefinitions();
       }
    
       public class StringDefinitions
       {
          public string StringName1 { get; } = "Banana";
          public string StringName2 { get; } = "Apple";
          public string StringName3 { get; } = "Orange";
       }
    }
    Edit : A simpler way would be to make StringDefinitions static, but I'm not sure if that is what you want.


    Tuesday, December 17, 2019 5:26 AM