Proper way to limit possible string argument values and see them in intellisense
arguments, asp.net, c#, function, methods
Solution
In this case instead of using a `String` you should use an `enum`.
enum OutputUnit {
KB,
MB,
GB
}
This will make the choice of input arguments much more explicit for developers. Of course it is not a fool proof operation because a developer can always create an invalid `enum` value by directly casting from `int`
OutputUnit u = (OutputUnit)10000;
Problem
Environment: Visual Studio 2012, .NET 4 Framework, ASP.NET web application (C#) I'd like to know the best, most advisable approach to accomplish limiting incoming arguments (of any type...int, string, etc...) to a predefined set of desired values. I'd like to know the industry-accepted best way. (the following code does not work - it's just to better illustrate the question) Lets say I have a utilities class something like this: ``` public class Utilities { public string ConvertFromBytes(int intNumBytes, string strOutputUnit) { //determine the desired output switch(strOutputUnit.ToUpper()) { case "KB": //Kilobytes - do something break; case "MB": //Megabytes - do something break; case "GB": //Gigabytes - do something break; } //Finish converting and return file size string in desired format... } } ``` Then, in one of my pages, I have something like this: ``` Utilities ut = new Utilities(); strConvertedFilesize = ut.ConvertFromBytes(1024, ``` What I'd like to know is, in the line of code immediately above, what is the best way for me to make it such that "KB", "MB", or "GB" are the only possible string values that can be entered for the "strOutputUnit" parameter? (And) Preferably with intellisense showing the possible available options? Update: JaredPar I'm using your suggestion and came up with the following reworked code: ``` public class Utilities { public enum OutputUnit { KB, MB, GB } public string ConvertFromBytes(int intNumBytes, OutputUnit ou) { //determine the desired output switch (ou) { case OutputUnit.KB: //Kilobytes - do something break; case OutputUnit.MB: //Megabytes - do something break; case OutputUnit.GB: //Gigabytes - do something break; default: //do something crazy break; } //Finish converting and return file size string in desired format... return ""; } } ``` and to call it: ``` Utilities ut = new Utilities(); string strConvertedFilesize = ut.ConvertFromBytes(1024, Utilities.OutputUnit.MB); ``` Is this above approach the most efficient way of using the enums as arguments? In the line directly above, having to type in the "Utilities.OutputUnit." part in my method call feels a little clunky...of course I could always assign shorter names, but are there any ways to better streamline the above? BTW Thanks everyone for the answers. JaredPar i will choose yours since it's correct and came in first. The other answers were very informative and helpful however - thanks to all.