Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

enum MyEnum : int != int ??? 1

Status
Not open for further replies.

MKuiper

Programmer
Jan 29, 2002
364
NL
Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace MyNameSpace
{
    public enum MyEnum : int
    {
        MyEnumValue1 = 1,
        MyEnumValue2 = 2
    }

    public class MyClass
    {
        void MyClassMethod(int SomeInteger)
        {
            // do something
        }

        void CallMyClassMethodWrong()
        {
            // Next line generates two compiler errors
            MyClassMethod(MyEnum.MyEnumValue1);
        }

        void CallMyClassMethodRight()
        {
            // Next line generates no errors
            MyClassMethod((int)MyEnum.MyEnumValue2);
        }
    }
}

the code above generates two errors on the same line (Visual Studio 2005):

Error 2 The best overloaded method match for 'MyNameSpace.MyClass.MyClassMethod(int)' has some invalid arguments
Error 3 Argument '1': cannot convert from 'MyNameSpace.MyEnum' to 'int'

Of course casting takes away the errors, but I wonder why is this? Is an enum : int not considered to be an int?


Marcel
 
As they are type safe, so cannot be implicitely converted. Needs explicit conversion.

Sharing the best from my side...

--Prashant--
 
[tt]MyClassMethod[/tt] is expecting a variable of type [tt]int[/tt] and you're sending it a variable of type [tt]MyEnum[/tt], so yes, the compiler will complain.

Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for the answer, guys. But i'm afraid i still do not understand the issue.
If a method expects a base class as a parameter (like Control), and a derived class is passed in (like Button), casting is not necessary. Why the difference?

Marcel
 
I don't think it makes a lot of sense to cast the enumeration into another type in this case, and I think an enum is already an int anyway.

Below is a small example of how you can use an enumeration:
Code:
public enum UserStatus {Active,Inactive,Expired};

public void ShowMessage(UserStatus status)
{
	switch(status)
	{
		case UserStatus.Active:
		MessageBox.Show("We have an active user");
		break;

		case UserStatus.Inactive:
		MessageBox.Show("We have an inactive user");
		break;

		case UserStatus.Expired:
		MessageBox.Show("This user is expired");
		break

		default:
		break;
	}

}

CLIENT
private void Button_click()
{
	Messages cls = new Messages();
	cls.ShowMessage(Messages.UserStatus.Active);
}

 
If a method expects a base class as a parameter (like Control), and a derived class is passed in (like Button), casting is not necessary. Why the difference?
Don't take this literally, but you can think of it this way: An instance of a class defines some memory space, kind of like this:
[tt]|---------------|------------|
| | |
| properties... | methods... |
| | |
|---------------|------------|[/tt]

An instance of a sub-class adds more memory usage like this:

[tt]|---------------|------------|--------------------|-------------------|
| (base class) | (base cl) | | |
| properties... | methods... | more properties... | more methods.... |
| | | | |
|---------------|------------|--------------------|-------------------|[/tt]

So you can pass a sub-class to a routine expecting the base class because the routine will only be referencing memory in the base class properties and methods. The additional memory will be completely ignored by the routine.

As I said, don't take this literally, but it is a useful mental image for the purpose.

 
From MSDN,
An Enum is a named constant whose underlying type is any integral type except Char. If no underlying type is explicitly declared, Int32 is used.
...
Class Enum is derived from class ValueType; that is, Enum is itself a reference type, not a value type.

Implicit conversions, through polymorphism, is supported intrinsically for reference types (eg. classes), like class to a base-class. To cast a class (ref-type) to value-type is possible only if the class implements the implicit method (see implicit keyword on MSDN).

my 2 cents, [wink]
 
Ah, those last two cents did clear up things. Thanks.

Marcel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top