473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Assigning variables using FieldInfo.SetValue

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 As Reflection.FieldInfo
fi = tp.GetField(Item)
MsgBox(fi.GetValue(InItems)) 'returns the original value
fi.SetValue(InItems, Value.ToString) 'Nothing happens
MsgBox(fi.GetValue(InItems)) 'still returns the original value
..
..
..

Am I missing something or doing it wrong?

Thanks
Brent

Nov 21 '05 #1
2 4971
You might want to read up on this thread:
http://groups.google.com/groups?lr=&th=84f4322176f2d1f4

Basically, since a structure is a value type but the SetValue expects an
object, the SetValue is working on the boxed object rather than on the
structure itself. You should find a couple of solutions in the thread above
on how to work around this.

hope that helps..
Imran.
"Brent" <Br***@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
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 As Reflection.FieldInfo
fi = tp.GetField(Item)
MsgBox(fi.GetValue(InItems)) 'returns the original value
fi.SetValue(InItems, Value.ToString) 'Nothing happens
MsgBox(fi.GetValue(InItems)) 'still returns the original value .
.
.

Am I missing something or doing it wrong?

Thanks
Brent

Nov 21 '05 #2
Ah

So close, but so far.
I just switched out the word Structure for Class and it worked.

Thanks
Brent

"Imran Koradia" wrote:
You might want to read up on this thread:
http://groups.google.com/groups?lr=&th=84f4322176f2d1f4

Basically, since a structure is a value type but the SetValue expects an
object, the SetValue is working on the boxed object rather than on the
structure itself. You should find a couple of solutions in the thread above
on how to work around this.

hope that helps..
Imran.
"Brent" <Br***@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
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 As Reflection.FieldInfo
fi = tp.GetField(Item)
MsgBox(fi.GetValue(InItems)) 'returns the original value
fi.SetValue(InItems, Value.ToString) 'Nothing happens
MsgBox(fi.GetValue(InItems)) 'still returns the original

value
.
.
.

Am I missing something or doing it wrong?

Thanks
Brent


Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andre | last post by:
I have two questions; 1) When executing the sub routine "TestStructure", I'm trying to update the member "intTyres" in the structure "structureCar" dynamically using System.Reflection. The code...
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...
0
by: JC Voon | last post by:
Hi All: I have a problem when trying to set the value of the structure member using reflaction,here is the code: Public Const MemberAccess As BindingFlags = BindingFlags.Public Or...
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...
9
by: Bill Grigg | last post by:
All, Can anyone supply an example or reference to an example of using reflection to determine the data types and array lengths contained in a nested stucture in C#? Actually, it is a structure...
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...
6
by: chandramohanp | last post by:
Hi I am trying to modify class instance members using reflection. I am having problem when trying to add/remove/display elements related to List<int> member. Following is the code. class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.