從這裏開始
Public Enum ColumnIndexEnum
FirstName = 1
LastName = 2
Address = 3
End Enum
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim row = 1
WorkSheet.GetValue(ColumnIndexEnum.FirstName, row)
End Sub
Step1-改變名稱的枚舉。(用紅色下劃線來更改所有名稱)
Public Enum ColumnIndexGetter
FirstName = 1
LastName = 2
Address = 3
End Enum
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim row = 1
WorkSheet.GetValue(ColumnIndexGetter.FirstName, row)
End Sub
第二步 - 擺脫枚舉
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim row = 1
WorkSheet.GetValue(ColumnIndexGetter.FirstName, row)
End Sub
的第三步 - 定義新的變量
Private ColumnIndexGetter As Cases
和案件的類。你需要修改這個以適合實際情況,希望你能明白。
Public Class Cases
Private Enum ColumnIndexCase1
FirstName = 1
LastName = 2
Address = 3
End Enum
Private Enum ColumnIndexCase2
FirstName = 2
LastName = 3
Address = 7
End Enum
Private _whCase As Integer
Public Sub New(WhichCase As Integer)
Me._whCase = WhichCase
End Sub
Public Function FirstName() As Integer
Dim rv As Integer = 0
Select Case Me._whCase
Case 1
rv = ColumnIndexCase1.FirstName
Case 2
rv = ColumnIndexCase2.FirstName
End Select
Return rv
End Function
Public Function LastName() As Integer
Dim rv As Integer = 0
Select Case Me._whCase
Case 1
rv = ColumnIndexCase1.LastName
Case 2
rv = ColumnIndexCase2.LastName
End Select
Return rv
End Function
Public Function Address() As Integer
Dim rv As Integer = 0
Select Case Me._whCase
Case 1
rv = ColumnIndexCase1.Address
Case 2
rv = ColumnIndexCase2.Address
End Select
Return rv
End Function
End Class
最後,使用前ColumnIndexGetter你需要
ColumnIndexGetter = New Cases(1)
'or
ColumnIndexGetter = New Cases(2)
,然後代碼將看起來像
Private ColumnIndexGetter As Cases
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ColumnIndexGetter = New Cases(1)
'or
ColumnIndexGetter = New Cases(2)
Dim row = 1
WorkSheet.GetValue(ColumnIndexGetter.FirstName, row)
End Sub
ColumnIndexGetter的初始化的實際位置可能將是你知道你在處理哪種情況。
這就是我最終做的,所以謝謝!只是一件小事。在GetColumn方法中,我允許它接受一個字符串,這是該列的名稱(不是標題,只是我在後端使用的名稱)。它試圖根據使用Enum.TryParse的時間將其解析爲正確的枚舉。所以我的解決方案比你的解決方案短一點。 但是你的建議讓我走上了正軌,所以謝謝! –