title: Converting String to Structure
body: <p>I am successfully converted structure data to string <a href="https://stackoverflow.com/questions/20903468/converting-structure-to-string">here</a> in plain and (as suggested) in XML serialized way which obviously every has it's own good and bad side effects.</p>
  
  <p>This is sample structure:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Now I have 2 functions for converting structure data to string:  </p>
  
  <ol>
  <li><p><code>Dim xString As String = oStructToString(myInstance)</code></p>
  
  <pre><code>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 &amp;= myField(i).GetValue(obj)
      If i = myField.Length - 1 Then Exit For
      structString &amp;= Convert.ToChar(161)
    Next i
  
    Return structString
  End Function 
  </code></pre></li>
  <li><p><code>Dim xString As String = xStructToString(myInstance)</code></p>
  
  <pre><code>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
  </code></pre></li>
  </ol>
  
  <p>But I can't get data back from string to structure.</p>
  
  <pre><code>Public Function oStringToStruct(ByVal xString As String) As Object
  </code></pre>
  
  <p>So I can call:</p>
  
  <pre><code>Dim mySecondInstance As New myList = oStringToStruct(xString)
  </code></pre>
  
  <p>Or</p>
  
  <pre><code>Dim mySecondInstance As New myList = xStringToStruct(xString)
  </code></pre>
  
  <p>How to do that?</p>
  
  <p>So far I came to this:</p>
  
  <pre><code>Public Function xStringToStruct(ByVal xString As String) As Object
  
      Dim x As New Xml.Serialization.XmlSerializer() ''&lt;- what here?
      Dim sr As New IO.StringReader(xString)
      Return x.Deserialize(sr)
  End Function
  </code></pre>
  
  <p>and this...
  By help of har07 still one error remains here...</p>
  
  <pre><code>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
  </code></pre>