2012-02-03 10 views
0

Okey、ここに私の問題は、私はアクセスする必要があるかどうかをチェックする文字列を使用して、K0にK6という名前の異なるpropertysの束を呼んで、これはいたずらです、私はもっとクリーンな方法でそれを行うことができますか?私はストリングスが行く方法ではないと確信していますので、正しい方向に進むために私にコメントをお願いします。どうすればこのプロパティコールをより適切に行うことができますか?

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

文字列をEnumに置き換えることができます。 – JeffO

答えて

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) 

コンパイラの設定によっては、(GetValueメソッドは、プレーンなオブジェクトとしてそれを返すので)整数に最後の行を変換するために、DirectCastを使用する必要があります。 「tempAntDec = DirectCast(prop.GetValue(Kobject、Nothing)、Integer)」のようなものは、必要に応じておそらく動作します。

関連する問題