Click to See Complete Forum and Search --> : Overloading '=', getting errors


c#_novice
July 18th, 2008, 08:59 AM
Anything wrong with this overloading

TankStatus operator=(int number, TankStatus ts)
{
return ts = number;
}

//: error C2801: 'operator =' must be a non-static member

TankStatus is enum type....

GCDEF
July 18th, 2008, 09:08 AM
You can only overload operators on classes, structs and unions. MSDN explains that if you look up the error number. Why would you want to overload the = operator on an enum anyway?

c#_novice
July 18th, 2008, 09:15 AM
You can only overload operators on classes, structs and unions. MSDN explains that if you look up the error number. Why would you want to overload the = operator on an enum anyway?

because...

if ( (ts>3 ) || (ts<0) ) {
ts=0 ;
}

// : error C2440: '=' : cannot convert from 'int' to TankStatus'


again, ts is enum.... // enum TankStatus {unknown, empty, half, full};

GCDEF
July 18th, 2008, 09:20 AM
The point of an enum is to provide names for its possible values. You should use those names when assigning it a value, otherwise there's no point in using an enum.

enum TankStatus
{
unknown,
empty,
full
}:

TanksStatus ts;

ts = unknown;

c#_novice
July 21st, 2008, 02:08 AM
The point of an enum is to provide names for its possible values. You should use those names when assigning it a value, otherwise there's no point in using an enum.

enum TankStatus
{
unknown,
empty,
full
}:

TanksStatus ts;

ts = unknown;

hey you right, thanx :)

s_kannan55
July 30th, 2008, 06:52 AM
elements of an enum can only be assigned values at the point of declaration.

kempofighter
July 30th, 2008, 10:25 AM
elements of an enum can only be assigned values at the point of declaration.

If you define a variable that happens to be an enum (and it is not a const) you can assign to it as much as you want.

enum colors
{
RED,
BLUE,
GREEN
}

colors textColor (RED);

// later I can do this
textColor = BLUE;

Am I misunderstanding your point?