On Oct 3, 2006, at 9:23 AM, Kevin Windham wrote:
Is there a smart way to access an object and it's properties from a
string.
Suppose I have an object of class Car and properties of weight and
maxSpeed. If I read in a config file that has some config
parameters for this car,
weight: 5000
maxSpeed: 248
along with a whole bunch of other parameters, is there a decent way
to set up my code to fill those parameters in without using a bunch
of case of if statements?
What you could do is add two methods: Serialize() As String,
Deserialize(data As String)
As long as you have both methods with the data in the same order, you
will be able to read/write the object properties. Here are examples
of the two methods (using REALbasic 2006):
Function Serialize() As String
// create in-memory writable BinaryStream
Dim b As New BinaryStream(New MemoryBlock(0))
// write the object properties
b.WriteInt32(Me.Weight)
b.WriteInt32(Me.MaxSpeed)
b.Position = 0
Return b.Read(b.Length)
End Function
Sub Deserialize(data As String)
// create in-memory read-only BinaryStream
Dim b As New BinaryStream(data)
// read the object properties
Me.Weight = b.ReadInt32
Me.MaxSpeed = b.ReadInt32
End Sub
These functions return binary data and so it would be to use a Binary
file, but if you want to save it in a Text file you can use Base64
encoding.
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>