从C#属性获取器返回枚举 [英] Return Enum From C# Property getters

查看:108
本文介绍了从C#属性获取器返回枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从setter/getter函数中返回枚举?

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

目前我有这个:

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

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.

非常感谢.

[最新编辑]

首先感谢所有答复.

为简化此问题,似乎我使用了大写/小写的错误类比,并且你们中的一些人有错误的主意-显然不是您的错:)

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 :)

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

This is the code I have so far and allows you to choose between ChoiceOne and 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;
        }
    }      
    }

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

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

问题是SetSubChoice属性显示为灰色,但取决于SetChoice的设置,它确实设置为"WindowsFormsApplication4.CustomControl1 + ChoiceOne"或"WindowsFormsApplication4.CustomControl1 + ChoiceTwo".我想要的是能够使用SetSubChoice来选择SubChoiceA或SubChoiceB或SubChoiceC或SubChoiceD,具体取决于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.

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

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.

希望这可以使事情变得更加清楚吗?

Hopefully this has clarified things a little more?

我们现在快到了:)不断提出建议.

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

谢谢

推荐答案

看起来像您想要的:

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

还是我完全错过了重点?

Or have I missed the point completely?

这篇关于从C#属性获取器返回枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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