Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating A Struct In Foxpro

Status
Not open for further replies.

foxprox558

Programmer
Oct 19, 2013
53
US
Hello, again. Regarding my previous program, I need a way to create a kind of a table inside of a table, I know in oracle it is called a struct. Please help
 
A struct is usually just a block of storage containing two or more separate items, not necessarily of the same data type. For example, you might have fields containing name, birth date, salary, etc, all in one contiguous block. That block is the struct.

Is that what you are referring to?

If so, there's no direct equivalent in VFP, but you can easily simulate it with a string. So, if the struct contains, say, a name (12 characters) and a date, you might create it like this:

Code:
lcStruct = "Mike" + SPACE(8) + DTOC({^2013-12-16})

And to extract the seperate fields:

Code:
lcName = LEFT(lcStruct, 12)
lcDate = CTOD(RIGHT(lcStruct, 4))

If the struct contains integers or floats, you need a bit more work to handle the conversion to and from a string. I can give you some code for that, but first I'll wait for you to confirm that the above is what you are looking for.

Mike




__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Another quite usual approach is to save XML into a memo field. Or an INI like structure like this for single values:

Code:
[Section 1]
key1=value1
key2=value2 
[Section 2]
key3=value1
key4=value2

Structs for Windows API function calls are rather stored in a single string, a bit like Mike suggests, but on a lower level, eg dates are probably not stored in a DTOC() format, but in a binary format, for a Windows API call. Of course you could also use such struct strings and store them in a Memo(binary) or Blob field.

If you come from oracle and try to do things the way you did them there, the question is, if this really helps you. There is low value of such sub table data in a VFP table. In T-SQL you at least have ways to query and filter data stored in XML type fields. Here you just use the possibility to store anything into blob or binary fields, give it some structure, but then won't have any other means as to read it back into objects or collections or arrays or cursors to filter and query that data.

You better rethink your database design in the VFP world than try to squeeze an oracle concept into DBFs.

Bye, Olaf.
 
Craig, it's not me who is going to the trouble. I am simply trying to answer Foxprox's question.

That said, I would have thought a collection would the appropriate choice where you've got ... a collection, that is, many instances of the data that you want to store. A struct is more often used for passing data to or from functions (including API functions called from VFP). In those cases, you would usually have just a single instance of the struct.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Well, Mike, Craig,

you can also see and use a collection as a struct.

Code:
struct account {
   int account_number;
   char *first_name;
   char *last_name;
   float balance;
};

Code:
oAccount = CreateObject("Collection")
oAccount.additem(123456,"account_number")
oAccount.additem("Olaf,"first_name")
oAccount.additem("Doschke,"last_name")
oAccount.additem(1000000,"balance")


See the similarity?

A record of a DBF itself also is Kind of a struct. In fact fread recsize number of Bytes from a dbf at a record Position and yo have a single string being a struct of the record in the binary Format of the dbf.

You could also store a "struct" in an object:

Code:
oAccount = CreateObject("Empty")
AddProperty(oAccount,"account_number",123456)
AddProperty(oAccount,"first_name","Olaf")
AddProperty(oAccount,"last_name","Doschke")
AddProperty(oAccount,"balance",1000000)

And last not least of course you can store that into a record:

Code:
Create Cursor curAccounts (account_number int, first_name V(50), last_name V(50), Balance B)
Isert Into curAccounts Values (123456,"Olaf","Doschke",1000000)

All that Needs to be serialised in some way to be able to store it, despite of the Cursor of course, it's already a dbf. That would mean a separate table for that data, in the simple case the struct is the same for each record. If it varies per record it explains, why you won't want a seperate table.

And by the way SCATTER NAME oAccount also creates an object from a curAccounts record, ou can even INSERT FROM NAME oAccount to create a new account record with the object values or GATHER FROM NAME oAccount to update the current record.

I don't think foxprox is talking about C++ structs for API calls, though. Inside VFP you can use the collection or the object constructed from the initial empty object or from SCATTER or work with the cursor. I skip the Array solution, because array elements are not named elements, otherwise it's of course another solution to work with "structs".

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top