解決型態變質後的ResDim

引入: 自定義

 

ResDim 用來對一個被宣告為Array 的陣列重新定義索引邊界。之前的ResDim 使用Object 陣列接住欲重新定義的陣列,但是在這個過程中,原本的陣列也變質成為Object 陣列而發生衝突。

]Function ResDim(ByVal Origin As Array, ByVal Index As Integer, Optional ByVal ToPreserve As Boolean = True) As Array

]Dim Result As Array = Array.CreateInstance(ArrayType(Origin, GetType(Object)), Index + 1)

]If (ToPreserve And Not (Origin Is Nothing)) Then

]For i1 As Integer = LBound(Result) To Ubound(Result) Step 1

]If (Castle(i1, LBound(Origin), UBound(Origin)) = i1) Then

]Result(i1) = Origin(i1)

]End If

]Next

]End If

]Return Result

]End Function

 

ResPush 也隨著ResDim 的更改而修改結構。

]Function ResPush(ByVal Origin As Array, ByVal Value As Object, Optional ByVal Index As Integer = -1) As Array

]Dim Result As Array = Nothing

]If (Origin Is Nothing) Then

]Result = Array.CreateInstance(DisNull(Value, New Object).GetType, 0 + 1)

]Index = 0

]Else

]Result = Array.CreateInstance(ArrayType(Origin), UBound(Origin) + 1 + 1)

]If (Index = -1) Then

]Index = UBound(Result)

]End If

]Index = Castle(Index, LBound(Result), UBound(Result))

]For i1 As Integer = LBound(Result) To Index - 1 Step 1

]Result(i1) = Origin(i1)

]Next

]For i1 As Integer = Index + 1 To UBound(Result) Step 1

]Result(i1) = Origin(i1 - 1)

]Next

]End If

]Result(Index) = Value

]Return Result

]End Function

文章標籤
全站熱搜
創作者介紹
創作者 GPhettoH 的頭像
GPhettoH

歇息,等待明日的天空

GPhettoH 發表在 痞客邦 留言(0) 人氣(9)