Differenze tra le versioni di "Strutture: dichiarazione ed uso"

Da Gambas-it.org - Wikipedia.
Riga 4: Riga 4:
  
  
  '''PUBLIC''' Struct ''<Font Color= #B22222>NomeStruttura</font>    ''<Font Color= #006400>' ''definisco i campi della Struttura "modello"''</font>
+
  '''PUBLIC''' Struct <Font Color= #B22222>NomeStruttura</font>    ''<Font Color= #006400>' ''definisco i campi della Struttura "modello"''</font>
 
   
 
   
 
  ''<Font Color= #006400>' ''Questa Struttura si riserverà, occupandola, 9 byte di memoria in tutto:''</font>
 
  ''<Font Color= #006400>' ''Questa Struttura si riserverà, occupandola, 9 byte di memoria in tutto:''</font>
Riga 41: Riga 41:
 
     Print .valore3
 
     Print .valore3
 
   End With
 
   End With
 +
 +
'''End'''
 +
 +
 +
==Strutture contenenti Array e Matrici==
 +
Le Strutture possono essere costituite da campi di variabili Array e Matrici.
 +
 +
Esempio:
 +
 +
'''PUBLIC''' Struct <Font Color= #B22222>StrutturaModello</font>
 +
    arB[10] As Byte      <Font Color= #006400>' ''array monodimensionale definito''</font>
 +
    arI As Integer[]      <Font Color= #006400>' '' array monodimensionale non definito''</font>
 +
    mt$[10, 4] As String  <Font Color= #006400>' '' matrice''</font>
 +
'''End''' Struct
 +
 +
  Private <Font Color= #0000FF>strutturaVariabile</font> As New <Font Color= #B22222>StrutturaModello</font>
 +
 +
 +
'''PUBLIC''' Sub Form_Open()
 +
 +
  Dim a, m As Byte
 +
 +
  <Font Color= #0000FF>strutturaVariabile</font> = New <Font Color= #B22222>StrutturaModello</font>
 +
 +
  <Font Color= #0000FF>strutturaVariabile</font>.arI = New Integer[]
 +
 +
  For a = 0 To 9
 +
    With <Font Color= #0000FF>strutturaVariabile</font>
 +
      .arB[a] = a
 +
      .arI.Add(a + a)
 +
        For m = 0 To 3
 +
          .ar$[a, m] = Chr(<Font Color= #0000FF>strutturaVariabile</font>.arI[a] + 70)
 +
        Next
 +
    End With
 +
  Next
 +
 +
'''End'''
 +
 +
 +
'''PUBLIC''' Sub Button1_Click()
 +
 +
  Dim a, m As Byte
 +
 +
  For a = 0 To 9
 +
    With <Font Color= #0000FF>strutturaVariabile</font>
 +
    Print .arB[a]
 +
    Print .arI[a]
 +
    For m = 0 To 3
 +
      Print .ar$[a, m]
 +
    Next
 +
    End With
 +
  Next
 
   
 
   
 
  '''End'''
 
  '''End'''

Versione delle 18:10, 14 mag 2012

La Struttura similmente all'Array aggrega due o più elementi. Però, mentre l'Array aggrega elementi del medesimo tipo (per esempio tutti valori di tipo Integer, o di tipo Byte, etc), la Struttura aggrega solitamente (ma non necessariamente) elementi di tipo diverso.

Dichiariamo una Struttura con dei campi, che dovranno essere riempiti con dei valori. Tale Struttura avrà un proprio nome e farà da modello alle particolari variabili strutture che saranno effettivamente utilizzate per contenere valori.


PUBLIC Struct NomeStruttura    ' definisco i campi della Struttura "modello"

' Questa Struttura si riserverà, occupandola, 9 byte di memoria in tutto:
  valore1 As Integer   ' dal byte n° 0 al n° 3
  valore2 As Integer   ' dal byte n° 4 al n° 7
  valore3 as Byte      ' il byte n° 8
 END Struct

' creo una struttura "reale" composta a perfetta immagine di quella modello:
   nuovaStruttura As New NomeStruttura



Public Sub Form_Open()

Dim variabValor as Byte

 variabValor = Inputbox("Immetto un valore per \"riempire\" il campo \"variabile\" di \"nuovaStruttura\": valore3 .")

' attribuisco un valore a ciascun campo variabile della struttura
' (qui i valori sono due "costanti" ed uno "variabile" che immetto io con InputBox):
 nuovaStruttura.valore1 = 3
 nuovaStruttura.valore2 = 6
 nuovaStruttura.valore3 = variabValor

End


Public Sub Button1_Click()

' quindi opero con i valori contenuti da "nuovaStruttura",
' per esempio li faccio mostrare in console:
  With nuovaStruttura
    Print .valore1
    Print .valore2
    Print .valore3
  End With

End


Strutture contenenti Array e Matrici

Le Strutture possono essere costituite da campi di variabili Array e Matrici.

Esempio:

PUBLIC Struct StrutturaModello
    arB[10] As Byte       ' array monodimensionale definito
    arI As Integer[]      '  array monodimensionale non definito
    mt$[10, 4] As String  '  matrice
End Struct

 Private strutturaVariabile As New StrutturaModello


PUBLIC Sub Form_Open()

 Dim a, m As Byte

  strutturaVariabile = New StrutturaModello

  strutturaVariabile.arI = New Integer[]

  For a = 0 To 9
    With strutturaVariabile
      .arB[a] = a
      .arI.Add(a + a)
        For m = 0 To 3
         .ar$[a, m] = Chr(strutturaVariabile.arI[a] + 70)
        Next
   End With
 Next

End


PUBLIC Sub Button1_Click()

 Dim a, m As Byte

  For a = 0 To 9
   With strutturaVariabile
    Print .arB[a]
    Print .arI[a]
    For m = 0 To 3
      Print .ar$[a, m]
    Next
   End With
 Next

End