FieldInfo Class - SetValue Member Method

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
 
C

Cor Ligthert

Dennis,

Why you want to use structures for this, for the "why not" you can search
this newsgroup, what Jay have endless written about this.

Your sample as this will run.
\\\
Public Sub starttest()
Dim t(0) As testing
t(0) = New 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
Public Class testing
Public col1 As String
Public col2 As Integer
End Class

However I stopped as well with your previous problem, you are trying in my
opinion to use endless late binding, what can in my opinion only lead to the
programs as I not want them.

Cor
 
G

Guest

Yes, I know it works with classes, not only properties in classes but fields
as well. It just won't work with Structures. As for why I want to do this,
I am working on a control that accepts various types of input from a user. I
believe C++ has generics but I don't know C++ and don't really want to learn
as VB is my choice of languages to learn.
 
G

Guest

Cor, I've found the solution from Jay's previous replies to the newsgroup,
simply convert it to a value type. It works great.

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 cv1 as valuetype = Ctype(obj,ValueType)
Dim field As FieldInfo = cv1.GetType.GetField(fldname)
If Not field Is Nothing Then
field.SetValue(obj, value)
End If
obj = cv1
End Sub
 
C

Cor Ligthert

Dennis,

I have the idea that you are busy with this, I think that probably Charles
Law is as well busy with something as this, so when you search this
newsgroup for that name you will find probably as well a lot of stuff you
can use. However I don't like this kind of challenges anymore.

Cor
 
M

Marcelo Barros G. via DotNetMonster.com

Dennis,

I need your help, your example doesn?t work for me. After the set_value sub the value still "AAAA".

Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top