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?