mi sono imbattuto in questa domanda, mentre colpire questa strada bloccare me stesso. Ho finito per scrivere un pezzo di codice molto veloce per gestire questo ReDim Preserve
su un array di nuova dimensione (prima o ultima dimensione). Forse aiuterà gli altri che affrontano lo stesso problema.
Quindi, per l'utilizzo, diciamo che l'array è stato impostato originariamente come MyArray(3,5)
e che si desidera aumentare le dimensioni (innanzitutto!), Diciamo semplicemente a MyArray(10,20)
. Saresti abituato a fare qualcosa del genere giusto?
ReDim Preserve MyArray(10,20) '<-- Returns Error
Ma purtroppo restituisce un errore perché si è tentato di modificare la dimensione della prima dimensione. Così con la mia funzione, si sarebbe solo fare qualcosa di simile, invece:
MyArray = ReDimPreserve(MyArray,10,20)
Ora la matrice è più grande, ei dati vengono conservati. Il tuo ReDim Preserve
per un array a più dimensioni è completo. :)
E, ultimo ma non meno importante, la funzione miracolosa: ReDimPreserve()
'redim preserve both dimensions for a multidimension array *ONLY
Public Function ReDimPreserve(aArrayToPreserve,nNewFirstUBound,nNewLastUBound)
ReDimPreserve = False
'check if its in array first
If IsArray(aArrayToPreserve) Then
'create new array
ReDim aPreservedArray(nNewFirstUBound,nNewLastUBound)
'get old lBound/uBound
nOldFirstUBound = uBound(aArrayToPreserve,1)
nOldLastUBound = uBound(aArrayToPreserve,2)
'loop through first
For nFirst = lBound(aArrayToPreserve,1) to nNewFirstUBound
For nLast = lBound(aArrayToPreserve,2) to nNewLastUBound
'if its in range, then append to new array the same way
If nOldFirstUBound >= nFirst And nOldLastUBound >= nLast Then
aPreservedArray(nFirst,nLast) = aArrayToPreserve(nFirst,nLast)
End If
Next
Next
'return the array redimmed
If IsArray(aPreservedArray) Then ReDimPreserve = aPreservedArray
End If
End Function
ho scritto questo come 20 minuti, quindi non ci sono garanzie. Ma se vuoi usarlo o estenderlo, sentiti libero. Avrei pensato che qualcuno avrebbe già avuto un codice come questo qui, beh apparentemente no. Quindi qui vai a fare i colleghi.
VB6 non supporta array di array –
@EuroMicelli sì lo fa. Puoi avere una serie di varianti 1-D e le varianti possono contenere array. – MarkJ