I have an issue where I have a database wrapper class that I have defined. The internal database property is defined as a REALSQLDatabase and I wish to change this for a SQLiteDatabase if compiling under Xojo.
Is there a way to change the property definition if the RBVersion > 2013?
In my setup method of the class I have the following code:
#if RBVersion > 2013 then
Database = new SQLiteDatabase
#else
Database = new REALSQLDatabase
#endif
This works fine provided the actual property is set to the right class which means physically changing the property definition.
I am looking for the ability to programmatically change the definition…
I have a class with a property that is defined as REALSQLDatabase.
I wish to change this programmatically if I am using Xojo. I know that I can just go to the property and redefine it in the IDE as SQLiteDatabase but I want the class to react depending upon the version I am using.
A property is not defined in code otherwise I would use the RBVersion directive (as I use it to construct the new Database as shown in my original post). However I cannot find a way to do this with the base property. It is this directive that I am looking for.
You can define the property as Database, since both SQLiteDatabase and REALSQLDatabase are Database subclasses.
The problem with this approach is that your are just postponing the problem, and every time you use the property you must cast it to the proper class.
Btw, dont use "Database as a property name, since this is the name of a class.
In practice, provided that you have a property defined like this
myDB as Database
you can do:
#if RBVersion > 2013 then
myDB = new SQLiteDatabase
#else
myDB = new REALSQLDatabase
#endif
[quote=58929:@Massimo Valle]You can define the property as Database, since both SQLiteDatabase and REALSQLDatabase are Database subclasses.
The problem with this approach is that your are just postponing the problem, and every time you use the property you must cast it to the proper class.
Btw, dont use "Database as a property name, since this is the name of a class.
In practice, provided that you have a property defined like this
myDB as Database
you can do:
#if RBVersion > 2013 then
myDB = new SQLiteDatabase
#else
myDB = new REALSQLDatabase
#endif
[/quote]
Yes, I tried this and you are right, I got compiler errors in the following code. Can you post a quick description of the casting you are referring to?
[quote=58929:@Massimo Valle]The problem with this approach is that your are just postponing the problem, and every time you use the property you must cast it to the proper class.
[/quote]
To clarify, not every time, just when you need a method or property that is found only in the subclass.
For example:
dim db as Database
db = new SQLiteDatabase // Works because it's a Database
rs = db.SQLSelect sql // Works because Database defines SQLSelect
db.Encrypt "pw" // Won't work because Database doesn't have Encrypt
SQLiteDatabase( db ).Encrypt "pw" // Works
Well, you can assign a SQLiteDatabase object to a Database property, since the SQLiteDatabase class is a Database subclass. That is, SQLiteDatabase is ALSO a Database object.
But if you need to access a property which is only present in the SQLiteDatabase class, then you have to do something like this:
SQLLiteDatabase(myDB.DatabaseFile) = f // f is a folderItem.