Passing an Enum into a Function to determine mapping of return type?

J

Joe Duchtel

Hello -

I am trying to come up with a generic function that maps integers to a
specified Enum.

I tried ...
Function getEnum(ByVal aEnum as Type, ByVal aValue As Integer) As
Object
.... but when I call getEnum(eEnumA, 1), it is not working with the
interface.

I thought I could do a ...
return CType(aValue, [Type].GetType(aEnum.ToString))
.... to do the mapping but this is not working either. I also
tried ...
return CType(aValue, [Type].GetType("eEnumA"))
.... without any success.

Is there any way to pass a type into a function? How do I use that
type then to map an integer to the Enum that the type defines?

Thanks!
Joe
 
A

AMercer

You could encode inline as follows to assign integer i to enum x:

dim x as YourEnum
x = CType([Enum].ToObject(GetType(YourEnum), i), YourEnum)

To avoid having YourEnum twice in the expression, you could make a generic
function:

Public Function IntegerToEnum(Of T As Structure)(ByVal iValue As Integer)
As T
Return CType([Enum].ToObject(GetType(T), iValue), T)
End Function

Some sample usage:

Dim x As YourEnum = YourEnum.SomeEnumValue
Dim i As Integer = x
Dim z As YourEnum = IntegerToEnum(Of YourEnum)(i)
 
J

Joe Duchtel

You could encode inline as follows to assign integer i to enum x:

  dim x as YourEnum
  x = CType([Enum].ToObject(GetType(YourEnum), i), YourEnum)

To avoid having YourEnum twice in the expression, you could make a generic
function:

  Public Function IntegerToEnum(Of T As Structure)(ByVal iValue As Integer)
As T
    Return CType([Enum].ToObject(GetType(T), iValue), T)
  End Function

Some sample usage:

      Dim x As YourEnum = YourEnum.SomeEnumValue
      Dim i As Integer = x
      Dim z As YourEnum = IntegerToEnum(Of YourEnum)(i)



Joe Duchtel said:
I am trying to come up with a generic function that maps integers to a
specified Enum.
I tried ...
Function getEnum(ByVal aEnum as Type, ByVal aValue As Integer) As
Object
.... but when I call getEnum(eEnumA, 1), it is not working with the
interface.
I thought I could do a ...
return CType(aValue, [Type].GetType(aEnum.ToString))
.... to do the mapping but this is not working either.  I also
tried ...
return CType(aValue, [Type].GetType("eEnumA"))
.... without any success.
Is there any way to pass a type into a function?  How do I use that
type then to map an integer to the Enum that the type defines?
Thanks!
Joe- Hide quoted text -

- Show quoted text -

Hello -

Thanks for the feedback! I was trying to do the generic function but
your sample code will not compile. Is this something that only works
in .NET 2.0+? I am using .NET 1.1 with Visual Studio 2003.

Thanks,
Joe
 
A

AMercer

Generics are not available with .net 1.1. But the inline form that I
suggested should work:

dim x as YourEnum
dim i as integer
i = 123
x = CType([Enum].ToObject(GetType(YourEnum), i), YourEnum)

Does this also fail for you? Maybe ToObject wasn't available in 1.1 as well.

Joe Duchtel said:
You could encode inline as follows to assign integer i to enum x:

dim x as YourEnum
x = CType([Enum].ToObject(GetType(YourEnum), i), YourEnum)

To avoid having YourEnum twice in the expression, you could make a generic
function:

Public Function IntegerToEnum(Of T As Structure)(ByVal iValue As Integer)
As T
Return CType([Enum].ToObject(GetType(T), iValue), T)
End Function

Some sample usage:

Dim x As YourEnum = YourEnum.SomeEnumValue
Dim i As Integer = x
Dim z As YourEnum = IntegerToEnum(Of YourEnum)(i)



Joe Duchtel said:
I am trying to come up with a generic function that maps integers to a
specified Enum.
I tried ...
Function getEnum(ByVal aEnum as Type, ByVal aValue As Integer) As
Object
.... but when I call getEnum(eEnumA, 1), it is not working with the
interface.
I thought I could do a ...
return CType(aValue, [Type].GetType(aEnum.ToString))
.... to do the mapping but this is not working either. I also
tried ...
return CType(aValue, [Type].GetType("eEnumA"))
.... without any success.
Is there any way to pass a type into a function? How do I use that
type then to map an integer to the Enum that the type defines?
Thanks!
Joe- Hide quoted text -

- Show quoted text -

Hello -

Thanks for the feedback! I was trying to do the generic function but
your sample code will not compile. Is this something that only works
in .NET 2.0+? I am using .NET 1.1 with Visual Studio 2003.

Thanks,
Joe
 
A

AMercer

I assume you are compiling with option strict on. With option strict off, I
think you can freely assing integer to enum and enum to integer. Not the
best solution, but maybe in a pinch it can help you.

Also, for my own enum,

Public Enum MyEnum
x = 1
y = 2
End Enum

I can do this

Dim j As Integer = 1
Dim ee As MyEnum
ee = CType(j, MyEnum)
j = ee

No diagnostics, and option strict is on. I have some trouble with system
enums. I'm not really sure what is going on here.
Joe Duchtel said:
You could encode inline as follows to assign integer i to enum x:

dim x as YourEnum
x = CType([Enum].ToObject(GetType(YourEnum), i), YourEnum)

To avoid having YourEnum twice in the expression, you could make a generic
function:

Public Function IntegerToEnum(Of T As Structure)(ByVal iValue As Integer)
As T
Return CType([Enum].ToObject(GetType(T), iValue), T)
End Function

Some sample usage:

Dim x As YourEnum = YourEnum.SomeEnumValue
Dim i As Integer = x
Dim z As YourEnum = IntegerToEnum(Of YourEnum)(i)



Joe Duchtel said:
I am trying to come up with a generic function that maps integers to a
specified Enum.
I tried ...
Function getEnum(ByVal aEnum as Type, ByVal aValue As Integer) As
Object
.... but when I call getEnum(eEnumA, 1), it is not working with the
interface.
I thought I could do a ...
return CType(aValue, [Type].GetType(aEnum.ToString))
.... to do the mapping but this is not working either. I also
tried ...
return CType(aValue, [Type].GetType("eEnumA"))
.... without any success.
Is there any way to pass a type into a function? How do I use that
type then to map an integer to the Enum that the type defines?
Thanks!
Joe- Hide quoted text -

- Show quoted text -

Hello -

Thanks for the feedback! I was trying to do the generic function but
your sample code will not compile. Is this something that only works
in .NET 2.0+? I am using .NET 1.1 with Visual Studio 2003.

Thanks,
Joe
 
A

AMercer

I got into a tangle with this issue. Ctype should solve all your problems.

AMercer said:
I assume you are compiling with option strict on. With option strict off, I
think you can freely assing integer to enum and enum to integer. Not the
best solution, but maybe in a pinch it can help you.

Also, for my own enum,

Public Enum MyEnum
x = 1
y = 2
End Enum

I can do this

Dim j As Integer = 1
Dim ee As MyEnum
ee = CType(j, MyEnum)
j = ee

No diagnostics, and option strict is on. I have some trouble with system
enums. I'm not really sure what is going on here.
Joe Duchtel said:
You could encode inline as follows to assign integer i to enum x:

dim x as YourEnum
x = CType([Enum].ToObject(GetType(YourEnum), i), YourEnum)

To avoid having YourEnum twice in the expression, you could make a generic
function:

Public Function IntegerToEnum(Of T As Structure)(ByVal iValue As Integer)
As T
Return CType([Enum].ToObject(GetType(T), iValue), T)
End Function

Some sample usage:

Dim x As YourEnum = YourEnum.SomeEnumValue
Dim i As Integer = x
Dim z As YourEnum = IntegerToEnum(Of YourEnum)(i)



:
Hello -

I am trying to come up with a generic function that maps integers to a
specified Enum.

I tried ...
Function getEnum(ByVal aEnum as Type, ByVal aValue As Integer) As
Object
.... but when I call getEnum(eEnumA, 1), it is not working with the
interface.

I thought I could do a ...
return CType(aValue, [Type].GetType(aEnum.ToString))
.... to do the mapping but this is not working either. I also
tried ...
return CType(aValue, [Type].GetType("eEnumA"))
.... without any success.

Is there any way to pass a type into a function? How do I use that
type then to map an integer to the Enum that the type defines?

Thanks!
Joe- Hide quoted text -

- Show quoted text -

Hello -

Thanks for the feedback! I was trying to do the generic function but
your sample code will not compile. Is this something that only works
in .NET 2.0+? I am using .NET 1.1 with Visual Studio 2003.

Thanks,
Joe
 
J

Joe Duchtel

Generics are not available with .net 1.1.  But the inline form that I
suggested should work:

   dim x as YourEnum
   dim i as integer
   i = 123
   x = CType([Enum].ToObject(GetType(YourEnum), i), YourEnum)

Does this also fail for you?  Maybe ToObject wasn't available in 1.1 aswell.



Joe Duchtel said:
You could encode inline as follows to assign integer i to enum x:
  dim x as YourEnum
  x = CType([Enum].ToObject(GetType(YourEnum), i), YourEnum)
To avoid having YourEnum twice in the expression, you could make a generic
function:
  Public Function IntegerToEnum(Of T As Structure)(ByVal iValue As Integer)
As T
    Return CType([Enum].ToObject(GetType(T), iValue), T)
  End Function
Some sample usage:
      Dim x As YourEnum = YourEnum.SomeEnumValue
      Dim i As Integer = x
      Dim z As YourEnum = IntegerToEnum(Of YourEnum)(i)
:
Hello -
I am trying to come up with a generic function that maps integers to a
specified Enum.
I tried ...
Function getEnum(ByVal aEnum as Type, ByVal aValue As Integer) As
Object
.... but when I call getEnum(eEnumA, 1), it is not working with the
interface.
I thought I could do a ...
return CType(aValue, [Type].GetType(aEnum.ToString))
.... to do the mapping but this is not working either.  I also
tried ...
return CType(aValue, [Type].GetType("eEnumA"))
.... without any success.
Is there any way to pass a type into a function?  How do I use that
type then to map an integer to the Enum that the type defines?
Thanks!
Joe- Hide quoted text -
- Show quoted text -
Thanks for the feedback!  I was trying to do the generic function but
your sample code will not compile.  Is this something that only works
in .NET 2.0+?  I am using .NET 1.1 with Visual Studio 2003.
Thanks,
Joe- Hide quoted text -

- Show quoted text -

Hello -

Yep ... the ToObject is available. The only problem with this is that
even though there is no enum entry for 123, the x.ToString will output
123. I would have thought that the casting would fail in that case.

Enum MyEnum
a = 10
b = 11
End Enum

Thanks,
Joe
 
J

Joe Duchtel

Generics are not available with .net 1.1.  But the inline form that I
suggested should work:

   dim x as YourEnum
   dim i as integer
   i = 123
   x = CType([Enum].ToObject(GetType(YourEnum), i), YourEnum)

Does this also fail for you?  Maybe ToObject wasn't available in 1.1 aswell.



Joe Duchtel said:
You could encode inline as follows to assign integer i to enum x:
  dim x as YourEnum
  x = CType([Enum].ToObject(GetType(YourEnum), i), YourEnum)
To avoid having YourEnum twice in the expression, you could make a generic
function:
  Public Function IntegerToEnum(Of T As Structure)(ByVal iValue As Integer)
As T
    Return CType([Enum].ToObject(GetType(T), iValue), T)
  End Function
Some sample usage:
      Dim x As YourEnum = YourEnum.SomeEnumValue
      Dim i As Integer = x
      Dim z As YourEnum = IntegerToEnum(Of YourEnum)(i)
:
Hello -
I am trying to come up with a generic function that maps integers to a
specified Enum.
I tried ...
Function getEnum(ByVal aEnum as Type, ByVal aValue As Integer) As
Object
.... but when I call getEnum(eEnumA, 1), it is not working with the
interface.
I thought I could do a ...
return CType(aValue, [Type].GetType(aEnum.ToString))
.... to do the mapping but this is not working either.  I also
tried ...
return CType(aValue, [Type].GetType("eEnumA"))
.... without any success.
Is there any way to pass a type into a function?  How do I use that
type then to map an integer to the Enum that the type defines?
Thanks!
Joe- Hide quoted text -
- Show quoted text -
Thanks for the feedback!  I was trying to do the generic function but
your sample code will not compile.  Is this something that only works
in .NET 2.0+?  I am using .NET 1.1 with Visual Studio 2003.
Thanks,
Joe- Hide quoted text -

- Show quoted text -

I was planning on moving to .NET 2.0 (2008) shortly anyway and already
have the Visual Studio for it installed. So I think I will go the
rout of using the generic. This is actually a rather neat concept I
used to use in C++ (templates) quite a bit.

Thanks!
Joe
 
T

Tom Shelton

Generics are not available with .net 1.1.  But the inline form that I
suggested should work:

   dim x as YourEnum
   dim i as integer
   i = 123
   x = CType([Enum].ToObject(GetType(YourEnum), i), YourEnum)

Does this also fail for you?  Maybe ToObject wasn't available in 1.1 as well.



Joe Duchtel said:
You could encode inline as follows to assign integer i to enum x:
  dim x as YourEnum
  x = CType([Enum].ToObject(GetType(YourEnum), i), YourEnum)
To avoid having YourEnum twice in the expression, you could make a generic
function:
  Public Function IntegerToEnum(Of T As Structure)(ByVal iValue As Integer)
As T
    Return CType([Enum].ToObject(GetType(T), iValue), T)
  End Function
Some sample usage:
      Dim x As YourEnum = YourEnum.SomeEnumValue
      Dim i As Integer = x
      Dim z As YourEnum = IntegerToEnum(Of YourEnum)(i)
:
Hello -
I am trying to come up with a generic function that maps integers to a
specified Enum.
I tried ...
Function getEnum(ByVal aEnum as Type, ByVal aValue As Integer) As
Object
.... but when I call getEnum(eEnumA, 1), it is not working with the
interface.
I thought I could do a ...
return CType(aValue, [Type].GetType(aEnum.ToString))
.... to do the mapping but this is not working either.  I also
tried ...
return CType(aValue, [Type].GetType("eEnumA"))
.... without any success.
Is there any way to pass a type into a function?  How do I use that
type then to map an integer to the Enum that the type defines?
Thanks!
Joe- Hide quoted text -
- Show quoted text -
Thanks for the feedback!  I was trying to do the generic function but
your sample code will not compile.  Is this something that only works
in .NET 2.0+?  I am using .NET 1.1 with Visual Studio 2003.
Thanks,
Joe- Hide quoted text -

- Show quoted text -

I was planning on moving to .NET 2.0 (2008) shortly anyway and already
have the Visual Studio for it installed. So I think I will go the
rout of using the generic. This is actually a rather neat concept I
used to use in C++ (templates) quite a bit.

Thanks!
Joe

Just don't confuse generics with templates... While similar in concept, they
are different. In C++ a template is a compile time thing. And, the template
facility is sometimes used for some neat code generation tricks - but these
won't work in .NET.
 
J

Joe Duchtel

Generics are not available with .net 1.1.  But the inline form that I
suggested should work:
   dim x as YourEnum
   dim i as integer
   i = 123
   x = CType([Enum].ToObject(GetType(YourEnum), i), YourEnum)
Does this also fail for you?  Maybe ToObject wasn't available in 1.1as well.
:
You could encode inline as follows to assign integer i to enum x:
  dim x as YourEnum
  x = CType([Enum].ToObject(GetType(YourEnum), i), YourEnum)
To avoid having YourEnum twice in the expression, you could make ageneric
function:
  Public Function IntegerToEnum(Of T As Structure)(ByVal iValue As Integer)
As T
    Return CType([Enum].ToObject(GetType(T), iValue), T)
  End Function
Some sample usage:
      Dim x As YourEnum = YourEnum.SomeEnumValue
      Dim i As Integer = x
      Dim z As YourEnum = IntegerToEnum(Of YourEnum)(i)
:
Hello -
I am trying to come up with a generic function that maps integers to a
specified Enum.
I tried ...
Function getEnum(ByVal aEnum as Type, ByVal aValue As Integer) As
Object
.... but when I call getEnum(eEnumA, 1), it is not working with the
interface.
I thought I could do a ...
return CType(aValue, [Type].GetType(aEnum.ToString))
.... to do the mapping but this is not working either.  I also
tried ...
return CType(aValue, [Type].GetType("eEnumA"))
.... without any success.
Is there any way to pass a type into a function?  How do I usethat
type then to map an integer to the Enum that the type defines?
Thanks!
Joe- Hide quoted text -
- Show quoted text -
Hello -
Thanks for the feedback!  I was trying to do the generic function but
your sample code will not compile.  Is this something that only works
in .NET 2.0+?  I am using .NET 1.1 with Visual Studio 2003.
Thanks,
Joe- Hide quoted text -
- Show quoted text -
I was planning on moving to .NET 2.0 (2008) shortly anyway and already
have the Visual Studio for it installed.  So I think I will go the
rout of using the generic.  This is actually a rather neat concept I
used to use in C++ (templates) quite a bit.
Thanks!
Joe

Just don't confuse generics with templates...  While similar in concept, they
are different.  In C++ a template is a compile time thing.  And, the template
facility is sometimes used for some neat code generation tricks - but these
won't work in .NET.

Yeah ... I figured it wasn't excatly the same thing ... so thanks for
the information!

Joe
 
H

Herfried K. Wagner [MVP]

Joe Duchtel said:
I am trying to come up with a generic function that maps integers to a
specified Enum.

I tried ...
Function getEnum(ByVal aEnum as Type, ByVal aValue As Integer) As
Object
... but when I call getEnum(eEnumA, 1), it is not working with the
interface.

'GetEnum(GetType(eEnumA), 1)'.
 

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

Top