简体   繁体   English

我怎样才能使我的 class 变量只能设置为三个选项之一?

[英]How can I make it so my class variables can only be set to one of three choices?

I have a class like this:我有一个像这样的 class :

public class Meta
{
    public string Height { get; set; }
}

I would like to add some things to the class but I'm not sure how to do it.我想在 class 中添加一些东西,但我不知道该怎么做。 What I would like is for the Height to only be able to be set to either "Tall" or "Short".我想要的是高度只能设置为“高”或“短”。 Maybe more things in the future but for now it would be a choice just between those two.也许将来会有更多的事情,但现在只能在这两者之间做出选择。 Also I would like it to default to "Short" in the constructor.我也希望它在构造函数中默认为“Short”。 I think I would need to use an Enums but I am not sure how to do this.我想我需要使用枚举,但我不知道该怎么做。

Could someone explain.有人可以解释一下。 I would very much appreciate.我将非常感激。

Yes, you could use an enum:是的,您可以使用枚举:

public enum Height
{
    Short = 0,
    Tall = 1;
}

public class Meta
{
    public Height Height { get; private set; }

    public Meta(Height height)
    {
        if (!Enum.IsDefined(typeof(Height), height))
        {
            throw new ArgumentOutOfRangeException("No such height");
        }
        this.Height = height;
    }
}

(If you want the property to be writable, you need to put the validation in the setter.) (如果您希望属性可写,则需要将验证放入 setter。)

You need the validation because enums are really just differently-typed integer values.您需要验证,因为枚举实际上只是不同类型的 integer 值。 For example, without the validation this would proceed fine:例如,如果没有验证,这将正常进行:

new Meta((Height) 1000);

but it would obviously be meaningless to any callers.但这对任何来电者来说显然毫无意义。

You could define an enum with the possible values:您可以使用可能的值定义一个枚举

public enum HeightTypes
{
    Tall,
    Short
}

and then use this as the type of the Height property:然后将其用作Height属性的类型:

public class Meta
{
    public Meta()
    {
        // Set the Height property to Short by default in the constructor
        Height = HeightTypes.Short;
    }
    public HeightTypes Height { get; set; }
}

Now when you have an instance of the Meta class you could set its Height property only to Tall or Short:现在,当您拥有 Meta class 的实例时,您可以仅将其 Height 属性设置为 Tall 或 Short:

var meta = new Meta();
meta.Height = HeightTypes.Tall;

Define an Enum.定义一个枚举。

public Enum Heights
{
    Tall,
    Short
}

and then define your property to be of type enum然后将您的属性定义为枚举类型

public Heights Height { get; set; }

see http://msdn.microsoft.com/en-us/library/system.enum.aspx for more info有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/system.enum.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何做到这一点,使我的字段只设置一​​次? - How can I make it so my field is only set the one time? Unity C#-如何做到这一点,以便可以通过一个类访问所有其他类,变量和方法? - Unity c# - How can I make it so that I can access all my other classes, variables and methods via a single class? 我怎样才能让我的业务逻辑只执行一次,所以它只改变 object 一次 - How can i make my business logic only execute once, so it alters the object only once 我该怎么办才能选择一个复选框? - How can I do so only one checkbox can be selected? 我如何制作一个递归 function,它只使用加法和 2 个变量进行除法 - How can I make one recursive function which does division using only addition and only 2 variables 我怎样才能使 controller 中的 selectList 仅返回一个要显示的选项? - how can i make it so the selectList in the controller returns only one choice to be displayed? 我怎样才能让我的用户输入只接受特定的符号? - How can I make it so that my user input only accepts specific symbols? 如何使我的 MAUI 应用程序的 window 固定,因此无法调整大小或设置最小高度/宽度? - How can I make the window of my MAUI app fixed, so not resizable or set a minimum height/width? 如何编辑序列,使其仅使用LINQ返回一个元素 - How can I edit my sequence so it returns one element only using LINQ 如何使我的 Form Control 变量可以访问我的 Form 类以外的类? - How can I make my Form Control variables acessible to classes other than my Form class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM