views:

214

answers:

6

I have a switch statement in one class that generates Command objects, based on if a certain line in a code file matches the particular code for that Command object.

like this:

switch (line):
{
   case "#CODE1":
      return new Command1();
   case "#CODE2":
      return new Command2();
}

I want to turn it into something like this:

switch (line):
{
   case Command1.Code:
      return new Command1();
   case Command2.Code:
      return new Command2();
}

I tried it by creating a public static property of Command1, but the compiler complains...

Is there a way to do something along this intent, that the compiler won't complain about?

+6  A: 

I think you can do this only with constants... so if it's possible for you, use constant fields instead of public static properties. The difference is that it's treated like a literal at compile time, so if the value changes, you would need to recompile all assemblies that reference the constant.

Botz3000
+2  A: 

Switch only works on numeric types and string types, not properties. You can try defining a public string constant on your Command1 and Command2 class, which you should be able to switch on instead.

public class Command1 
{
   public string const Code = "#CODE1";
}
Scott Dorman
+1  A: 

since you are working with strings I'm not sure if it's the best approach, but using enums works for me

enum CommandEnum { CommandOne, ComandTwo, CommandN }
//...

CommandEnum myCommand;
//...
switch (myCommand)
{
  case myCommand.CommandOne:
       return new Command1();
  case myCommand.CommandTwo:
       return new Command2();
  //...
}
nairdaen
A: 

I think your problem is you need to instantiate your objects before you can determine their property values.

I'm assuming you have a code file with tokenized instructions, like:

#CODE1
#CODE2

and you want a class to read each line of your file and then perform whichever instructions the tokens define.

You got it to work, but instead of doing a direct string comparison to the token in the file, you want the value of the token stored in a property of your objects? The problem with that approach is you have to create a new instance of your object (commands 1 and 2) before you can refer to their property values.

If I understand what you've done and what you want to do, I think your solution was fine the way it was. I'm not 100% sure I really understand what you want to do, though, or why.

HTH

Beth
I don't like having the string constants needing to be "memorized". I want them attached to their respective Commands. There is no possibility of typos this way. Intellisense will catch them.
Alex Baranosky
True, but you can still have typos in your code file. The point is to do what you want, you have to instantiate the variable first. You can't create the new instance after checking the property value, you have to create the instance before you can check for your value.
Beth
After reading your comment below, I'm not sure why the compiler would complain about a switch statement and not if/then/else. I thought it was complaining about your object reference, although that wasn't clear. If you got it to work with the if/then/else structure, and you changed it back to switch, are you still getting errors from the compiler??
Beth
A: 

If you want to check against properties on a class. I'm assuming that you probably want to be able to change those properties and check accordingly. You'll have to use an if, else if, else if type statement.

If you've got a jones to use a switch statement, you're SOL unless you're checking an integral type against constant values. if, else if, else if blocks are ugly, but it'll do what you're looking for.

blesh
A: 

You could always fall back on the tried and true if / else block

 if (line == Command1.Code)
  return new Command1();
 else if (line == Command2.Code)
  return new Command2();

That's not so evil, is it?

Scott P
Nope, not so evil...... I had to do this :) I went with a static readonly variable named 'Code'
Alex Baranosky