3

I have defined such public structure:

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

Later in code I assign values to it's instance:

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

And then I can convert those values to string (for storing to database) like this:

    Dim newString As String = ""
    Dim i As Integer
    Dim myType As Type = GetType(myList)
    Dim myField As System.Reflection.FieldInfo() = myType.GetFields()
    For i = 0 To myField.Length - 1
        newString &= myField(i).GetValue(myInstance) & " "
    Next i

Where string "newString" contain values of each field. All of that works OK.

Now I would like to make a function for such converting for several different structures which I have in program but I can't (don't know how) to pass certain structure and instance to function.

My try:

Public Function StructToString(ByVal myStruct As System.Type) As String

    Dim structString As String = ""
    Dim i As Integer
    Dim myType As Type = GetType(myStruct)
    Dim myField As FieldInfo() = myType.GetFields()
    For i = 0 To myField.Length - 1
        newString &= myField(i).GetValue(Nothing) & " " 
    Next i

    Return structString
End Function

But that don't work since structure cannot be converted to type.

Is here possibility to make such function for converting various structures to string (which can be placed to some public module) and how to do that properly?

Wine Too
  • 4,515
  • 22
  • 83
  • 137
  • Why? Structure the database properly with seperate feilds or use a serializer. Don't just concatanate the values with a space. – user1937198 Jan 03 '14 at 12:20
  • @user1937198, Serialization is here not good to use because of compatibility issues and all fields of structure have to be saved in single database field no matter of structure elements number. – Wine Too Jan 03 '14 at 12:42
  • The xmlserilizer will have less issues then this solution as it can handle spaces in values. It also provides a round trip guarantee which ToString does not. Also XML and JSON are defined standards which can be used from any language. – user1937198 Jan 03 '14 at 12:57
  • Ok, I will look if I can use that, thanks. – Wine Too Jan 03 '14 at 14:32

1 Answers1

4

Just pass the instance you want to transform to a string as Object and use the GetType() instance method to get the Type of the instance.

Public Function StructToString(obj As Object) As String

    Dim structString As String = ""
    Dim i As Integer
    Dim myType As Type = obj.GetType()
    Dim myField As FieldInfo() = myType.GetFields()
    For i = 0 To myField.Length - 1
        structString += myField(i).GetValue(obj) & " " 
    Next i

    Return structString
End Function

Note that your string will end with a space, so you probably should use String.Join for string concatenation. Here's your function written as a one-liner:

Public Function StructToString(obj As Object) As String
    Return String.Join(" ", obj.GetType().GetFields().Select(Function(field) field.GetValue(obj)))
End Function

Note that your system of storing stuff in a database like this will break if any string field will contain a space. Why not just create a proper database schema or use serialization? Or at least use another seperator than space.

Also, have you any specific reason to use structs with public mutable fields? If not, you should really use classes and properties instead of structs and fields.

Community
  • 1
  • 1
sloth
  • 99,095
  • 21
  • 171
  • 219
  • That works very nice, just as expected. Thank you very much! I suppose I can do same way later when I would need to convert that string back to structure fields? – Wine Too Jan 03 '14 at 12:29
  • The problem is that such a system (delimited fields by a character) will break if the delimiter is actually part of a field of an instance. Why not just use a proper serialization instead? This is a solved problem, just look at XML or JSON serialization. XML serialization is probably the easiest way to serialize objects in .Net since there's a build-in XML serializer that is good enough for most use cases. – sloth Jan 03 '14 at 12:33
  • Of course I would take different separator but not serialization because of incompatibility with other programming languages which may use same data such is pure C. But OK, that may be changed, most of problem was solved with your solution. Thanks again. – Wine Too Jan 03 '14 at 12:36