PDA

Click to See Complete Forum and Search --> : [VB6] - Save/Load UDT Arrays Easily


DigiRev
Jul 1st, 2007, 04:59 PM
This is an example of saving a UDT array directly to disk, and then reading it back into the UDT array. It shows how to save/load UDT arrays that aren't fixed in size.

This code can be useful to store settings or any other kind of information without having to worry about parsing files.

mut80r
Mar 21st, 2008, 09:08 AM
I was going over this and it works great.
You said in the comments that it should be able to change the max amount of items (the header part) from 4 characters (9999) to whatever.

I tried changing it to 5 characters long, and I modified all the relevant code, however it didn't work anymore.

Also I noticed this:


Private Function BuildHeader(ByVal Length As Long) As String
BuildHeader = String$(Abs(4 - Len(CStr(UBound(udtTest())))), "0") & CStr(UBound(udtTest()))
End Function


Shouldn't this be


Private Function BuildHeader(ByVal Length As Long) As String
BuildHeader = String$(Abs(Length - Len(CStr(UBound(udtTest())))), "0") & CStr(UBound(udtTest()))
End Function


because otherwise you're not making any use of the Length parameter as far as I can see.

Any suggestions ?

I changed all 4's to a 5 and all 5's to a 6, and it saves properly, but doesn't load.

I even changed 4 to Length and it still wouldn't load after another save.

DigiRev
Mar 21st, 2008, 03:40 PM
Thanks for pointing that out about the BuildHeader function.

If you change the length of the header, say, to 5, then you will also need to modify the LoadUDT and SaveUDT routines, where it has something like (going from memory here):

Put #intFF, 5, udtArray()
to
Put #intFF, 6, udtArray()

Same for the Get #intFF

The 2nd argument specifies which byte the UDT data starts at, which is immediately after the header. So it should be +1 length of header.

If that doesn't work, you may need to decide on a header length and then delete the file and resave the UDT array.

mut80r
Mar 21st, 2008, 05:54 PM
Thanks for pointing that out about the BuildHeader function.

No problem.

If you change the length of the header, say, to 5, then you will also need to modify the LoadUDT and SaveUDT routines, where it has something like (going from memory here):

(snip)

If that doesn't work, you may need to decide on a header length and then delete the file and resave the UDT array.

Tried that. Even deleted and resaved. Might not have done it in that order though :P can't think much lately.

will try again. :)

EDIT: works. great job. this simplifies my saving by miles.

DigiRev
Mar 21st, 2008, 06:38 PM
No problem.



Tried that. Even deleted and resaved. Might not have done it in that order though :P can't think much lately.

will try again. :)

EDIT: works. great job. this simplifies my saving by miles.

You're welcome, glad it has helped someone. :D This is how I save a lot of data in my programs now also.