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.
For more information, see Supply XML code comments. You can manually invoke Parameter Info by choosing Edit > IntelliSense > Parameter Info, by pressing Ctrl + Shift + Space, or by choosing the Parameter Info button on the editor toolbar. Quick Info displays the complete declaration for any identifier in your code.
You can't use IntelliSense if the cursor is in a comment in your source file. The cursor is in a string literal. You can't use IntelliSense if the cursor is in the quotation marks around a string literal, as in the following example:
If you prefer, you can turn off IntelliSense while you type. See Customizing IntelliSense below to learn how to disable or customize VS Code's IntelliSense features. As provided by the language service, you can see quick info for each method by either pressing ⌃Space (Windows, Linux Ctrl+Space) or clicking the info icon.
Thank you. IntelliSense is a code-completion aid that includes a number of features: List Members, Parameter Info, Quick Info, and Complete Word. These features help you to learn more about the code you're using, keep track of the parameters you're typing, and add calls to properties and methods with only a few keystrokes.
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;
In addition to using an enum
in your specific case, from a more general perspectice, you might take a look at (by no means inclusive -- there's more than one way to do it) these:
Microsoft Research's Design by Contract extensions for C#: https://stackoverflow.com/a/267671/467473.
You can install the Visual Studio extension at http://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f-1ce455f66970
Here's an article by Jon Skeet on code contracts: http://www.infoq.com/articles/code-contracts-csharp.
The ur-text for design-by-contract — and arguably the best overall book on O-O design — is Bertrand Meyer's Object-Oriented Software Construction, 2nd ed.. The Eiffel language has design-by-contract at its core: Eiffel/Eiffel Studio is a full-fledged Eiffel IDE that produces CLR-compliant assemblies.
This is exactly what enums are for:
public enum SizeUnit { KB, MB, GB }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With