Salve. Nel capitolo 11 della guida di Emanuele La Torre, c'è il progetto "Classes" che ho copiato e sto cercando di testare. Purtroppo questa guida è stata scritta per una vecchia versione di Gambas e, fino al capitolo 10, sono riuscito a correggere qualcosa di obsoleto (p. es. il modo di scrivere le Open dei file). Ora sono incappato in un errore che non riesco a risolvere.
Qui sotto vedete la parte iniziale del codice nel form di avvio:
' Gambas class file
'Contacts will represent the collection of records for the addr book
Public Contacts As Collection
'represents a single contact record used by all subroutines
Public ContactRecord As Contact
'index that points to the current record
Public RecordPointer As Integer
'we will need a string var to use when the user searches by lastname
Public SearchKey As String
Qui c'è una subroutine di ricerca che sta in un modulo:
Public Sub DoFind()
'declare a contact object for local use
Dim MyContactRecord As Contact
'instantiate it
'set the record pointer to the first record in the collection
RecordPointer = 1
'for debug only
Print "In DoFind with: " & SearchKey
'use FOR EACH to iterate through each object in the collection
For Each MyContactRecord In Contacts
'assign each temp record to our global contact object
ContactRecord = Contacts[CStr(RecordPointer)]
'if the last name matches search key, update the form and leave
If ContactRecord.LastName = SearchKey Then
'for debug only
Print "Found: " & MyContactRecord.LastName
'now update the form
UpdateForm
Break 'force return with this statement
Endif
'no match so we increment the record pointer and move to the next
Inc RecordPointer
Next
End
L'errore è "Unknown Identifier: RecordPointer in Module1.Module: 12.
Chi m'aiuta?
Grazie.
Grazie. Benissimo. Io invece stavo mettendo altre Dim come quelle che ho "commentato" con due apici (nel codice qui sotto) e quindi cambiando il nome 'RecordPointer' in 'MyRecordPointer'. In questo modo l'errore scompariva, ma poi non so se avrebbe funzionato. Invece ora un altro errore esce sul Break: Unexpected Break... Alt! Mentre parlavo del Break sono andato a controllare e mi sono accorto che avevo commentato anche la riga del 'For Each MyContactRecord...', ho tolto l'apice e corretto 'In Contacts' con 'In Form1.Contacts' e l'errore del Break è scomparso (così ho capito a che serve il break). Ne è comparso un altro in un Form di ricerca. Ne parliamo dopo.
Ecco il codice, dove ho messo anche l'inizio del modulo, perché ancora non ho capito a che serve la funzione. Procedo un passetto alla volta.
' Gambas module file
Public Function SearchOn(sKey As String) As String
Return sKey
End
Public Sub DoFind(sKey As String)
'declare a contact object for local use
Dim MyContactRecord As Contact
''Dim MyRecordPointer As Integer
'instantiate it
''MyContactRecord = New Contact
'set the record pointer to the first record in the collection
Form1.RecordPointer = 1
'for debug only
Print "In DoFind with: " & sKey
'use FOR EACH to iterate through each object in the collection
For Each MyContactRecord In Form1.Contacts
'assign each temp record to our global contact object
MyContactRecord = Form1.Contacts[CStr(Form1.RecordPointer)]
'if the last name matches search key, update the form and leave
If MyContactRecord.LastName = sKey Then
'for debug only
Print "Found: " & MyContactRecord.LastName
'now update the form
Form1.UpdateForm
Break 'force return with this statement
Endif
'no match so we increment the record pointer and move to the next
Inc Form1.RecordPointer
Next
End