2017-02-20 83 views
-1

我想調用與Enum值(例如Enum WorkDay:週一,週二,週三等)相關聯的某種方法(稱爲「情緒」的方法)。
我顯然可以做一個選擇的情況下,並調用下面的某些方法。Enum的和接口

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接口的一些模板代碼,因爲我只是不能重新創建它。我錯過了一些東西!

+1

爲什麼不''方法'採取'WorkDay'作爲參數?那麼你只需要一個方法和一個電話。邊注:已經存在的['DayOfWeek'-枚舉(https://msdn.microsoft.com/en-us/library/system.dayofweek(V = vs.110)的.aspx),你會得到當前與['DateTime.DayOfWeek'](https://msdn.microsoft.com/en-us/library/system.datetime.dayofweek(v = vs.110)的.aspx):'昏暗陶氏= Date.Today.DayOfWeek ' –

+1

你確定你在.net中看到過這個嗎?在Java中,枚舉可以有方法,並且枚舉方法可以實現一個接口。 – k3b

+0

這聽起來像你正在尋找一個類的枚舉?你想要這些類來實現一個特定的接口,這樣你可以調用Class1.WorkDay.GetMood()?我在.NET中沒有見過類似的東西。也許你看過類似的東西,使用屬性? http://stackoverflow.com/questions/25323406/calling-a-method-by-enum-attribute-in-c-sharp –

回答

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 
1

這是很容易的類型安全枚舉模式處理,請參閱本文(和前一個鏈接自)給出一個很好的概述。

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

謝謝亞歷克斯。當我輸入答案時,C#標籤被移除時,我有點困惑。 –

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 

根據您的代碼編寫方式,您可能需要更改一些內容,但基本想法就在那裏。

0

基於對您的允許未來的枚舉值和/或類的目標的描述中,一個簡單的解決方案是使用鍵連接在基座Enum類型委託字典。

假設類的聲明是這樣的:

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塊。你只要在詞典中添加/刪除項目。