限制可能的字符串参数值并在智能感知中查看它们的正确方法 [英] Proper way to limit possible string argument values and see them in intellisense

查看:69
本文介绍了限制可能的字符串参数值并在智能感知中查看它们的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:Visual Studio 2012,.NET 4框架,ASP.NET Web应用程序(C#)

Environment: Visual Studio 2012, .NET 4 Framework, ASP.NET web application (C#)

我想知道最好的,最可取的方法来完成将传入参数(任何类型... int,字符串等...)限制为一组预定义的所需值.我想知道业界公认的最佳方法.

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,

我想知道的是,在上面的代码行中,最理想的方式是使"KB","MB"或"GB"成为唯一的字符串值可以为"strOutputUnit"参数输入的内容? (并且)最好是intellisense显示可能的可用选项?

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?

更新:JaredPar我正在使用您的建议,并提出了以下重新编写的代码:

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

并命名为:

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

以上方法是使用枚举作为参数的最有效方法吗?在正上方的行中,必须输入"Utilities.OutputUnit".我的方法调用的一部分感觉有些笨拙……当然,我总是可以分配较短的名称,但是有什么方法可以更好地简化上面的代码?

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?

顺便说一句,谢谢大家的回答. JaredPar我会选择您的,因为它是正确的,并且是第一名.其他答案非常很有帮助,但很有帮助-谢谢大家.

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.

推荐答案

在这种情况下,应该使用enum而不是使用String.

In this case instead of using a String you should use an enum.

enum OutputUnit { 
  KB,
  MB,
  GB
}

这将使开发人员对输入参数的选择更加明确.当然,这不是一个简单的操作,因为开发人员总是可以通过直接从int

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;

这篇关于限制可能的字符串参数值并在智能感知中查看它们的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆