2012-02-03 82 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的,没什么),整数)” 的东西,如果需要可能会工作。

相关问题