views:

53

answers:

1

Hi

I have a base class with the the following enum and property:

    Public Enum InitType
        Focus = 0
        Help = 1
        ErrorToolTip = 2
    End Enum

    Property ToolTipInitType() As InitType
        Get
            Return m_initType
        End Get
        Set(ByVal value As InitType)
            m_initType = value
        End Set
    End Property

I would like to create a derived class which extends the enum, so the derived classes enum would be:

    Public Enum InitType
        Focus = 0
        Help = 1
        ErrorToolTip = 2
        HelpPopUp = 3
    End Enum

First off, how do I do this - is it by simply overloading it? And secondly will my original property pick up the new enum automatically when using the derived class or will I need to overload that too?

Any help greatly appreciated.

Thanks

Sniffer

+1  A: 

There is a way to inherit something that works pretty much the same way as an enum. As far as your code usage goes it looks pretty much the same. The trick is to define a class with static/shared fields rather than using an enum.

Public Class InitType
    Protected Sub New()
    End Sub
    Public Shared ReadOnly Focus As New InitType()
    Public Shared ReadOnly Help As New InitType()
    Public Shared ReadOnly ErrorToolTip As New InitType()
End Class

This has the same syntax when using it in your class. Like so:

Public Class ExtendingEnums
    Private m_initType As InitType = InitType.Focus
    Property ToolTipInitType() As InitType
        Get
            Return m_initType
        End Get
        Set(ByVal value As InitType)
            m_initType = value
        End Set
    End Property
End Class

Now to extend your "enum" you simply do this:

Public Class InitTypeEx
    Inherits InitType
    Public Shared ReadOnly HelpPopUp As New InitType()
End Class

Now you can access the derived enum with all of the original values plus the new one.

Public Sub Execute()
    Dim ee As New ExtendingEnums()
    ee.ToolTipInitType = InitType.Help
    ee.ToolTipInitType = InitTypeEx.HelpPopUp
    ee.ToolTipInitType = InitTypeEx.Focus
End Sub
Enigmativity
Nice one. Thanks for your help!
Sniffer