Sending and Storing data in Class Module

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I am using a class module and want the class module to run a SQL query and load the text from the database in a string on the class module so I can get the names quicker and won't have to run multiple SQL query's later on in my app.

My Class module looks like the following:

Class Name: Test1
B4X:
'Class module
Sub Class_Globals
   Dim name(200) As String
   Dim SQL1 As SQL
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize

End Sub

'Gets all names from the database and stores it in the string
Public Sub GetNames
For i = 1 To 200
   name(i) = SQL1.ExecQuerySingleResult("SELECT name" & i & " FROM names WHERE id = 1")
Next
End Sub

'Displays the name of the requested item
Public Sub DisplayName(item)
   Return name(item)
End Sub


Then I am calling the class like:
B4X:
CallSubDelayed("test1", "GetNames")

but when I do that my app crashes.

I am doing this correctly?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that you didn't initialize the SQL object.

In your main activity you should write:
B4X:
Sub Process_Globals
 Dim MyTest As Test1
End Sub

Sub Activity_Create(FirstTime As Boolean)
 If FirstTime Then
   MyTest.Initialize
   MyTest.GetNames
End Sub

The first index in arrays is 0. This means that you should change the name(i) line to:
B4X:
name(i - 1) = ...
 
Upvote 0
Top