Confusion over Classes!

Greetings. I’m new to Xojo and really considering for some future projects. I’ve been programming for years (C, VB6, ASP, VB.net) but not really versed in oop. Looking at Xojo, I’ve started out by building a simple, one button webform. All I want to do is connect to a database, run a SQL statement, and have it read back some of the items from a SELECT statement. Now, within the action event of the button, I can do this and it works fine. Declare a db object, set the parameters, read a recordset, etc. However, to extend my learning I am trying to move it over to a class. I have created a class with a db property and methods for opening, closing and reading a recordset from the database, but it doesn’t seem like I can set the properties (host, db name,etc) and get it to run properly. I am quite certain I could run all of this in a module but this is probably not the best way to do this in XOJO. Any advice would be most helpful. Thanks.

Can you post what you have so far?

///Action of button

Dim rsB As RecordSet
Dim clsTest as Class1
clsTest=new Class1

Dim i as Integer

i=clsTest.Connect_To_MYSQL(“localhost”,3306,“sakila”,“XOJO1”,“XOJO123”)
if i = 1 Then
rsB=clsTest.Retrieve_Data(“SELECT city.city FROM sakila.city”)

If rsB <> NIL Then
  rsB.MoveFirst
  While Not rsB.EOF
    msgBox(rsB.Field("city").StringValue)
    rsB.MoveNext
  Wend
  rsB.Close
  clsTest.Close_My_SQL
End IF

Else
MsgBox(“Unable to connect to Databse”)
End If

//Properties
db as MySQLCommunityServer

//Method connect to database
//Parameters
//sHost as String,iPort as Integer,sDBName as String,sUserName as String,sPWD as String

db.Host=sHost
db.port =iPort
db.DatabaseName=sDBName
db.UserName=sUserName
db.Password=sPWD

If Not db.Connect Then return 0 else return 1

I have created the class for you. Made a few mods but the main thing was you declare db but did not initialise it. You needed to add db = new MySQLCommunityServer in your connect to database method. Made some other minor mods i.e. close method and a method to get your data.

In my cubby (dropbox equiv) here

Let me know how you get on

If you’re looking for extended training and like video training, I have over 100 Xojo and Real Studio training videos at http://www.bkeeney.com/XojoTraining/xojotraining.cgi. There is about 6 hours without a subscription and about 36 hours for subscribers. I have a complete series where I start from the beginning and end up with a basic database application. That’s about 10 hours and 30 videos.

Thanks MIke. I’ll take a look. I had the other methods too but had to open it to be able to the other things!

Thanks Bob - I’ll take a look. I’m familar with development, it is just the OO and Xojo that I am not as familar with.

You might also want to review Introduction to Programming with Xojo, Chapter 8 (which covers classes and OO). Also User Guide Book 1: Fundamentals, Chapter 5: Classes has some OO topics.

These are not in-depth, but they might help get you started.

And remember, Xojo uses pretty much the same OO concepts you’d see in things like VB.NET, C# or Java. So techniques you’ve learned with those languages can often be used as-is.

I also hear they’ll be some OO and DB topics at XDC this year. And that BKeeney fellow is doing some pre-conference training as well.