G
Guest
I have an array "t" where each element is a structure of type "testing". I
want to use the FieldInfo class to set the values of the array element
fields. I can read the field properties and return the velue using this
technique but cannot set the field value as per below.
Why won't it change the value. If define "testing" as a class where col1
and col2 are fields as opposed to properties, it works fine. Just won't work
when "testing" is a structure.
Public Structure testing
Public col1 As String
Public col2 As Integer
End Structure
Private Sub starttest()
Dim t(1) As testing
t(0).col1 = "AAAA"
t(0).col2 = 16
'Try to reset the value of t(0) using FieldInfo.SetValue Method
set_value(t(0), "col1", "XXXXX")
'Runs ok but didn't change t(0).col1
End Sub
Private Sub set_value(ByVal obj As Object, ByVal fldname As String,
ByVal value As Object)
Dim sourcetype As Type = obj.GetType
Dim field As FieldInfo = sourcetype.GetField(fldname)
If Not field Is Nothing Then
field.SetValue(obj, value)
End If
End Sub
want to use the FieldInfo class to set the values of the array element
fields. I can read the field properties and return the velue using this
technique but cannot set the field value as per below.
Why won't it change the value. If define "testing" as a class where col1
and col2 are fields as opposed to properties, it works fine. Just won't work
when "testing" is a structure.
Public Structure testing
Public col1 As String
Public col2 As Integer
End Structure
Private Sub starttest()
Dim t(1) As testing
t(0).col1 = "AAAA"
t(0).col2 = 16
'Try to reset the value of t(0) using FieldInfo.SetValue Method
set_value(t(0), "col1", "XXXXX")
'Runs ok but didn't change t(0).col1
End Sub
Private Sub set_value(ByVal obj As Object, ByVal fldname As String,
ByVal value As Object)
Dim sourcetype As Type = obj.GetType
Dim field As FieldInfo = sourcetype.GetField(fldname)
If Not field Is Nothing Then
field.SetValue(obj, value)
End If
End Sub