Home All Groups Group Topic Archive Search About

converting a string to it's enum (integer) equivalent

Author
22 May 2006 7:18 AM
Richy
Hi,

I have a class that exposes the Microsoft.DirectX.Direct3D.Compare
property, which allows a drop-down list of values (such as Never,
Always etc) to be selected via the propertygrid. I write the string
value ("Never", "Always" etc) to an XML file, and when I read back the
XML file and set the property I want to be able to set it via the
string. This is my property:

    <TypeConverter(GetType(Microsoft.DirectX.Direct3D.Compare))> _
    Public Property AlphaFunction() As
Microsoft.DirectX.Direct3D.Compare
        Get
            Return _properties(PropertiesList.AlphaFunction)
        End Get
        Set(ByVal value As Microsoft.DirectX.Direct3D.Compare)
            _properties(PropertiesList.AlphaFunction) = value
        End Set
    End Property

What do I need to do to be able to set this property via it's string
value, but still return a drop-down list within a propertygrid to allow
the value to be selected by its name? e.g.

myclass.AlphaFunction = "Never"

Thanks,

Richy

Author
22 May 2006 12:01 PM
Phill W.
Richy wrote:
> What do I need to do to be able to set this property via it's string
> value, but still return a drop-down list within a propertygrid to allow
> the value to be selected by its name?

You don't need to.
Convert the given String value into the Enum value that it represents,
something like:

    myclass.AlphaFunction _
       = CType("Never", Microsoft.DirectX.Direct3D.Compare)

HTH,
    Phill  W.
Author
22 May 2006 12:29 PM
Richy
Thanks. If I do that, however, I get

Conversion from String "Never" to type Integer is not valid.

But I just found I can do it this way:

[Enum].Parse(GetType(Microsoft.DirectX.Direct3D.Compare), "Never")

Cheers,

Richy