简体   繁体   English

从C#属性获取器返回枚举

[英]Return Enum From C# Property getters

How do you return an Enum from a setter/getter function? 如何从设置器/获取器函数返回枚举?

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' LowerCase是“类型”,但像“变量”一样使用

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. 问题是SetSubChoice属性显示为灰色,但取决于SetChoice的设置,它确实设置为“ WindowsFormsApplication4.CustomControl1 + ChoiceOne”或“ WindowsFormsApplication4.CustomControl1 + ChoiceTwo”。 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选择SetChoice设置为SubChoiceA或SubChoiceB或SubChoiceC或SubChoiceD。

So for example, if SetChoice is set to ChoiceOne then SetSubChoice will allow me to choose between ChoiceA or ChoiceB. 因此,例如,如果将SetChoice设置为ChoiceOne,则SetSubChoice将允许我在ChoiceA或ChoiceB之间进行选择。 Likewise if SetChoice is set to ChoiceTwo then SetSubChoice will allow me to choose between ChoiceC or ChoiceD. 同样,如果将SetChoice设置为ChoiceTwo,则SetSubChoice将允许我在ChoiceC或ChoiceD之间进行选择。

Hopefully this has clarified things a little more? 希望这可以使事情澄清一些?

We are almost there now :) keep the ideas coming. 我们现在就快到了:)不断提出建议。

Thanks 谢谢

It looks like you want: 看起来像您想要的:

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

Or have I missed the point completely? 还是我完全错过了重点?

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

You're mixing two enumerations together which is not a good idea since it defeats the purpose of the enum type. 您将两个枚举混合在一起,这不是一个好主意,因为它违反了枚举类型的目的。

Definitely do not cast to int and do comparisons that way -- you lose the ability to change the order of your enumerated values in future versions. 绝对强制转换为int和做比较-你们失去改变在未来的版本的枚举值的顺序的能力。

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个值的变量-“ true”或“ false”。

You have defined an enumeration: 您已经定义了一个枚举:

public enum LowerCase
    {
        a,
        b,
    }

This means that a variable of type LowerCase will only be able to be set to either a, or b. 这意味着LowerCase类型的变量只能设置为a或b。 This isn't really much use. 这并不是真正的用处。

Your other enumeration is more sensible: 您的其他枚举更明智:

public enum Case
    {
        Upper,
        Lower,
    }
A variable of type Case can be set to either "Upper" or "Lower". 可以将Case类型的变量设置为“ Upper”或“ Lower”。 Now when you define a variable of an enumeration type, you specify the type you want it to be. 现在,当您定义枚举类型的变量时,您可以指定想要的类型。 You have done this: 您已经这样做:
public Case ChooseCase
{...
but what think you mean is: 但是您的意思是:
 private Case case; 私人案件; 
This defines a private variable of type Case called case . 这定义了一个Case类型的私有变量,称为case The enum keyword is only used when you are defining a new enumeration, not when you are defining a variable of an enumeration type. enum关键字仅在定义新的枚举时使用,而不在定义枚举类型的变量时使用。

Similarly, where you have defined your property, you should use the enum type that you want to return: 同样,在定义属性的位置,应使用要返回的枚举类型:

 public Case ChooseCase 公共案例选择案例\n{... {... 
And where you are trying to return a enumeration value, you should be doing this: 在尝试返回枚举值的地方,您应该这样做:
 return Case.Lower 返回Case.Lower 

Use types... ie.. 使用类型...即


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

That error is being thrown because LowerCase is a type. 由于LowerCase是一种类型,因此将引发该错误。 Imagine if you were trying to write this statement. 想象一下,如果您试图编写此语句。

return Int32;

That would not work because Int32 is a type and not a variable. 这将不起作用,因为Int32是类型而不是变量。 You will need to create an instance of LowerCase and then you can return it. 您将需要创建LowerCase的实例,然后可以将其返回。

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. 这是您无法用枚举完成的事情,因为它们都完全源自枚举。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM