Oppure

Loading
06/06/08 12:08
antometal
ho un problema nel codice... quando ridimensiono un vettore, tutti dati vanno persi
sono arrivato fin qui
Private Type cont_rec
n As Integer
c As Integer
End Type

Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim pos As Integer
Dim b As Boolean

Dim Num() As Integer
Dim Cont() As cont_rec

    For i = 0 To 10                 '#############
        ReDim Num(i)                '
        Num(i) = Fix(Rnd * 5)       'carica vettore
        If i > 1 Then Print Num(i - 1)            '
    Next i                          '##############
    
ReDim Cont(0)
    For i = 0 To UBound(Num)
        
        b = True
            For j = 0 To pos
                    If Num(i) = Cont(j).n Then
                        Cont(j).c = Cont(j).c + 1
                        b = False
                    End If
            Next j
            
            If b = True Then
                pos = UBound(Cont) + 1
                ReDim Cont(pos)
                Cont(pos).n = Num(i)
                Cont(pos).c = 1
            End If
    Next i
    
    For i = 0 To UBound(Cont)
        Print Cont(i).n & "x" & Cont(i).c
    Next i


il resto credo sia a posto
aaa
06/06/08 17:59
gantonio
Se ridimensioni un vettore in VB6 e vuoi conservare i dati precedenti, devi usare il Preserve

ReDim Preserve ...
aaa
06/06/08 18:11
antometal
ecco il codice funzionante (il tipo cont_rec è come quello del mio post precedente)
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim pos As Integer
Dim b As Boolean

Dim Num() As Integer
Dim Cont() As cont_rec

    For i = 0 To 10                 '#############
        ReDim Preserve Num(i)       'ridimensiona il vettore e mantiene i dati
        Num(i) = Fix(Rnd * 5)       'carica vettore
        Print Num(i) & " " & i      'stampa sul form i numeri generati e l' indice
    Next i                          '##############
    
ReDim Cont(0)
    For i = 0 To UBound(Num)
        
        b = True
            For j = 0 To pos
                    If Num(i) = Cont(j).n Then
                        Cont(j).c = Cont(j).c + 1
                        b = False
                    End If
            Next j
            
            If b = True Then
                pos = UBound(Cont) + 1
                ReDim Preserve Cont(pos)
                Cont(pos).n = Num(i)
                Cont(pos).c = 1
            End If
    Next i
    Print                           'riga vuota
    
    For i = 0 To UBound(Cont)
        Print Cont(i).n & "x" & Cont(i).c   'stampa il numero e quante volte esiste
    Next i
End Sub


to gantonio: efficace come sempre, grazie
aaa