陣列轉換函式God
引入: 自定義
Visual Basic 對於Array 的判斷能力很弱,明明一個Integer 變數可以直接轉換成Long 型態,但是一個Integer 陣列卻不能轉換成Long 陣列。
為了解決該問題而誕生的函式God(資料來源, 轉換目標),會傳回一個陣列,請用該轉換目標接住。
]Function God(ByVal Input As Array, ByVal Output As Array) As Array
]If ((Input Is Nothing) OrElse (Output Is Nothing)) Then
]Return Nothing
]End If
]For i1 As Integer = Which(">", New Integer() {LBound(Input), LBound(Output)}) To Which("<", New Integer() {UBound(Input), UBound(Output)}) Step 1
]Output(i1) = Input(i1)
]Next
]Return Output
]End Function
]
]Dim testInteger As Integer() = {1, 2, 3, 4, 5}
]Dim testLong As Long(UBound(Integer) - LBound(Integer))
]testLong = God(testInteger, testLong)
不過這個函式是讓寫手在「知道轉換不會失敗」的情況下使用的,要注意幾點,第一,God 不會判斷兩者陣列索引數量是否相同,如果寫手傳入兩個索引根本就不對應的陣列,則會出現奇怪的資料。第二,請勿嘗試把一組字串陣列跟一組數值陣列傳入,這樣當然會出錯,而且也表示讀者你一定沒有好好閱讀程式碼,而是直接複製貼上。第三,這個函式不適用於二維以上的陣列,簡單來說只適用於一維陣列。
