How to get/set an enum type?

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; }
}
}}
 
E

Etienne Charland

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
 
C

Claes Bergefall

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
 
G

Guest

This will cause a compile error: Cannot explicitly convert type System.Type
to Test.ItemTypes.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

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;
}
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top