2

I'm all new to Enum types and seems like I can't figure something out in my project. I was asked to use Enum for creating a combobox with pre-defined values. What I did was:

namespace Models
{
    public class LastStatus
    {
        public LastStatus()
        {
        }

        public int ID { get; set; }
        public enum description { inProgress, done, accepted }

        public description desc;
        public int constant { get; set; }

    }
}

And this is my method for converting the DB model to my own model before I use them: (e.g add them to a List and then iterate over it to add to the combobox's listitems in aspx.cs side)

private Models.LastStatus ConvertStatusDbToObject(DBModel.myDB.LastStatu status)
        {
            Models.LastStatus statusObject = new Models.LastStatus();
            statusObject.ID = status.ID;
            //statusObject.description = status.description;
            (LastStatus.description)statusObject.desc = (LastStatus.description)status.@const;
            statusObject.constant = status.@const;

            return statusObject;
        }

I get the error on this line:

(LastStatus.description)statusObject.desc = (LastStatus.description)status.@const;

when trying to cast the integer value of the DB Model to my enum type. Isn't desc a property?

Error 23 The left-hand side of an assignment must be a variable, property or indexer

1
  • 2
    object o = new object(); o = (int)1; is OK but this is not object o = new object(); (int)o = (int)1; Commented Jul 23, 2015 at 5:57

2 Answers 2

4

Change (LastStatus.description)statusObject.desc = (LastStatus.description)status.@const; to statusObject.desc = (LastStatus.description)status.@const;

You don't need to cast the property itself!

Sign up to request clarification or add additional context in comments.

1 Comment

Wow I can't believe I didn't realize that. I guess I'm tired after all during this internship :D Thanks for pointing it out!
1

First, assign integer values to your enum values when declaring the enum. for example:

public enum description { inProgress = 0, done = 1, accepted = 2}

Second, I would recommend against having public fields in your class. change

public description desc;

to

public description desc { get; set; };

5 Comments

thanks for the suggestions. enums start from 0 by default,no?
Actually yes, I forgot about that. However the second recommendation is still valid. Check out Sweeper's answer, I think he got it right :-)
Yeah, will accept as an answer as soon as Stackoverflow lets me :)
What do you mean "as soon as Stackoverflow lets me "? You can always do that, can't you?
Well there is a cooldown before you get to accept an answer. Thanks for the fast reply again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.