By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

FieldInfo Class - SetValue Member Method

P: n/a
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
--
Dennis in Houston
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Nov 21 '05 #2

P: n/a
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.

"Cor Ligthert" wrote:
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

Nov 21 '05 #3

P: n/a
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
"Cor Ligthert" wrote:
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

Nov 21 '05 #4

P: n/a
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

"Dennis" <De****@discussions.microsoft.com>
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.

"Cor Ligthert" wrote:
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

Nov 21 '05 #5

P: n/a
Dennis,

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

Thanks in advance

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.