-3

I'm working on developing a simple game and am using the switch statement quite frequently. Is it possible to reference the specific case in question instead of typing out the constant value? For instance:

switch(String upgrade)
{
       case(Bullet_Power_1):
           remainingUpgrades.Remove("Bullet_Power_1");
           buyableUpgrades[i].setUpgradeId("Bullet_Power_1");
           break;
}

Whenever I write '"Bullet_Power_1"' is there a shortcut to reference the case as I will have to write out the string many times.

  • 10
    I think that upgrade is that string as that is what has matched the case, no? – Erik Eidt Aug 18 '16 at 4:46
  • DoStuff("Bullet_Power_1")? – Philip Kendall Aug 18 '16 at 4:48
  • @ErikEidt At least as written, there's a difference between the constant used to match in the case statement and the literal string used later. But that could just be a typo... – Philip Kendall Aug 18 '16 at 4:51
  • @Philip, I don't understand your point. my point: switch (i) { case 2: ... } inside case 2 ..., i has the value 2, right? And if one chooses to switch (i+1), perfectly legal, one could instead var j=i+1; switch (j)... leaving j to use in each case (e.g. as 2) in this hypothetical, eh? – Erik Eidt Aug 18 '16 at 4:54
  • 2
    nameof might be what you are looking for. (assuming that you are using c# 6) – Caleb Aug 18 '16 at 5:43
4

From your tiny snippet of code, I'm going to make two assumptions:

Assumption 1. Bullet_Power_1 is defined as:

const string Bullet_Power_1 = "Bullet_Power_1";

If this is the case, then your case can just be:

switch(String upgrade)
{
   case(Bullet_Power_1):
       remainingUpgrades.Remove(upgrade);
       buyableUpgrades[i].setUpgradeId(upgrade);
       break;
}

Assumption 2. The rest of your switch is something like:

switch(String upgrade)
{
    case(Bullet_Power_1):
       remainingUpgrades.Remove("Bullet_Power_1");
       buyableUpgrades[i].setUpgradeId("Bullet_Power_1");
       break;
    case(Bullet_Power_2):
       remainingUpgrades.Remove("Bullet_Power_2");
       buyableUpgrades[i].setUpgradeId("Bullet_Power_2");
       break;
    ...
}

In which case, you can get rid of the switch and just do:

remainingUpgrades.Remove(upgrade);
buyableUpgrades[i].setUpgradeId(upgrade);
  • If your first assumption would be true, the problem would be so trivial I guess the OP would not have asked first place - but lets see what he will reply. – Doc Brown Aug 18 '16 at 11:34
  • @DocBrown, you may be right, in which case your answer is good advice. Though you need to change eg string foo=constNames(Bullet_Power_1); to string foo=constNames[Bullet_Power_1]; to make it valid syntax. ;) – David Arno Aug 18 '16 at 11:37
  • 1
    Oops, that happens when you have to switch often between C# and VB ;-) Changed it, thanks. – Doc Brown Aug 18 '16 at 13:57
3

What you are asking for has nothing to do with those constants beeing part of a switch case. If you want to get the name of the constant Bullet_Power_1 as a literal string anywhere in your program when you know only the value, most simple solution I can think of is to create a dictionary to hold the key/value pairs beforehand:

   var constNames = new Dictionary<string,string>()
   {
       {Bullet_Power_1,"Bullet_Power_1"},
       {Bullet_Power_2,"Bullet_Power_2"}
       // ...
   };

(note that I assume your constant Bullet_Power_1 does not already hold the value "Bullet_Power_1", otherwise this would be superfluous, it would be helpful if you please confirm this assumption).

Now, whenever you need to lookup the constant name Bullet_Power_1, you can write

      string foo=constNames[Bullet_Power_1];

or in your case above

       remainingUpgrades.Remove(constNames(upgrade));

In C#6, one can utilize nameof to fill the dictionary:

   var constNames = new Dictionary<string,string>()
   {
       {Bullet_Power_1,nameof(Bullet_Power_1)},
       {Bullet_Power_2,nameof(Bullet_Power_2)}
       // ...
   };

The advantage is that this will avoid an accidental name change of the constant (for example, by using the rename refactoring of Visual Studio), where one might forget to change the string value, too.

-1

Strange that no-one suggested an enumeration. What about

public enum Upgrade
{
    Bullet_Power_1,
    Bullet_Power_2,
    Something_Different_1
}
  • 2
    Well, your current answer does not solve the OPs problem, but if you would suggest that he exchanges his string constants by an enumeration and then show him how to convert an enum name to a string again, then it could become useful. – Doc Brown Aug 19 '16 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.