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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Property Bags

Status
Not open for further replies.

elziko

Programmer
Nov 7, 2000
486
0
0
GB
Is it possible to store a entire user defined data type using a property bag?

Many Thanks

elziko
 
You should clarify what you want to do. It is not clear!
 
If you mean a UDT, then the answer is no. That is, you can't do it directly. What you can do is create a temp. UDT buffer variable (UDT with a single fixed length string with the same number of bytes as your regular UDT) and copy your UDT to it using LSET and then store the string representation of the buffer varaible in the property bag.
Here is an example . . .


Code:
Option Explicit

Private Type myType
    intData As Integer
    lngData As Long
End Type

Private Type UDTBuffer
    strBuffer As String * 6
End Type
Private Sub Form_Load()

    Dim myData As myType
    Dim myDataBuffer As UDTBuffer
    Dim objPB As PropertyBag
    
    With myData
        .intData = 1
        .lngData = 234
    End With
    
    LSet myDataBuffer = myData
    Set objPB = New PropertyBag
    objPB.WriteProperty "PackedData", myDataBuffer.strBuffer

End Sub
- Jeff Marler B-)
 
Yeah Thanks, I thought I'd have to somehown change my UDT into a string but there are a couple of things I dont understand fully:

Firstly, my UDT is made up of lots of data types including arrays. Do I have to set the length of my "UDTBuffer" or is there some other way to do it? My UDT will vary in size you see.

Secondly, how would I retrieve the data back out of my "UDTBuffer" back into the structire of my original UDT??

Thanks Again,

elziko

 
Question 1


"Firstly, my UDT is made up of lots of data types including arrays. Do I have to set the length of my "UDTBuffer" or is there some other way to do it? My UDT will vary in size you see."


You need to know how many bytes are used to store each variable type in VB and then allocate 1 character for every 2 bytes (remember that we are using Unicode so every character gets 2 bytes). A quick and easy way to determine the string length to use (in case you don't know how many bytes are used by each variable type) is to delcare a variable of your defined type and then check the val of Len(theVariable). The number returned is how long the string will have to be. For example . . .

Code:
    Dim myData As myType

    MsgBox Len(myData)



Question 2


Secondly, how would I retrieve the data back out of my "UDTBuffer" back into the structire of my original UDT??



Just reverse the steps. See below . . .


Code:
    Dim myData As myType
    Dim myDataBuffer As UDTBuffer
    
    myDataBuffer.strBuffer = objPropertyBag.ReadProperty("PackedData")
    LSet myData = myDataBuffer


- Jeff Marler B-)
 
I think the property bag approach with a LARGE (& variable sized) UDT is going to be a major hassle. I would suggest that you make the UDT a PUBLIC array and just store the index to the UDT array element in the property bag.


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
The property bags will work fine with large UDTs . . . however, if the UDT is a variable size, then this approach will not work. What part of your UDT will make it a variable size? Why don't you post the UDT definition that wou are working with.
- Jeff Marler B-)
 
My UDT (which I may not change) is probably too complex to post as it contains several other UDTs etc.

Several of the data members are arrays which do not have fixed bounds. As far as I know this will cause the UDT to vary in size.

Using a public array wont work will it? The idea is that the properties I set will exist even when I have closed down and reopened a form that contains my control. That is the point of Property Bags isn't it?

At the moment I'm doing this by creating a file that contains my UDT which can then be loaded by the control or its property page as and when needed. However, this will only be viable when I have a single control on a form. Multiple controls wont be able to have different properties.

Sorry for all the questions but as you might have guessed I've only been working with controls for a couple of days now!

Many Thanks,

elziko
 
What you are trying to do is called serialization of state data, and property bags are on means of doing this. This will still work even if you have a very complex structure (you may need to code a little aroung variable length item). I wrote a DLL that would handle collection of collection of objects (this could be layered many deep) and the DLL would autmatically return a string everytime. - Jeff Marler B-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top