The right way to limit the possible values ​​of string arguments and see them in intellisense

Environment: Visual Studio 2012, .NET 4 Framework, ASP.NET Web Application (C #)

I would like to know the best, most appropriate approach to achieve the limitation of incoming arguments (of any type ... int, string, etc.) to a predefined set of desired values. I would like to know the best way accepted in the industry.

(the following code does not work - it's just to better illustrate the question)
Suppose I have a utility class:

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 on one of my pages I have something like this:

Utilities ut = new Utilities();
strConvertedFilesize = ut.ConvertFromBytes(1024,

, , , , - , "KB", "MB" "GB" strOutputUnit? () , intellisense ?


: JaredPar :

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 "";
        }
}

:

Utilities ut = new Utilities();
string strConvertedFilesize = ut.ConvertFromBytes(1024, Utilities.OutputUnit.MB);

? "Utilities.OutputUnit". ... , , ?

BTW . JaredPar , . , , .

+4
5

String enum.

enum OutputUnit { 
  KB,
  MB,
  GB
}

. , , enum, int

OutputUnit u = (OutputUnit)10000;
+9

, :

public enum SizeUnit { KB, MB, GB }
+2

Enum s, , .

, , , :

, . , , IDE Visual Studio, - , (). , , . , .

+1

, , switch " -", . , , .

.NET Framework. Unit ( , ). {Point, Pixel, Percentage}. , switch. Parse, factory .

0

All Articles