Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to limit possible string argument values and see them in intellisense

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.

like image 327
user3070890 Avatar asked Dec 05 '13 18:12

user3070890


People also ask

How do I get parameter information in IntelliSense?

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.

Why can't I use IntelliSense with a comment in a file?

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:

How do I Turn Off IntelliSense in VS Code?

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.

What is IntelliSense and how do I use it?

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.


3 Answers

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;
like image 96
JaredPar Avatar answered Sep 21 '22 00:09

JaredPar


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:

  • Data Annotations. Attribute-based validation.
  • 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.

like image 27
Nicholas Carey Avatar answered Sep 19 '22 00:09

Nicholas Carey


This is exactly what enums are for:

public enum SizeUnit { KB, MB, GB }
like image 29
SLaks Avatar answered Sep 21 '22 00:09

SLaks