2016-04-13 19 views
0

変数名を自動的に動的に宣言する方法をオンラインで検索しましたが、結果は出ませんでした。次のように私は単純化していたコードへの私のアプローチは、次のとおりです。vb.netで自動的に変数名を動的に宣言する代替語

dim choice1 as string 
dim choice2 as string 
dim choice3 as string 
... 

私はあなたが私を助けることができる

dim count as integer 
while count is 1 to 10 
    count +=1 
end while 
dim (choice+count) as string 
' it suppose to create variable choice with the addition to the name from 1 to 10 like choice1, choice2, choice3 and so on. 

を次のように私はこれをapprochできることを願います。

PS:私もそうのような配列で宣言された変数を作成してみました:

dim count as integer 
Dim var As Array = {Dim choice1 as string, dim choice2 as string} 
while count is 1 to 10 
    var.add(dim choice+count as string) 
end while 

答えて

0

操作のこれらの種類は、私はなら、私を修正し、限り私が知っているとして(VB .NETでサポートされていません。間違っていると、賢くなります...)

しかし、保持したい値を保持するDictionary(String、Objectの)を宣言できます。

は、あなたが10個の文字列を作成したいとしましょう:

Dim myvars as New Dictionary(of String, String) 

For i = 1 to 10 
    myVars.Add("choice" & i, "New String Value") 
Next 

'Next you can access these vars like this 
Dim choice = "choice5" 
Dim string5 = myVars(choice) 
myvars(choice) = "Replaced" 

は、それが...

0

伝統的にこのパターンがアレイを使用して対処されるお役に立てば幸いです。今ではリストや辞書などを使うことができます。ほとんどの開発者は次のようなものを使います:

Dim choice(10) as String 
'choice(0) is same as choice0 
'choice(1) is same as choice1 
'choice(2) is same as choice2 
'choice(3) is same as choice3 
'etc. 
関連する問題