2012-02-03 86 views
0

奧基,這裏是我的問題,即時通訊使用一個字符串來檢查我需要訪問哪一個調用名爲K0至K6一堆不同propertys的,即時通訊,這是該死的凌亂,我怎麼能做到這一點更清潔的方式?我相信字符串不是要走的路,所以請給我一個評論,以便朝着正確的方向前進。我怎樣才能做到這一點propertycall以更好的方式?

Dim tempAntDec As Integer 

Select Case wd.MClass 
        Case "K0" 
         tempAntDec = wd.allMeasUnc.K0.antDec 
        Case "K1" 
         tempAntDec = wd.allMeasUnc.K1.antDec 
        Case "K2" 
         tempAntDec = wd.allMeasUnc.K2.antDec 
        Case "K3" 
         tempAntDec = wd.allMeasUnc.K3.antDec 
        Case "K4" 
         tempAntDec = wd.allMeasUnc.K4.antDec 
        Case "K4-5" 
         tempAntDec = wd.allMeasUnc.K4_5.antDec 
        Case "K5" 
         tempAntDec = wd.allMeasUnc.K5.antDec 
        Case "K5-6" 
         tempAntDec = wd.allMeasUnc.K5_6.antDec 
        Case "K6" 
         tempAntDec = wd.allMeasUnc.K6.antDec 
       End Select 

我想一些其他方式喜歡叫這個,這個..還是不知道,但我覺得有一種更好的方式來處理呢?

tempAntDec = wd.allMeasUnc.KValue.antDec 
+0

你可以替換枚舉的字符串。 – JeffO 2012-02-03 01:37:52

回答

1

你可以試試VB.NET CallByName Function

如果不行然後給出一些簡單的反射一試。這裏是一個簡單的reflection tutorial的鏈接。它使用C#,但轉換爲VB.NET應該相當容易。下面是使用反射做了未經測試的代碼:

' Get the K-object reflectively. 
Dim mytype As Type = wd.allMeasUnc.GetType() 
Dim prop as PropertyInfo = mytype.GetProperty(wd.MClass) ' From the System.Reflection namespace 
Dim Kobject as Object = prop.GetValue(wd.allMeasUnc, Nothing) 

' Get the antDec property of the K-object reflectively. 
mytype = Kobject.GetType() 
prop = mytype.GetProperty("antDec") 
tempAntDec = prop.GetValue(Kobject, Nothing) 

根據您的編譯器設置,你可能需要使用DirectCast到最後一行轉換爲整數(因爲GetValue返回它作爲一個普通的對象)。像 「tempAntDec = DirectCast(prop.GetValue(kobject的,沒什麼),整數)」 的東西,如果需要可能會工作。

相關問題