VB.NET Operator overloading Enums?

129 views
Skip to first unread message

thomat65

unread,
Nov 27, 2008, 12:01:43 AM11/27/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hey everyone!

I've run into a slight snag with VB.NET where I need a variable of an
enumerated type such as:

"
Public Enum States
x = 0
o
unmarked
unknown
End Enum
"

to be converted into some arbitrary string value other than "x", "o",
"unmarked", or "unknown", and vise versa possibly with an overloaded
operator like:

"
Public Shared Widening Operator CType(ByVal State As States) As
String
'useful stuff here...
End Operator
"

but I can't just put this overloaded CType function in the enum block
for obvious reasons, so where would it go? Am I missing something or
is it simply impossible to overload operators for enumerated types in
VB?

To give you a further understanding of what I want, in C++ overloading
operators for situations like this was quite simple and
straightforward. All you would have to do is say something like

"
States operator[operator here](some parameters)
{/*code here*/}
"

and it would do exactly what I needed with no fuss.

Cerebrus

unread,
Nov 28, 2008, 12:51:05 AM11/28/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I don't understand why this would be a problem. You would simply
declare both the operator overloads in the class that contains the
"States" enum.

thomat65

unread,
Nov 28, 2008, 1:23:01 PM11/28/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Thanks for the reply, and I appreciate the input.

However, I think I should apologize for not making my first post as
clear as it should have been. When all is said and done, I would like
to be able to code this:
"
enum states
x
o
othervalue1
othervalue2
end enum

public sub main()
dim enumVariable as states = x
dim str as String = enumVariable
msgbox(str)
end sub
"
and then call the main subroutine and have a message box pop up that
instead of saying the integer value of states.x (in this case "0"),
says "Value X" (or whatever).

I understand that i could do this:
"
public class statesConverted
public enum states
value1
value2
value3
value4
end enum

public value as states

public sub New()
value = states.value1
end sub

public shared widening operator CType(variable as statesConverted)
as String
select case variable.value
case states.value1
return "This is value 1"
case else
return "Other values"
end select
end operator
end class

public sub main()
dim newVariable as new statesConverted()
msgbox(newVariable) 'will work
msgbox(newVariable.value) 'still won't do what i want
end sub
"
and accomplish basically the same idea, but I run into the same
problems as before whenever I try to turn the enumerated type into a
string.

So now my revised question: does vb.net have the capability to
overload operators for enumerated types without using a container
class to act as a sort of middle man?

Thanks again.

Brandon Betances

unread,
Nov 28, 2008, 1:47:04 PM11/28/08
to DotNetDe...@googlegroups.com
cant you just add the value to the enum itself? dunno if you can in VB.NET but in C# you can do this:

enum States { value1 = 1, value2, value3, value4, value1 = "string", value2 = "string", value3 = "string", value4 = "string" }
States state = States.Value1;
ConsoleWriteLine((string)state);

dunno if it works, didnt try it, but you get the idea.

Cerebrus

unread,
Nov 28, 2008, 2:14:16 PM11/28/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Enums with string values ?? 'fraid it ain't that straightforward,
buddy. You can't have string Enums unless you use extension methods
and/or custom attributes.

On Nov 28, 11:47 pm, "Brandon Betances" <bbetan...@gmail.com> wrote:
> cant you just add the value to the enum itself? dunno if you can in
> VB.NETbut in C# you can do this:

Brandon Betances

unread,
Nov 28, 2008, 8:27:30 PM11/28/08
to DotNetDe...@googlegroups.com
OK C, i tried it and your right, should've remembered enums don't have anything to do with strings. But I don't understand why this wouldnt be sufficient enough:

enum States { AK, AL, AR, AZ ... }

States state = state.AK
Console.WriteLine("you've selected " + state);

Now, if your looking to translate AK to a string that prints "Alaska", then I dont think an enum is the right thing to handle this type of operation.

thomat65

unread,
Nov 29, 2008, 4:09:58 PM11/29/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
That's what I was afraid of. I suppose that I'll just settle for
something slightly less than exactly what I wanted.

Kaarthik Padmanabhan

unread,
Nov 30, 2008, 4:20:10 AM11/30/08
to DotNetDe...@googlegroups.com
Hey, 

I think the ToString() would do the trick for you. Hope I got what you were asking for right, anyways check this out:

    Public Class statesConverted
        Public Enum states
            value1
            value2
            value3
            value4
        End Enum

        Public value As states

        Public Sub New(ByVal val As states)
            value = val
        End Sub

        Public Shared Widening Operator CType(ByVal variable As statesConverted) As String
            Select Case variable.value
                Case states.value1
                    Return "This is value 1"
                Case Else
                    Return "Other values"
            End Select
        End Operator

        Public Overrides Function ToString() As String
            Select Case Me.value
                Case states.value1
                    Return "This is value 1"
                Case states.value2
                    Return "This is value 2"
                Case states.value3
                    Return "This is value 3"
                Case states.value4
                    Return "This is value 4"
                Case Else
                    Throw New System.ComponentModel.InvalidEnumArgumentException("Invalid Enum value passed")
            End Select
        End Function

    End Class

    Public Sub main()
        Dim newVariable As New statesConverted(statesConverted.states.value1)
        'MsgBox(newVariable) 'will work
        'MsgBox(newVariable.value) 'still won't do what you want
        MsgBox(newVariable.ToString) ' maybe this is what you are asking for?
    End Sub

Regards,
Kaarthik

CK

unread,
Dec 1, 2008, 3:45:18 AM12/1/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Try using attributes on your enum values, you can add verbose text
that way.
> > I dont think an enum is the right thing to handle this type of operation.- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages