L'idea è raccogliere i numeri derivati dalla scomposizione di un numero in unità, decine, centinaia eccetera, in una matrice, per poi scriverli comodamente ognuno in una label.
Vediamo...
Per ridimensionare le matrici dinamiche in VB ho costruito questo codice:
If (matrice(0) <> Nothing) Then ReDim Preserve matrice(UBound(matrice) + 1)che dovrebbe funzionare.
Ora metto dei numeri consecutivi da 1 a 10 in una matrice dinamica che inizia con lunghezza 1 (uBound = 0) e si allunga progressivamente a seconda della quantità di elementi che possiederà alla fine.
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim matrice(0) As Integer For n = 1 To 10 If matrice(0) <> Nothing Then ReDim Preserve matrice(matrice.Length) matrice(UBound(matrice)) = n Next For n = 0 To UBound(matrice) Debug.Print(matrice(n)) Next End Sub End Class
1 2 3 4 5 6 7 8 9 10Posso cambiare liberamente anche la quantità dei dati da immettere nella matrice:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim matrice(0) As Integer For n = 1 To 15 If matrice(0) <> Nothing Then ReDim Preserve matrice(matrice.Length) matrice(UBound(matrice)) = n Next For n = 0 To UBound(matrice) Debug.Print(matrice(n)) Next End Sub End Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Il codice dice che se la voce 0 non è pari a nulla la matrice va ridimensionata a un indice superiore di 1 all'ultimo indice già presente (e quindi uguale alla lunghezza della matrice), quindi il numero va scritto all'ultimo indice presente.
Molto semplice.
Ora posso scomporre il mio numero.
Ecco il codice che ho elaborato, che mi mette in una matrice le cifre di un numero, dopo aver eliminato l'eventuale virgola:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim numero As Double = 3453653 Dim stringa As String = numero.ToString.Replace(",", "") numero = Convert.ToDouble(stringa) Debug.Print(numero) Dim quoziente As Integer Dim resto As Integer Dim matrice(0) As Double Do While numero > 0 quoziente = numero \ 10 resto = numero Mod 10 If matrice(0) <> Nothing Then ReDim Preserve matrice(matrice.Length) matrice(UBound(matrice)) = resto numero = quoziente Loop For n = UBound(matrice) To 0 Step -1 Debug.Print(matrice(n)) Next End Sub End Class
3453653 3 4 5 3 6 5 3(la matrice va letta al contrario)