473,473 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

FieldInfo Class - SetValue Member Method

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
5 3936
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: James Hadwen | last post by:
Hi, I'm trying to do something with reflection that's a little wierd. The idea is that I call for a field to be updated on a base class. Using reflection it first updates the value, and then...
3
by: Glen | last post by:
FieldInfo.SetValue() doesn't appear to work when trying to set the values of fields of a struct. Why? Because structs are passed by value and not reference? -- Thanks, Glen
6
by: ORC | last post by:
I will use FieldInfo to import values from a XML file into the fields of an objects. But how to convert to the proper type like in this: public void Load(object MyClass) { foreach( FieldInfo...
3
by: Laurie | last post by:
Hi, I need to be able to manipulate field values within a structure using FieldInfo.GetValue and FieldInfo.SetValue, in VB.Nt 2003. The GetValue is working fine and makes it really easy for me...
2
by: Brent | last post by:
I have variables in a structure loaded into a list box. I thought I could use FieldInfo.SetValue to update the items value when the user clicks on it, but it is not working. .. .. .. Dim fi...
1
by: jw56578 | last post by:
Im trying to use FieldInfo.SetValue to set the values of fields in a class, the class is using reflection on itself to popluate its own fields. but i keep getting this error. Object type cannot...
1
by: David Gouge | last post by:
Posting on behalf of a colleague (no, really! ;) )... Hi, I was wondering if there is any equivalent of FieldInfo.SetValue(Object, Object) that is strongly typed or uses generics? I have been...
0
by: kernell | last post by:
Hi everyone, i'm trying to initialize all control of a UserControl to is appropriate class. The value is set but the event won't raise anymore. See the example: 'Not working 'Normally...
1
by: damiensawyer | last post by:
Hi, I need to invoke .getvalue and .setvalue on fieldinfo and propetyinfo objects. Even though they're both derived from MemberInfo, they don't share those two methods. I've finding myself...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.