Italian Funzioni ...

Roberto Tsolakis

Member
Licensed User
Longtime User
Ho ripreso dopo un anno a buttar giù qualche linea di codice, nonostante abbia sempre continuato a pagare gli aggiornamenti (è un ottimo compilatore, versione 7.00)

Passiamo al dunque ... le funzioni ... non mi funzionano!

Ho voluto copiare del codice dalla guida per l'utente ...

nel modulo MAIN ho inserito questo :

B4X:
Dim Interest = MyModule.CalcInterest(1234, 5.2)
    ToastMessageShow(Interest,True)

mentre nel modulo MyModule

B4X:
Sub CalcInterest(Capital As Double, Rate As Double) As Double
    Return Capital * Rate / 100
End Sub

Purtroppo nel log appage in rosso : Unknown member : calcinterest

dov'è l'errore ???
 

Star-Dust

Expert
Licensed User
Longtime User
Iniziamo dando qualche spiegazione?
B4A, B4J, B4I?
Che cosa stai provando?

Da quello che leggo il metodo é dentro un modulo di codice che é chiamato MyModule... lo hai creato?
Oppure hai creato una sub sul main?
 

Roberto Tsolakis

Member
Licensed User
Longtime User
1) b4a
2) e' un esempio del libro GUIDE
3) Si l'ho creato ... altrimenti il prefisso mymodule non sarebbe servito


Ho copiato l'esempio per vedere se non avevo ben capito le funzioni (cosa che utilizzo da VB6!).
Ma vedo che anche l'esempio base non funziona!
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Posta tutto il codice o invia il sorgente in ZIP
 

Star-Dust

Expert
Licensed User
Longtime User
A me funziona

Main
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim Interest As Double = MyModule.CalcInterest(1234, 5.2)
    ToastMessageShow(Interest,True)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Code Module: MyModule

B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub CalcInterest(Capital As Double, Rate As Double) As Double
    Return Capital * Rate / 100
End Sub
 

Star-Dust

Expert
Licensed User
Longtime User
PS. Aggiorna in B4A 7.0.1
 

Star-Dust

Expert
Licensed User
Longtime User

Star-Dust

Expert
Licensed User
Longtime User
Activity non é modulo ma é una nuova attività.
Un App può avere più attività separate. Per comunicarsi si usano variabili pubbliche non si chiamano però i metodi se non con comandi speciali (callSub(Activity,Metodo) )

Il Modulo di codice invece é condiviso da tutti.

Le classi che sono Oggetti che hanno Proprietà, Metodi ed Eventi.
 

Roberto Tsolakis

Member
Licensed User
Longtime User
Non avevo l'aggiornamento, forse perchè ho scaricato il compilatore mesi fa e non ho guardato di recente (o magari l'ho guardato ma avevo altro da fare).
Infatti c'è scritto di scaricare dal link in email ... lo stesso della 7.00.
Ora lo faccio.
Grazie per le info!


Ad ogni modo stavo facendo un programma creando tutto da codice ... l'activity completa.
Poi ho spostato la funzion ... o se vogliamo metodo ... in un altro activity module ... e da li ho notato che non era globale.
Per il momento grazie!
 

Star-Dust

Expert
Licensed User
Longtime User
Di nulla. Il cambio dei nomi é perché siamo su Android.... questo Basic é un involucro di Java e quindi ne ereditiamo molte cose fra cui nomi.
 

Roberto Tsolakis

Member
Licensed User
Longtime User
Ad ogni modo avviare un activity da CODE MODULE non è possibile .... qualche idea ?


B4X:
Activity.Initialize("Activity")

L'errore che mi da ...

Cannot access activity object from sub Process_Globals
 

Star-Dust

Expert
Licensed User
Longtime User
StartActivity(NomeAttivita)

Ma cosa serve avviare un anuova attività da codice?
 

Roberto Tsolakis

Member
Licensed User
Longtime User
Perchè in un app che sto facendo mi serve più volte un menu a buttos ... quindi volevo farmi una funzione tipo :

Sub NomeFunzione(Titolo as string ,MatriceConNomePulsanti as string))

dici che è meglio usare dei pannel ?
 

Star-Dust

Expert
Licensed User
Longtime User
Avere più attività in una sola App ... deve servire.... Devono essere veramente attività diverse... altrimenti se le usi come fossero pannelli devi impazzire per far comunicare i vari pezzi dell'App.

Avere più pannelli magari può essere più pratico... poi dipende cosa stai realizzando
 

Star-Dust

Expert
Licensed User
Longtime User
Notte
 

Roberto Tsolakis

Member
Licensed User
Longtime User
Rieccomi ...
Il mio progetto inizia con chiedere all'utente una serie di domande,
che per comodità ho inserito in dei buttons all'interno di una scrollview.
ovviamente ripetere lo stesso codice con semplicemente cambiando le domande è stupido e poco leggibile.
Quindi avevo pensato di creare la funzione MENU_DI_BOTTONI in un Active Module e
diciamo che chiamando tale "funzione" con un CallSub, funzionicchiaa,
ma mi sono accorto che la Sub chiamata (nell'altro modulo) inviava un risultato (solitamente NULL) prima della fine della scelta dell'utente,
confermato anche da un ToastMessage.

Mentre la chiamata ad una funzione tipo risultato=activity.nomesub non da risultati strani, ma allo stesso tempo non accetta di lavorare con le Activity, I Pannell e le ScrollView.

Qualche Idea ?

Allego il progetto.
 

Attachments

  • Menu a Bottoni.zip
    8.5 KB · Views: 240

Star-Dust

Expert
Licensed User
Longtime User
Una classe...fammi dare un occhiata
 

Roberto Tsolakis

Member
Licensed User
Longtime User
ACTIVE MODULE : MAIN

B4X:
Sub Process_Globals
    LogColor("MAIN : Process_Globals",Colors.Blue)
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Globals
    LogColor("MAIN : Globals",Colors.Blue)
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.Title=""

    ToastMessageShow("Menu 1",True)
' MENU DI BOTTONI #1
    Dim Lista As List
    Lista.Initialize
    Dim Testi() As String
    Testi = Array As String("MENU 1","","Voce 1", "Voce 2" , "Voce 3" , "Voce 4", "Voce 5", "Voce 6" , "Esci dal programma","")
    'Lista.Add(Testi)
    CallSubDelayed2(Menu,"Semplice_a_bottoni",Testi)
    LogColor("Bottone > " & Menu.ritorno,Colors.Magenta)
    LogColor("Risultato è " & Menu.ritorno,Colors.Magenta)

    ToastMessageShow("Menu 2",True)
' MENU DI BOTTONI #2
    Dim Lista As List
    Lista.Initialize
    Dim Testi() As String
    Testi = Array As String("MENU 2","","Voce2 1", "Voce2 2" , "Voce2 3" , "Voce2 4", "Voce2 5", "Voce2 6" , "Esci dal programma 2","")
    'Lista.Add(Testi)
    CallSub2(Menu,"Semplice_a_bottoni",Testi)
    LogColor("Bottone > " & Menu.ritorno,Colors.Magenta)
   
    LogColor("Risultato è " & Menu.ritorno,Colors.Magenta)
    ToastMessageShow("Finito",True)
End Sub


ACTIVE MODUE : MENU

B4X:
Sub Semplice_a_bottoni(Matrice() As String)
    ' ***********************************************************************
    ' Matrice = Testo dei buttons
    ' ***********************************************************************
    Activity.Initialize("Activity")                                            ' CREA IL FORM DA CODICE
    Dim SV1 As ScrollView                                                    ' CREA L'OGGETTO SCROLLVIEW DA CODICE
    Dim Buttons(Matrice.Length) As Button                                                ' CREA L'OGGETTO ARRAY DI BUTTON DA CODICE
    Dim AltezzaSchermo As Int = Activity.Height / 10                        ' IMPOSTA ALTEZZA SCHERMO PARI AD 1 / 10 DI SCHERMO
   SV1.Initialize(1000dip)                                                    ' IMPOSTA LA SCROLLVIEW A 1000 DIP
    Activity.AddView(SV1,0,0,100%x,100%y)                                    ' AGGIUNGI L'OGGETTO SCROLLVIEW NEL FORM
    SV1.Color=Colors.black                                                    ' IMPOSTA IL COLORE BLUE DELLA SCROLLVIEW
    For a = 0 To Buttons.Length -1
        Buttons(a).Initialize("Buttons")                                    ' INIZIALIZZA I BUTTON
        SV1.Panel.addview(Buttons(a),10,AltezzaSchermo    * a+10,100%x-10,10%y-10)          ' DISEGNA I BUTTONS NELLA SCROLLVIEW
        If Matrice(a)="" Then 
            Buttons(a).Enabled=False  
            Buttons(a).visible=False
        End If
        Buttons(a).Color=Colors.DarkGray                                        ' IMPOSTA IL COLORE DEL BUTTON
        Buttons(a).Text=Matrice(a)                                                    ' IMPOSTA IL TESTO DEL BUTTON
        Buttons(a).TextColor=Colors.Yellow   
        Buttons(a).tag=a                                                    ' IMPOSTA NUMERO BUTTONS DI RICONOSCIMENTO
    Next
        Buttons(0).TextColor=Colors.Cyan
    Buttons(0).Enabled=False
 
End Sub




Sub Buttons_Click
    Private ButtonTaggato As Button
    ButtonTaggato = Sender
    LogColor("Button " & ButtonTaggato.tag & " ButtonTaggato",Colors.Magenta)
    Ritorno = ButtonTaggato.tag
 End Sub
 
Top