在 VB.NET 中重载枚举和属性

发布于 2024-09-24 18:33:42 字数 646 浏览 3 评论 0原文

我有一个具有以下枚举和属性的基类:

    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

我想创建一个扩展枚举的派生类,因此派生类枚举将是:

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

首先,我该如何做到这一点 - 是通过简单地重载它吗?其次,在使用派生类时,我的原始属性是否会自动获取新的枚举,还是我也需要重载它?

非常感谢任何帮助。

谢谢

嗅探器

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗梦空间 2024-10-01 18:33:42

有一种继承某些东西的方法与枚举的工作方式几乎相同。就您的代码使用而言,它看起来几乎相同。诀窍是定义一个具有静态/共享字段的类,而不是使用枚举。

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

在类中使用它时,它具有相同的语法。像这样:

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

现在要扩展您的“枚举”,您只需执行以下操作:

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

现在您可以访问包含所有原始值和新值的派生枚举。

Public Sub Execute()
    Dim ee As New ExtendingEnums()
    ee.ToolTipInitType = InitType.Help
    ee.ToolTipInitType = InitTypeEx.HelpPopUp
    ee.ToolTipInitType = InitTypeEx.Focus
End Sub

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文