views:

79

answers:

4

So I have string returnType which a developer can choose to = to JSON , XML or PRINT_R. How to limit his choice and make VS 2008 or later suggest him what value (from that 3) string returnType can be? (.net 3.5)

+3  A: 

You have to use an enum instead of a string. Using strings for something like that is extremely ugly anyway - it will even require a runtime string comparison instead of a much cheaper integer or even bit comparison when using an enum (which is a number).

ThiefMaster
COULD YOU give some code example, please?
Blender
I would recommend it too
Junior Mayhé
public enum ReturnType { JSON=0, XML=1, PRINTR=2 }
Junior Mayhé
A: 

You can do a switch statement on the string. Then use a default for all invalid cases.

unholysampler
+2  A: 

If I understand the question right, enum is the keyword you're looking for. So, you would declare a data type which represents the available return types:

public enum DataFormatType { Json, Xml, PrintR }

and then in your function parameters, change string returnType to DataFormatType returnType. This will allow visual studio to suggest the values (this is called Code Completion or Intellisense) and the only valid values are those supplied in the enum.

Cheers, Alex

AlexC
+2  A: 
public enum DataFormat{ JSON=0, XML=1, PRINTR=2 } 

public ReturnType SomeFunction( DataFormat format )
{
    if( DataFormat.JSON == format ) 
        return ....
    //etc
}
BioBuckyBall
The =0, =1, and =2 are not necessary, they only need to be entered if you have specific need to cast your enum to something like a int. see http://msdn.microsoft.com/en-us/library/sbbt4032.aspx for a example
Scott Chamberlain
You are correct of course, I borrowed the enum definition from @juniorMayhe's comment to @ThiefMaster so I didn't have to type it out myself :)
BioBuckyBall