从 C# 属性 getter 返回枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1015016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:35:30  来源:igfitidea点击:

Return Enum From C# Property getters

c#visual-studio

提问by cosmarchy

How do you return an Enum from a setter/getter function?

你如何从 setter/getter 函数返回一个枚举?

Currently I have this:

目前我有这个:

    public enum LowerCase
    {
        a,
        b,
    }
    public enum UpperCase
    {
        A,
        B,
    }

    public enum Case
    {
        Upper,
        Lower,
    }

    private Enum _Case;
    private Enum _ChosenCase;


    public Enum ChooseCase
    {
        get
        {
            if ('Condition')
            {
                return LowerCase; //[need to return LowerCase]
            }
            else
            {
                return UpperCase; //[need to return UpperCase]
            }
        }
        set
        {
            _ChosenCase = value; 
        }
    }

When I try to run this I get an error:

当我尝试运行它时,我收到一个错误:

LowerCase is a 'type' but is used like a 'variable'

小写是一种“类型”,但像“变量”一样使用

Any ideas what I need to do to return an enum????

任何想法我需要做什么才能返回枚举????

I am also not so sure about setting the value at the moment either.

我现在也不太确定设置该值。

If anyone can give some general advise, I would appreciate it; most people should be able to see what I am trying to do.

如果有人可以提供一些一般性建议,我将不胜感激;大多数人应该能够看到我正在尝试做什么。

Thanks very much.

非常感谢。

[Latest Edit]

[最新编辑]

Firstly thanks to all who replied.

首先感谢所有回复的人。

In an attempt to simplify this question, it appears I have used the wrong analogy of upper/lower case and some of you have got the wrong idea - clearly not your fault :)

为了简化这个问题,看来我使用了错误的大写/小写类比,你们中的一些人有错误的想法 - 显然不是你的错:)

This is the code I have so far and allows you to choose between ChoiceOne and ChoiceTwo

这是我到目前为止的代码,允许您在 ChoiceOne 和 ChoiceTwo 之间进行选择

    public partial class CustomControl1 : Control
    {
    public enum ChoiceOne
    {
        SubChoiceA,
        SubChoiceB,
    }
    public enum ChoiceTwo
    {
        SubChoiceC,
        SubChoiceD,
    }
    public enum Choice
    {
        ChoiceOne,
        ChoiceTwo,
    }

    private Type _subChoice;
    private Choice _choice;

    public Type SetSubChoice
    {
        get
        {
            if (_choice.Equals(Choice.ChoiceOne))
            {
                return typeof(ChoiceOne); 
            }
            else
            {
                return typeof(ChoiceTwo);
            }
        }
        set
        {
            _subChoice = value; 
        }
    }

    public Choice SetChoice
    {
        get
        {
            return _choice;
        }
        set
        {
            _choice = value;
        }
    }      
    }

What happsens in VisualStudio is that property grid allows you to set the SetChoice property between ChoiceOne and ChoiceTwo which is correct.

VisualStudio 中发生的事情是属性网格允许您在正确的 ChoiceOne 和 ChoiceTwo 之间设置 SetChoice 属性。

The problem is that the SetSubChoice property is greyed out but it does set to either 'WindowsFormsApplication4.CustomControl1+ChoiceOne' or 'WindowsFormsApplication4.CustomControl1+ChoiceTwo' dependent upon what SetChoice is set to. What I want is to be able to use SetSubChoice to pick SubChoiceA or SubChoiceB or SubChoiceC or SubChoiceD depending on what SetChoice is set to.

问题是 SetSubChoice 属性是灰色的,但它确实设置为“WindowsFormsApplication4.CustomControl1+ChoiceOne”或“WindowsFormsApplication4.CustomControl1+ChoiceTwo”,具体取决于 SetChoice 的设置。我想要的是能够使用 SetSubChoice 来选择 SubChoiceA 或 SubChoiceB 或 SubChoiceC 或 SubChoiceD,具体取决于 SetChoice 的设置。

So for example, if SetChoice is set to ChoiceOne then SetSubChoice will allow me to choose between ChoiceA or ChoiceB. Likewise if SetChoice is set to ChoiceTwo then SetSubChoice will allow me to choose between ChoiceC or ChoiceD.

例如,如果 SetChoice 设置为 ChoiceOne,则 SetSubChoice 将允许我在 ChoiceA 或 ChoiceB 之间进行选择。同样,如果 SetChoice 设置为 ChoiceTwo,则 SetSubChoice 将允许我在 ChoiceC 或 ChoiceD 之间进行选择。

Hopefully this has clarified things a little more?

希望这已经澄清了一些事情?

We are almost there now :) keep the ideas coming.

我们现在差不多了:) 让想法不断涌现。

Thanks

谢谢

回答by Paul Alexander

The error is correct. It's basically the same as doing this

错误是正确的。基本上和做这个一样

public int ChooseCase
{
    get{ return int; }
}

What you'll want to do is use real classes - not enums - with a common base.

您要做的是使用具有共同基础的真实类 - 而不是枚举。

public abstract class Case
{
    public abstract string ModifyString(string value);
}

public class UpperCase : Case
{
    public override string ModifyString( string value )
    { 
        return String.ToUpper( value ); 
    }
}

回答by Marc Gravell

It looks like you want:

看起来你想要:

public Case ChooseCase
{
    get { return 'Condition' ? Case.Lower : Case.Upper; }
}

Or have I missed the point completely?

还是我完全没有抓住重点?

回答by apandit

Use types... ie..

使用类型...即...


public Type ChooseCase{
    get{ return typeof(Uppercase); }
}

回答by Mark

That error is being thrown because LowerCase is a type. Imagine if you were trying to write this statement.

抛出该错误是因为 LowerCase 是一种类型。想象一下,如果您正在尝试编写此语句。

return Int32;

That would not work because Int32 is a type and not a variable. You will need to create an instance of LowerCase and then you can return it.

这是行不通的,因为 Int32 是一种类型而不是变量。您将需要创建一个 LowerCase 实例,然后您可以返回它。

回答by micahtan

You're mixing two enumerations together which is not a good idea since it defeats the purpose of the enum type.

您将两个枚举混合在一起,这不是一个好主意,因为它违背了枚举类型的目的。

Definitely do notcast to int and do comparisons that way -- you lose the ability to change the order of your enumerated values in future versions.

当然也不能转换为int和做比较-你们失去改变在未来的版本的枚举值的顺序的能力。

回答by Simon P Stevens

I think you may have misunderstood the reasons for using enumerations. An enumeration is a special type of variable that can only take a specific set of values. For example. a boolean is a very special case of enumeration. A boolean variable is one that can only take 2 values - Either "true" or "false".

我想你可能误解了使用枚举的原因。枚举是一种特殊类型的变量,它只能采用一组特定的值。例如。布尔值是枚举的一个非常特殊的情况。布尔变量是一个只能取 2 个值的变量——“真”或“假”。

You have defined an enumeration:

您已经定义了一个枚举:

public enum LowerCase
    {
        a,
        b,
    }

This means that a variable of type LowerCasewill only be able to be set to either a, or b. This isn't really much use.

这意味着类型变量LowerCase只能设置为 a 或 b。这其实用处不大。

Your other enumeration is more sensible:

您的其他枚举更明智:

public enum Case
    {
        Upper,
        Lower,
    }
Case 类型的变量可以设置为“Upper”或“Lower”。现在,当您定义枚举类型的变量时,您可以指定所需的类型。你已经这样做了:
private Enum case;
但你认为你的意思是:
private Case case;
这定义了一个CaseCase名为的私有变量casecase。enum 关键字仅在定义新枚举时使用,而不是在定义枚举类型的变量时使用。

Similarly, where you have defined your property, you should use the enum type that you want to return:

同样,在定义属性的地方,应该使用要返回的枚举类型:

public Case ChooseCase
{...
在您尝试返回枚举值的地方,您应该这样做:
return Case.Lower

回答by Dmitry Risenberg

Probably you should use a base class Case and two derived classes, so that your ChooseCase property returns an object of the corresponding class:

可能您应该使用一个基类 Case 和两个派生类,以便您的 ChooseCase 属性返回相应类的对象:

abstract class Case
{
    abstract char GetItem(int index);
}

class LowerCase : Case
{
    override char GetItem(int index)
    {
        // Not sure for the following
        Encoding ascii = Encoding.ASCII;
        return ascii.GetString(ascii.GetBytes("a")[0] + index)[0];
    }
}

class UpperCase : Case
{
    // The same for the upper case
}

public Case ChooseCase
{
    get
    {
        if (condition)
        {
            return new LowerCase();
        }
        else
        {
            return new UpperCase();
        }
    }
}

This is a thing you cannot do with enums, because they all derive exactly from Enum.

这是你不能用枚举做的事情,因为它们都完全来自枚举。