2

We have constants defined in a static class along with functions like

public const string MSG_1 = "New error {0}" 
public const string MSG_2 = "Random error occurred {1}"

public static string Message_ToForm(string MType, string[] vals)

public static GetNewType(string MType)
{
  switch (MType)
    {
      case "MSG_1" :  ...........
    }
}

I require to call it from a program like Message_ToForm("MSG_1", string[]);

How can I convert it to get the value of the constant from string type? Basically, it should return me as "New error {0} when "MSG_1" is passed?

RohanDsouza
  • 412
  • 2
  • 16
  • 1
    If these are (as you've shown) being used to form error messages, you might want to look into storing them as resources. There's quite a bit of support in the framework for storing and retrieving resources (and then you don't really need this class or `Message_ToForm()` since you would just write `string.Format(MyResources.MSG_1,value1,value2)`) – Damien_The_Unbeliever Mar 28 '13 at 07:51
  • @Rohan: Does the suggestions below, solve your issue? – jacob aloysious Mar 28 '13 at 08:09

7 Answers7

1

I'm really confused with your question, but think that's what you're looking for:

public static string GetNewType(string MType)
{
  switch (MType)
    {
      case "MSG_1": return MSG_1;
      case "MSG_2": return MSG_2;
      default: throw new ArgumentException("MType");
    }
}

But I must say - that's really bad approach! You should rethink your design.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
1

I would create a MessageType enumeration and switch based on that.

enum MessageType
{ 
   None = 0,
   Msg1,
   Msg2
}

public static string GetNewType(MessageType MType)
{
    string msg = string.Empty;
    switch (MType)
    {
      case MessageType.Msg1:
         msg = MSG_1;
         break;
      case MessageType.Msg2:
         msg = MSG_2;
         break;
    }
    return msg;
}
d.moncada
  • 16,900
  • 5
  • 53
  • 82
1

You were missing a return type on your method. I believe this is what you want.

static string GetNewType(string MType)
{
   switch (MType)
      {
         case "MSG_1" :
            return MSG_1;
         case "MSG_2":
            return MSG_2;
      }
   return "";
}

But is there are reason your strings are saved as constants in variables? Couldn't you just return the strings in the switch? Like this:

switch (MType)
{
   case "MSG_1" :
      return "New error {0}";
   case "MSG_2":
      return "Random error occurred {1}";
}
Peter Rasmussen
  • 16,474
  • 7
  • 46
  • 63
1

You might need a return type as String for your GetNewType

Suggestion:

If the constants are NOT reused and it if its ONLY for your lookup.

You could use a Dictionary to do the lookup

     Dictionary<string, string> constValues = new Dictionary<string, string>()
                                                         {
                                                             {"MSG_1", "New error {0}"},
                                                             {"MSG_2", "Random error occurred {1}"}
                                                         };


    public string GetNewType(string MType)
    {
        if (constValues.ContainsKey(MType))
            return constValues[MType];

        return string.Empty;
    }
jacob aloysious
  • 2,547
  • 15
  • 16
  • This solution achieved the results. I was looking for a solution where my message is getting constructed dynamically as well as for logging information, I required the constant name and not the actual value associated with the constant. All this was encapsulated within a class. – RohanDsouza Mar 28 '13 at 09:12
0

Create a static readonly ReadOnlyDictionary property populated with your 'constants':

private static readonly System.Collections.ObjectModel.ReadOnlyDictionary<string, string> _msgDictionary =
    new System.Collections.ObjectModel.ReadOnlyDictionary<string, string>(
        new Dictionary<string, string>()
        {
            { "MSG_1", "New error {0}" },
            { "MSG_2", "Random error occurred {1}" }
        });

public static System.Collections.ObjectModel.ReadOnlyDictionary<string, string> Messages
{
    get { return _msgDictionary; }
}

Then usage:

var msg = Messages["MSG_1"];
Moho
  • 15,457
  • 1
  • 30
  • 31
0
public static GetNewType(string MType)
{
string MType = yourClass.MSG_1
  switch (MType)
    {
      case yourClass.MSG_1
        break;
          ....
        default:
       // Code you might want to run in case you are
       // given a value that doesn't match.
       break;
    }
}
Zero-dev
  • 340
  • 6
  • 17
0

I strongly recommend using the out of the box .NET localization feature. You simply add a resource file to your project and put all your string messages into it, the resource file is like a key value pair of string resources, the IDE automatically creates a property for each string message which you can use in your code.

Check this How to use localization in C# for more details.

Community
  • 1
  • 1
Siraf
  • 1,133
  • 9
  • 24