Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Structure to String

Tags:

vb.net

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?

like image 643
Wine Too Avatar asked Aug 13 '26 19:08

Wine Too


1 Answers

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.

like image 199
sloth Avatar answered Aug 17 '26 20:08

sloth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!