473,405 Members | 2,349 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,405 software developers and data experts.

FieldInfo.SetValue failing under VB.Net 2003

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 to maintain my code.
Unfortunately, SetValue doesn't return any errors but doesn't actually
set the value.

I have read through various newsgroup postings, and think the problem
might have something to do with "boxing", and passing of structures by
reference instead of by value. As such, I think I might be modifying a
copy of the structure, not the original structure itself. Even more
strange is that during my attempts to make it work, I found I could
issue a SetValue command within the Command Window in the VB.Net IDE
(and it would work), but the same code placed in the program did not
work. I suspect this is also to do with copies of the structure
instead of the original.

Anyway, the code below doesn't return any errors, but also doesn't set
the value! Please can somebody help me? I don't want to have to
redesign the code, and getting this working would make it really
efficient and easy to maintain

Thanks in advance

Laurie
Module module1

Structure ConfigVariables
Dim FirstVar As String
Dim SecondVar As String
Dim ThirdVar As String
End Structure
Sub Main()

Dim fld As System.Reflection.FieldInfo
Dim myType As Type = GetType(ConfigVariables)
Dim myFields As System.Reflection.FieldInfo() = myType.GetFields()
For Each fld In myFields
fld.SetValue(CV, "Test Value")
Next fld

End Sub
Nov 21 '05 #1
3 3089
Have tried various other ways to resolve this, but still to no avail.
Has anyone got any ideas? Thanks, Laurie
Nov 21 '05 #2
Have tried various other ways to resolve this, but still to no avail.
Has anyone got any ideas? Thanks, Laurie
Nov 21 '05 #3
Managed to answer my own question after reading some advice from Jay
at http://thedotnet.com/howto/work325581.aspx

The answer is to:

Dim cv1 As ValueType = CV
<then perform all SetValue operations on the cv1 object>
CV = cv1

....Sorted!!!

Thanks Jay

Laurie
Nov 21 '05 #4

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...
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...
5
by: Dennis | last post by:
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...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.