views:

601

answers:

7

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'

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

    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.

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.

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.

Thanks

+3  A: 

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 ); 
    }
}
Paul Alexander
+10  A: 

It looks like you want:

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

Or have I missed the point completely?

Marc Gravell
For me it looks like he wants to return some kind of "baseclass" enum. He thinks that LowerCase and UpperCase are both derived from Enum. Like using polymorphism on enums.
tanascius
But LowerCase and UpperCase _are_ both derived from Enum.
Eric Lippert
Yes, I explained it very badly. He can do even "return new LowerCase();", but it doesn't help in the way he wants. I think he would like to do "var enum = ChooseCase;" and thinks that "enum." shows with intellisense A/B or a/b depending on the case. That is not working.
tanascius
Maybe we should explain to people that enums are really just pretty forms of integer values.
Matthew Whited
A: 

Use types... ie..


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

apandit
A: 

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.

Mark
+2  A: 

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.

micahtan
+1  A: 

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

Simon P Stevens
A: 

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

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.

Dmitry Risenberg