Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
Following program is not compiling
using System;

namespace enum2
{
    class Program
{
    private const string V = "\n";

    private enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
    static void Main(string[] args)
    {

        Console.Write(value: Days.Sun + V);
        Console.Write(value: Days.Sun + V);
        Days.Sun = (int)  10;//error here
        Console.Write(value: Days.Mon + V);
        Console.ReadKey();
    }
}
}


Error is
left hand-side of an assignment must be variable, property or indexer


What I have tried:

I have tried to cast the value
Posted
Updated 24-May-19 9:46am
v2

Days.Sun, for your purposes, is a "constant"; an enum value; which can only appear on the RIGHT side.
 
Share this answer
 
v2
Comments
[no name] 24-May-19 16:06pm    
Yes a 5
Days.Sun = (int) 10;//error here
Think about what you are trying to do here...

"Days" is the enum and "Sun" is one of the enumeration values... Completly confusing...

Do you like mabye to do something like this?

int day= (int)Days.Sun


Anyway what do you expect from the value '10' from an enum in the range 0..6?

Ok, on the other hand in case you like/need to have a specific relation for your enum you can do something like this

enum Days { Sun= 4, Mon= 5, Tue= 6, Wed= 7, Thu= 8, Fri=9, Sat=10 };
 
Share this answer
 
v2
Comments
zak100 24-May-19 16:36pm    
Hi,
Thanks. I was actually trying to do something similar but out the curly brackets which you are doing inside the curly brackets. This means we can't assign outside the curly brackets?

Zulfi.
[no name] 24-May-19 16:44pm    
Thanks also. This one Days.Sun = (int) 10;you can't do either inside nor outside the curly brackets. The only way you can assign specific value for an enum element is at the time you define the enum. In this case enum Days{Sun= 4, ....
[Edit]
To make it more clear: Once defined like enum Days{Sun= 4, ... you can not change it anymore at runtime.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900