2014-09-25 89 views
0

我使用vb 2010,我有變量,我想用數組填充它們。所以,在數組中是可變的。 例如:我怎樣才能用數組填充多個變量?

public rbt_ckd , nozzle_c, carrier_x as integer 
public state(3) as integer  
dim arrayX() as integer = {rbt_ckd,nozzle_c,carrier_x}  

for i as integer = 0 to 2  
    arrayX(i) = state(i)  
next 

我試過這個腳本格式。但它不起作用。

如何在VB.NET中做到這一點?

+0

你得到的錯誤是什麼?我想你實際上是想用'rbt_ckd,nozzle_c,carrier_x'變量填充'state'數組,不是嗎? – 2014-09-25 04:09:46

+0

狀態(3)有4個元素不是3 - (0到2)是3元素循環。 0到3 = 4個元素。 – OneFineDay 2014-09-25 04:54:56

+0

您是否期待該代碼設置來自'arrayX'的元素的每個變量的值?如果是這樣,那麼你會感到失望。如果這就是你想要的,那麼它基本上是無法完成的。 – jmcilhinney 2014-09-25 06:13:53

回答

0

變化PublicDim

Sub Test() 

    Dim rbt_ckd, nozzle_c, carrier_x As Integer 
    Dim state(3) As Integer 
    state(0) = 10 'sample added 
    state(1) = 11 'sample added 
    state(2) = 12 'sample added 
    Dim arrayX() As Integer = {rbt_ckd, nozzle_c, carrier_x} 

    For i As Integer = 0 To 2 
     arrayX(i) = state(i) 
    Next 

    Debug.Print(rbt_ckd) ' this will print 0, not 10 

End Sub 

注意,填充是值,而不是通過引用。 (見輸出添加Debug.Print()。)

相關問題