1

Can i use the same variables in several cases without writing them multiple times? e.g.

switch(a)
{
    case 1:
        ans = string.Format("someString {0}, {1}, {2}", var1, var2, var3);
    case 2:
        ans = string.Format("anotherString {0}, {1}, {2}", var1, var2, var3);
    case 3:
        ans = string.Format("thirdString {0}, {1}, {2}", var1, var2, var3);   
 }

can I write the variables var1, var2 and var3 in one place only? thanks.

1
  • Depending on the version, you could use something like a local function. Commented Jul 15, 2018 at 5:52

3 Answers 3

7

So only the format is changing while you want to insert the same values in each?

Then only select the format in your switch and do the string.Format once after it.

string fmt="";
switch(a)
{
    case 1:
        fmt = "someString {0}, {1}, {2}";
    case 2:
        fmt = "anotherString {0}, {1}, {2}";
    case 3:
        fmt = "thirdString {0}, {1}, {2}";   
 }
 ans = string.Format(fmt, var1, var2, var3);
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, this is because the code won't assign value to fmt if the variable a is not 1, 2 or 3, and you cannot use variable that not initialized in C#. So, use default instead of case 3, or declare fmt like string fmt = null.
I have added a default value of an empty string, as I suspect the string.Format will not like a null
This doesn't matter, because it seem that anyway a will be 1, 2 or 3. So you can use default instead of case 3, too.
1

I would wrtie a Dictionary<int,string> let code more clear. then just put once var1,var2,var3

Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "someString");
dict.Add(2, "anotherString");
dict.Add(3, "thirdString");
string result = string.Empty;

if (dict.ContainsKey(a))
    result = string.Format("{0} {1}, {2}, {3}", dict[a], var1, var2, var3);

EDIT

thank for @mjwills suggest

TryGetValue would be faster than ContainsKey because ContainsKey need to do twice search getting the value, but TryGetValue just once

Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "someString");
dict.Add(2, "anotherString");
dict.Add(3, "thirdString");
string ans = string.Empty;
string key = string.Empty;

if (dict.TryGetValue(a, out key))
    ans = string.Format("{0} {1}, {2}, {3}", key, var1, var2, var3);

Comments

0
var temp = string.Format(" {0}, {1}, {2}", var1, var2, var3);

switch (a)
{
    case 1: ans = "someString" + temp; break;
    case 2: ans = "anotherString" + temp; break;
    case 3: ans = "thirdString" + temp; break;
}

Shorter and less readable alternative in Visual Studio 2015 or above:

if ((uint)--a < 3)
    ans = $"{new[] { "someString", "anotherString", "thirdString" }[a]} {var1}, {var2}, {var3}";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.