I think the new PowerBasic objects and Oops style coding ...
Announcement
Collapse
No announcement yet.
Saving and retrieving objects
Collapse
X
-
Just noticed this unfortunate typo...
-
Great ideas, making database access DBMS-independent, using the Component Object Model interface and doing it all using Object-Oriented Syntax...
.. I think I would use and enjoy that myself.
But if it's OK with you, I'll just use ADO until your product is ready.....
Leave a comment:
-
I just had a thought when I got back to this thread. Developing a data base type object like this, you could even define two or more instances of the object and use one as the main data base and the others as result sets of data that you pull out of the main data base. That would then give you the ability to save and load result sets to disk. There are other issues with doing this (like how to get changes made to a result set back to the main data base) but it is doable. Using inheritance could make it much easier to create a main data base object and a result set object.
There are definitely some neat possibilities here.
Leave a comment:
-
Thanks for your improvements, Jeff. I was really just trying to demonstrate the use of arrays of objects as well as some file operations with them. But for real world applications, I really like your way of handling the data, especially the fact that no global array of objects is necessary.
I'm with you, Jeff... I think the new PowerBasic objects and Oops style coding can be very useful, even without full fledged COM progrogramming.
Leave a comment:
-
I've been meaning to do up some code like that. One thing you could do with your code is, instead of creating an array of objects, make all your instance variables into arrays and have the object act like a database. I modified your code as follows:
Code:CLASS DBClass INSTANCE dNumRecords AS DWORD INSTANCE sName() AS STRING INSTANCE lAge() AS LONG CLASS METHOD CREATE() DIM sName(0) DIM lAge(0) dNumRecords = -1 END METHOD INTERFACE DBInterface INHERIT IUNKNOWN PROPERTY GET NumRecords AS DWORD PROPERTY = dNumRecords END PROPERTY PROPERTY GET FullName(dRecordNum AS DWORD) AS STRING PROPERTY = sName(dRecordNum) END PROPERTY PROPERTY GET Age(dRecordNum AS DWORD) AS LONG PROPERTY = lAge(dRecordNum) END PROPERTY METHOD addRecord(s AS STRING, n AS LONG) INCR dNumRecords REDIM PRESERVE sName(dNumRecords) REDIM PRESERVE lAge(dNumRecords) sName(dNumRecords) = s lAge(dNumRecords) = n END METHOD METHOD FileSave(sPathname AS STRING) LOCAL hFile AS DWORD LOCAL lIndex AS LONG LOCAL lTemp AS LONG hFile = FREEFILE OPEN sPathname FOR BINARY AS hFile PUT hFile, , dNumRecords FOR lIndex = 0 TO dNumRecords lTemp = LEN(sName(lIndex)) PUT hFile, , lTemp PUT hFile, , sName(lIndex) PUT hFile, , lAge(lIndex) NEXT lIndex CLOSE hFile END METHOD METHOD FileOpen(sPathname AS STRING) LOCAL hFile AS DWORD LOCAL lIndex AS LONG LOCAL lTemp AS LONG hFile = FREEFILE OPEN sPathname FOR BINARY AS hFile GET hFile, , dNumRecords REDIM sName(dNumRecords) REDIM lAge(dNumRecords) FOR lIndex = 0 TO dNumRecords GET hFile, , lTemp GET$ hFile, lTemp, sName(lIndex) GET hFile, , lAge(lIndex) NEXT lIndex CLOSE hFile END METHOD END INTERFACE END CLASS FUNCTION PBMAIN LOCAL lIndex AS LONG LOCAL sPathname AS STRING LOCAL oDB AS DBInterface LET oDB = CLASS "DBClass" sPathname = "Sample File with Objects" IF ISFILE(sPathname) THEN 'open file and list records oDB.FileOpen(sPathname) FOR lIndex = 0 TO oDB.NumRecords MSGBOX "Name: " + oDB.FullName(lIndex) + $CRLF + "Age: " + FORMAT$(oDB.Age(lIndex)) NEXT lIndex ELSE 'save records oDB.addRecord("Mary Ann Rutherford", 25) oDB.addRecord("Robert A. Jones", 36) oDB.addRecord("Sam Smith", 29) oDB.FileSave(sPathname) MSGBOX "Done saving... run again to open file and list records" END IF END FUNCTION
This version is basically creating an in memory database that can be saved to and loaded from disk. You could rewrite it so that it was completely disk based, add caching of some data, and even have the object take care of indexing the data. All in all, objects are looking pretty handy.
Leave a comment:
-
Saving and retrieving objects
Here is some sample code I thought would be helpful to people wanting to get started with some OOP style coding. It uses a minimum of INSTANCE variables including a dynamic string to demonstrate saving objects to a file and later retrieving them. Each object defined from the cRecord class is a data base record listing a name and age of a person. These records are stored in a global array of objects.
Code:GLOBAL oRecord() AS iRecord GLOBAL numRecords AS LONG CLASS cRecord INSTANCE sName AS STRING INSTANCE age AS LONG INTERFACE iRecord: INHERIT IUNKNOWN PROPERTY GET sName AS STRING PROPERTY = sName END PROPERTY PROPERTY GET age AS LONG PROPERTY = age END PROPERTY METHOD addRecord(s AS STRING, n AS LONG) sName = s: age = n END METHOD METHOD saveRecord(fileNo AS LONG) LOCAL n AS LONG n = LEN(sName): PUT fileNo,, n: PUT fileNo,, sName PUT fileNo,, age END METHOD METHOD openRecord(fileNo AS LONG) LOCAL n AS LONG GET fileNo,, n: sName = SPACE$(n): GET fileNo,, sName GET fileNo,, age END METHOD END INTERFACE END CLASS FUNCTION PBMAIN LOCAL i AS LONG, fileName AS STRING DIM oRecord(3) AS iRecord LET oRecord(1) = CLASS "cRecord" LET oRecord(2) = CLASS "cRecord" LET oRecord(3) = CLASS "cRecord" fileName = "Sample File with Objects" IF ISFILE(fileName) THEN 'open file and list records fileOpen(fileName) FOR i = 1 TO 3 MSGBOX "Name: " + oRecord(i).sName + $CRLF + "Age: " + STR$(oRecord(i).age) NEXT i ELSE 'save records oRecord(1).addRecord("Mary Ann Rutherford", 25) oRecord(2).addRecord("Robert A. Jones", 36) oRecord(3).addRecord("Sam Smith", 29) numRecords = 3 fileSave(fileName) MSGBOX "Done saving... run again to open file and list records" END IF END FUNCTION SUB fileSave(fileName AS STRING) LOCAL i, fileNo AS LONG fileNo = FREEFILE OPEN fileName FOR BINARY AS fileNo PUT fileNo,, numRecords FOR i = 1 TO numRecords oRecord(i).saveRecord(fileNo) NEXT i CLOSE fileNo END SUB SUB fileOpen(fileName AS STRING) LOCAL i, fileNo AS LONG fileNo = FREEFILE OPEN fileName FOR BINARY AS fileNo GET fileNo,, numRecords FOR i = 1 TO numRecords oRecord(i).openRecord(fileNo) NEXT i CLOSE fileNo END SUB
Leave a comment: