Reputation: 412
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?
Upvotes: 2
Views: 536
Reputation: 16942
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}";
}
Upvotes: 1
Reputation: 1300
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.
Upvotes: 0
Reputation: 2597
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;
}
Upvotes: 1
Reputation: 340
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;
}
}
Upvotes: 0
Reputation: 16563
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"];
Upvotes: 0
Reputation: 17402
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;
}
Upvotes: 1
Reputation: 125650
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.
Upvotes: 1