枚举和接口

时间:2017-02-20 13:43:05

标签: .net vb.net enums interface

我试图调用与Enum值相关联的某种方法(比如称为“情绪”的方法)(例如Enum WorkDay:星期一,星期二,星期三等)。
我显然可以选择一个案例并调用某些方法,如下所示。

Public Enum WorkDay
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
End Enum

Dim CurrentDay As Class1.WorkDay = Class1.WorkDay.Monday
    Select Case CurrentDay
        Case Class1.WorkDay.Monday
            Function_Monday()
        Case Class1.WorkDay.Tuesday
            Method_Tuesday()
        Case Class1.WorkDay.Wednesday
            Method_Wednesday()
        Case Class1.WorkDay.Thursday
            Method_Thursday()
        Case Class1.WorkDay.Friday
            Method_Friday()
    End Select

但是,我之前已经看到它使用Enum的接口来调用不同类中的方法。这意味着它可以通过简单地为较新的Enum类型添加新类来进行扩展,因此不需要为较新的Enum类型(例如星期六和星期日)添加额外的Case分支。任何人都可以给我这个Enum接口的模板代码,因为我无法重新创建它。我错过了什么!

4 个答案:

答案 0 :(得分:1)

这可以通过Typesafe枚举模式轻松处理,请参阅此文章(以及之前链接的文章)提供了一个很好的概述。

Enhancing the Typesafe Enum Pattern

以下是此模式如何处理您的特定问题(转换为VB .NET)的示例:

Public Class WorkDay
   ' private constructor

   Private Sub New(day As String)
       Me.Day = day
   End Sub

  Public Day As WorkDay

  Public Sub DoDayJob()
     ' do whatever need done for that day of the week
  End Sub

  Public Shared Monday As New WorkDay("Monday")
  Public Shared Tuesday As New WorkDay("Tuesday")
  Public Shared Wednesday As New WorkDay("Wednesday")
  Public Shared Thursday As New WorkDay("Thursday")
  Public Shared Friday As New WorkDay("Friday")
End Class

 'used like this (to assign the 'Monday' value)
 Dim day = Workday.Monday

我添加了一个方法,但是这个类可以像它需要的那样复杂。

答案 1 :(得分:0)

您是否无法将枚举传递给您的方法?并基于那个执行逻辑?

有些事情如下

Function ProcessDay(DayOfWeek as WorkDay) As IWorkDay

    Select Case DayOfWeek
        Case WorkDay.Monday
             Return <<class that inherits IWorkDay>>

       Case WorkDay.Tuesday
             Return <<class that inherits IWorkDay>>


    End Select

End Function

答案 2 :(得分:0)

这似乎对我有用 - 而不是有Select Case语句此循环遍历每个Enum值并将其与Enum变量进行比较,并使用适当的名称执行该方法匹配Enum名称

    For Each day As class1.WorkDay In System.Enum.GetValues(GetType(class1.WorkDay))
        If CurrentDay = day Then
            CallByName(Me, "Method_" & [Enum].GetName(GetType(class1.WorkDay), day), CallType.Method, Nothing)
        End If
    Next

根据代码的编写方式,您可能需要更改一些内容,但基本的想法就在那里。

答案 3 :(得分:0)

根据您允许未来枚举值和/或类的目标的描述,一个简单的解决方案是使用基于枚举类型键入的委托字典。

假设这样的类声明:

Public Class Class1
    Public Enum WorkDay
         Monday
         Tuesday
         Wednesday
         Thursday
         Friday
    End Enum

    Public Shared Sub Method_Monday()
    End Sub
    Public Shared Sub Method_Tuesday()
    End Sub
    Public Shared Sub Method_Wednesday()
    End Sub
    Public Shared Sub Method_Thursday()
    End Sub
    Public Shared Sub Method_Friday()
    End Sub
End Class

Public Class Class2
    Public Enum NotWorkDay
        Saturday
        Sunday
    End Enum
    Public Shared Sub Method_Saturday()
    End Sub
    Public Shared Sub Method_Sunday()
    End Sub
End Class

然后使用上述类的类(在本例中为WinForm)看起来像这样:

Public Class Form1

    ' declare the delegate dictionary
    Private WorkMethods As New Dictionary(Of [Enum], Action)

    Public Sub New()
        InitializeComponent()
        SetWorkMethods()
    End Sub

    Private Sub SetWorkMethods()
        'fill the dictionary with the needed actions
        WorkMethods.Add(Class1.WorkDay.Monday, New Action(AddressOf Class1.Method_Monday))
        WorkMethods.Add(Class1.WorkDay.Tuesday, New Action(AddressOf Class1.Method_Tuesday))
        WorkMethods.Add(Class1.WorkDay.Wednesday, New Action(AddressOf Class1.Method_Wednesday))
        WorkMethods.Add(Class1.WorkDay.Thursday, New Action(AddressOf Class1.Method_Thursday))
        WorkMethods.Add(Class1.WorkDay.Friday, New Action(AddressOf Class1.Method_Friday))
        WorkMethods.Add(Class2.NotWorkDay.Saturday, New Action(AddressOf Class2.Method_Saturday))
        WorkMethods.Add(Class2.NotWorkDay.Sunday, New Action(AddressOf Class2.Method_Sunday))
    End Sub

    ' a helper method to retrieve and execute the action
    Private Sub DoWork(day As [Enum])
        Dim actionToPerform As Action = Nothing
        If WorkMethods.TryGetValue(day, actionToPerform) Then
            If actionToPerform IsNot Nothing Then actionToPerform()
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        DoWork(Class1.WorkDay.Wednesday)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        DoWork(Class2.NotWorkDay.Saturday)
    End Sub
End Class

这种用法使您可以灵活地配置调用的方法,而无需在需要更改时重写Select-Case块。您只需在字典中添加/删除项目。