Home > database >  How can I compare a class against an enum and have Intellisense suggest the enums?
How can I compare a class against an enum and have Intellisense suggest the enums?

Time:08-20

At first, I had defined a class like so:

Public Class Company
      Public CompanyName As String
      (... some other properties ...)
End Public 

In my code, I often have to check against the company.

I used this:

If SomeCompany.CompanyName = "Google" Then

Since this is time taking and error prone, I have introduced a new property:

Public Enum eWellKnownCompany
     eNone = 0
     eAmazon = 1
     eGoogle = 2
End Enum

Public Class Company
      Public Identifier As eWellknownCompany
      Public CompanyName As String
      (... some other properties ...)
End Public 

Now I use this:

If SomeCompany.Identifier = eGoogle Then

This way I can avoid making spelling errors. However, coding like this still takes too much time for me.

Ideally, I would like to be able to make the If Then statement like this:

If SomeCompany = eGoogle Then 

I know that this is checking an object against an enum and therefore not working.

Is there perhaps a smart solution similar to this in VB.NET anyways?

Per suggestion by @John, I have added operator overrides. This works fine, but it does not work as I had expected.

I expected Intellisense to suggest the Enums only. (I hope my post makes more sense now and shows how I expect it to work.)

Instead, I see this:

enter image description here

enter image description here

Thank you!

CodePudding user response:

I just tested this code:

Module Module1

    Sub Main()
        Dim sc1 As New SomeClass With {.SomeProperty = SomeEnum.SomeField}

        Console.WriteLine(sc1 = SomeEnum.SomeField)
        Console.WriteLine(sc1 = SomeEnum.SomeOtherField)
        Console.WriteLine(sc1 <> SomeEnum.SomeField)
        Console.WriteLine(sc1 <> SomeEnum.SomeOtherField)

        Console.ReadLine()
    End Sub

End Module

Public Enum SomeEnum
    SomeField
    SomeOtherField
End Enum

Public Class SomeClass

    Public Property SomeProperty As SomeEnum

    Public Shared Operator =(someClass As SomeClass, someEnum As SomeEnum)
        Return someClass.SomeProperty = someEnum
    End Operator

    Public Shared Operator <>(someClass As SomeClass, someEnum As SomeEnum)
        Return someClass.SomeProperty <> someEnum
    End Operator

End Class

and the output was this:

True
False
False
True

That basically allows you to do as you want, although you obviously still need to qualify the enum field with the enum name, which you omitted from your question.

EDIT:

If you're really determined to be able to use the fields of the enumeration unqualified then this will work:

Module Module1

    Sub Main()
        Dim sc1 As New SomeClass With {.SomeProperty = SomeEnum.SomeField}

        Console.WriteLine(sc1 = SomeField)
        Console.WriteLine(sc1 = SomeOtherField)
        Console.WriteLine(sc1 <> SomeField)
        Console.WriteLine(sc1 <> SomeOtherField)

        Console.ReadLine()
    End Sub

End Module

Public Enum SomeEnum
    SomeField
    SomeOtherField
End Enum

Public Class SomeClass

    Public Property SomeProperty As SomeEnum

    Public Shared Operator =(someClass As SomeClass, someEnum As SomeEnum)
        Return someClass.SomeProperty = someEnum
    End Operator

    Public Shared Operator <>(someClass As SomeClass, someEnum As SomeEnum)
        Return someClass.SomeProperty <> someEnum
    End Operator

End Class

Public Module Module2

    Public Const SomeField As SomeEnum = SomeEnum.SomeField
    Public Const SomeOtherField As SomeEnum = SomeEnum.SomeOtherField

End Module
  • Related