Because a Class will be available to my entire application, I want to connect to a MySQL Server using a Class object.
I am new to Xojo, so I borrowed the class from an example provided from another Xojo member from this forum, but the member’s example provided no other code for the application.
The Class object is named “dbs”, with no super.
Within the dbs class, there is a Method named “Constructor”.
This is the code for the method Constructor:
Constructor(Host As String, Port As Integer, UserName As String, Password As String, DBName As String, KeepAlive As Integer)
mDB = New MySQLCommunityServer
mDB.Host = Host
mDB.Port = Port
mDB.UserName = UserName
mDB.Password = Password
mDB.DatabaseName = DBName
mKeepAlive = New Timer
mKeepAlive.Period = 15 * 60 * 1000
AddHandler mKeepAlive.Action, AddressOf KeepAlive
#If DebugBuild Then
mDB.Host = “portal.axisdirect.co.nz”
mDB.Port = 6033
#endif
If mDB.Connect Then
mKeepAlive.Period = KeepAlive
mKeepAlive.Mode = Timer.ModeMultiple
Else
Break
End If
In my application, I have a Public Property: MyDB As dbs
An event in a window in the app has the following code:
Dim sHost As String, sUserName As String, sPassword As String, sDBName As String, intPort As Integer, intKeepAlive As Integer
sHost = “127.0.0.1”
sUserName = “root”
sPassword = “”
sDBName = “test”
intPort = 3306
intKeepAlive =1
Dim MyDB As dbs
MyDB.Constructor (sHost, intPort , sUserName, sPassword, sDBName, intKeepAlive)
When I run the code, I get a NilObjectException, but I do not know why. Any help would be appreciated.
John