1

I am successfully converted structure data to string here in plain and (as suggested) in XML serialized way which obviously every has it's own good and bad side effects.

This is sample structure:

Public Structure myList
    Dim a As String
    Dim b As Integer
    Dim c As Double
End Structure

Dim myInstance As New myList
myInstance.a = "Nemo"
myInstance.b = 10
myInstance.c = 3.14

Now I have 2 functions for converting structure data to string:

  1. Dim xString As String = oStructToString(myInstance)

    Public Function oStructToString(ByVal obj As Object) As String
    
      Dim structString As String = ""
      Dim i As Integer
      Dim myType As Type = obj.GetType()
      Dim myField As System.Reflection.FieldInfo() = myType.GetFields()
      For i = 0 To myField.Length - 1
        structString &= myField(i).GetValue(obj)
        If i = myField.Length - 1 Then Exit For
        structString &= Convert.ToChar(161)
      Next i
    
      Return structString
    End Function 
    
  2. Dim xString As String = xStructToString(myInstance)

    Public Function xStructToString(ByVal obj As Object) As String
    
      Dim x As New Xml.Serialization.XmlSerializer(obj.GetType)
      Dim sw As New IO.StringWriter()
      x.Serialize(sw, obj)
      Return sw.ToString
    End Function
    

But I can't get data back from string to structure.

Public Function oStringToStruct(ByVal xString As String) As Object

So I can call:

Dim mySecondInstance As New myList = oStringToStruct(xString)

Or

Dim mySecondInstance As New myList = xStringToStruct(xString)

How to do that?

So far I came to this:

Public Function xStringToStruct(ByVal xString As String) As Object

    Dim x As New Xml.Serialization.XmlSerializer() ''<- what here?
    Dim sr As New IO.StringReader(xString)
    Return x.Deserialize(sr)
End Function

and this... By help of har07 still one error remains here...

Public Function oStringToStruct(ByVal xString As String, ByVal type As Type) As Object

    Dim structString() As String = xString.Split(Convert.ToChar(161))
    Dim myType As Type = type.GetType()
    Dim myField As System.Reflection.FieldInfo() = myType.GetFields()
    For i As Integer = 0 To structString.Length - 1
        myField(i).SetValue(type, structString(i)) ''here crashes
    Next i

    Return type
End Function
Community
  • 1
  • 1
Wine Too
  • 4,515
  • 22
  • 83
  • 137
  • This version of the post looks certainly better. – varocarbas Jan 04 '14 at 09:42
  • You have got an answer which seems to work fine for one of your functions (X one). Regarding the other one (o), you started fine with the Split thing, but the iteration is wrong. You should iterate through the fields of the type and set a parallel counter to extract the corresponding strings. For example: – varocarbas Jan 04 '14 at 09:50
  • Well... I am more used to properties (and classes); not sure why you want to rely on a Structure at all. I was about to write the code I would use with a class (and propertyInfo) but I got unexpected results. Anyway... I guess that the idea is clear and you will be able to create the algorithm without any problem. – varocarbas Jan 04 '14 at 09:59
  • The main problem of `oStringToStruct` function is, you have to cast `structString(i)` value to a correct type of `myField(i)`. I can't find any way to do that so far. AFAIK CType/DirectCast need target cast Type to be known at compile time, can't be at run time – har07 Jan 04 '14 at 10:57
  • I concluded that too so I try to cast string to myField(i).FieldType but without (usable) result. – Wine Too Jan 04 '14 at 11:03

1 Answers1

2

Deserialize xml string back to struct is easier :

Public Function xStringToStruct(ByVal xString As String, ByVal type As Type) As Object
    Dim x As New Xml.Serialization.XmlSerializer(type)
    Dim sw As New IO.StringReader(xString)
    Return x.Deserialize(sw)
End Function

You can use it like so :

Dim xObject As myList = xStringToStruct(xString, GetType(myList))
har07
  • 88,338
  • 12
  • 84
  • 137
  • That work as expected, thank you. Anyway, I would like to get both versions working so if you have suggestion please post it. – Wine Too Jan 04 '14 at 09:46
  • I have removed my +1 because just +1 for having answered part of the question I think that is enough. – varocarbas Jan 04 '14 at 09:47