nostradamus1915 (Normal User)
Newbie
Messaggi: 12
Iscritto: 03/08/2010
|
Questo topic è stato chiuso dal moderatore Ciao a tutti, sto provando a creare un array di oggetti in VB 2008. Vi spiego come ho fatto:
1. Ho creato una nuova classe di nome pezzo
Public Class pezzo
Public x, y, tipo As Integer
Public alive As Boolean
Public move(8, 8) As Boolean
Public colour As Char
End Class
2. Ho dichiarato le variabili globali all'interno del modulo
Module mod
Public vmpb(2, 8) As pezzo
Public vmpn(2, 8) As pezzo
Public scacc As scacchiera
End Module
3. Infine ho eseguito le diverse operazioni di assegnazione nel form
Dim i, k As Integer
For i = 1 To 2
For k = 1 To 8
vmpb(i, k).alive = True
vmpn(i, k).alive = True
vmpb(i, k).colour = "b"
vmpn(i, k).colour = "n"
vmpb(i, k).x = i
vmpb(i, k).y = k
vmpn(i, k).x = i + 7
vmpn(i, k).y = k
Next
Next
Il problema si verifica quando eseguo il programma. Infatti mi viene richiesto di inserire new affianco alla dichiarazione dell'oggetto, quindi Public vmpb(2, 8) As NEW pezzo, ma, quando vado ad apportare la modifica, mi dice che per un array non può essere usata l'istruzione new. Come posso fare? Spero possiate aiutarmi, grazie.
|
|
mattia1481 (Member)
Pro
Messaggi: 84
Iscritto: 03/11/2008
|
Non si possono inizializzare delle matrici a dimensione fissa...devi prima inizializzare la matrice:
Dim MiaMatrice(-1,-1) As New MioOggetto
dopo aggiungi nuove istanze di MioOggetto a MiaMatrice.
|
|
nostradamus1915 (Normal User)
Newbie
Messaggi: 12
Iscritto: 03/08/2010
|
Scusa la domanda (sono ancora un beginner in visual basic), ma come faccio a ridimensionare la matrice?
Dopo
Module cazzeggio_di_variabili
Public vmpb(-1, -1) As pezzo
Public vmpn(-1, -1) As pezzo
Public scacc As scacchiera
End Module
cosa devo fare?
|
|
mattia1481 (Member)
Pro
Messaggi: 84
Iscritto: 03/11/2008
|
Dim N as Integer = 9
Dim Q as Integer = 5
System.Array.Resize(vmpb, N)
For i As Integer = 0 To N-1
System.Array.Resize(vmpb(i), Q)
Next
Otterrai :
vmpb(8, 4)
|
|
nostradamus1915 (Normal User)
Newbie
Messaggi: 12
Iscritto: 03/08/2010
|
Mi dice che gli array non possono essere dichiarati con new, inoltre mi da 2 errori rispettivamente nelle righe di codice 3 e 5.
Error 7 Data type(s) of the type parameter(s) in method 'Public Shared Sub Resize(Of T)(ByRef array() As T, newSize As Integer)' cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error. C:\Documents and Settings\Simone\Documenti\Visual Studio 2008\Projects\scacchi\scacchi\Form1.vb 55 9 scacchi
Error 8 Data type(s) of the type parameter(s) in method 'Public Shared Sub Resize(Of T)(ByRef array() As T, newSize As Integer)' cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error. C:\Documents and Settings\Simone\Documenti\Visual Studio 2008\Projects\scacchi\scacchi\Form1.vb 57 13 scacchi
|
|
mattia1481 (Member)
Pro
Messaggi: 84
Iscritto: 03/11/2008
|
scusa...
Dim MiaMatrice(-1,-1) As MioOggetto
poi fai tutto il resto che ti ho scritto
|
|
nostradamus1915 (Normal User)
Newbie
Messaggi: 12
Iscritto: 03/08/2010
|
Niente da fare, copiando i codici in maniera identica a come li hai scritti vengono fuori questi 2 errori.
Error 7 Data type(s) of the type parameter(s) in method 'Public Shared Sub Resize(Of T)(ByRef array() As T, newSize As Integer)' cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error. C:\Documents and Settings\Simone\Documenti\Visual Studio 2008\Projects\scacchi\scacchi\Form1.vb 55 9 scacchi
Error 8 Number of indices is less than the number of dimensions of the indexed array. C:\Documents and Settings\Simone\Documenti\Visual Studio 2008\Projects\scacchi\scacchi\Form1.vb 58 37 scacchi
|
|
nostradamus1915 (Normal User)
Newbie
Messaggi: 12
Iscritto: 03/08/2010
|
Il codice che mi hai dato tu, ossia
Dim N as Integer = 9
Dim Q as Integer = 5
System.Array.Resize(vmpb, N)
For i As Integer = 0 To N-1
System.Array.Resize(vmpb(i), Q)
Next
funziona solo con gli array unidimensionali.
|
|
nostradamus1915 (Normal User)
Newbie
Messaggi: 12
Iscritto: 03/08/2010
|
Ok, sono riuscito a ridimensionare le matrici con le seguenti istruzioni:
ReDim Preserve vmpb(2, 8)
ReDim Preserve vmpn(2, 8)
Però quando vado a lavorare sull'array mi da sempre l'eccezione "Object reference not set to an instance of an object.". Any solutions? Please
|
|