Return Enum From C# Property getters

前端 未结 7 888
谎友^
谎友^ 2021-01-21 14:35

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

Currently I have this:

    public enum LowerCase
    {
        a,
        b,
    }
    public en         


        
相关标签:
7条回答
  • 2021-01-21 14:42

    It looks like you want:

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

    Or have I missed the point completely?

    0 讨论(0)
  • 2021-01-21 14:42

    Use types... ie..

    
    public Type ChooseCase{
        get{ return typeof(Uppercase); }
    }
    
    
    0 讨论(0)
  • 2021-01-21 14:49

    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".

    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. 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". Now when you define a variable of an enumeration type, you specify the type you want it to be. You have done this:
    private Enum case;
    but what think you mean is:
    private Case case;
    This defines a private variable of type Case called 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.

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

    public Case ChooseCase
    {...
    And where you are trying to return a enumeration value, you should be doing this:
    return Case.Lower

    0 讨论(0)
  • 2021-01-21 14:49

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

    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.

    0 讨论(0)
  • 2021-01-21 14:53

    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 ); 
        }
    }
    
    0 讨论(0)
  • 2021-01-21 15:03

    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.

    0 讨论(0)
提交回复
热议问题