How to get/set an enum type?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,
How do I make a property accept only a value from an enum type? Let's say
for example:
using System;
namespace Test {
enum ItemTypes {Raw,Full,Kit}
class Item {
Type itemType = typeof(ItemTypes);
public Item() {}
public int ItemType // I want the allowed value to be of ItemTypes type.
How?
{
get { return itemType; }
set { itemType = value; }
}
}}
 
Is this what you want?

namespace Test {
enum ItemTypes {Raw, Full, Kit}

class Item {
public Item() {}

private ItemType itemType;

public ItemTypes ItemType {
get {
return this.itemType;
}
set {
this.itemType = value;
}
}
}
}

Etienne
 
enum ItemTypes {Raw, Full, Kit}
class Item
{
ItemTypes m_itemType;
public Item {m_itemType = Raw;}
public ItemTypes ItemType
{
get {return m_itemType;}
set {m_itemType = value;}
}
}

/claes
 
This will cause a compile error: Cannot explicitly convert type System.Type
to Test.ItemTypes.
 
Hi Amil,
This will cause a compile error: Cannot explicitly convert type
System.Type
to Test.ItemTypes.

No it won't. You got compiler error saying "The type or namespace name
'ItemType' could not be found (are you missing a using directive or an
assembly reference?)"

and that is because enum's name in the itemType declaration has been
misstyped.

the enum's name is ItemTypes (plural).

So the suggestion is correct, but it has one glitch. I can cast any value of
the underlaying type to this enum

For example I can set the property like:

item.ItemType = (ItemTypes)1000;

And the compiler won't complain because this is correct.
If this is not acceptable, the value needs to be checked in the set method

set
{
if(!Enum.IsDefined(typeof(ItemTypes),value))
{
throw new ArgumentOutOfRangeException("value", "bla bla");
}
this.itemType = value;
}
 
Yes, it was a typo in my post but not inside my code. I created a new class
for testing and it works now. The line of code that causes it to fail is

public enum ItemTypes {Raw,Full,Kit}
public class Test {
Type itemType = typeof(ItemTypes); // cause a compiler error. this is
what I saw in the examples.
ItemTypes iT; // does not throw a compiler error. As posted in the replies.
public Test() {}
}

Stoitcho Goutsev (100) said:
Hi Amil,
This will cause a compile error: Cannot explicitly convert type
System.Type
to Test.ItemTypes.

No it won't. You got compiler error saying "The type or namespace name
'ItemType' could not be found (are you missing a using directive or an
assembly reference?)"

and that is because enum's name in the itemType declaration has been
misstyped.

the enum's name is ItemTypes (plural).

So the suggestion is correct, but it has one glitch. I can cast any value of
the underlaying type to this enum

For example I can set the property like:

item.ItemType = (ItemTypes)1000;

And the compiler won't complain because this is correct.
If this is not acceptable, the value needs to be checked in the set method

set
{
if(!Enum.IsDefined(typeof(ItemTypes),value))
{
throw new ArgumentOutOfRangeException("value", "bla bla");
}
this.itemType = value;
}

--
HTH
Stoitcho Goutsev (100) [C# MVP]


Amil said:
This will cause a compile error: Cannot explicitly convert type
System.Type
to Test.ItemTypes.
 
Back
Top