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

FieldInfo.SetValue failing under VB.Net 2003

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
Have tried various other ways to resolve this, but still to no avail.
Has anyone got any ideas? Thanks, Laurie
Nov 21 '05 #2

P: n/a
Have tried various other ways to resolve this, but still to no avail.
Has anyone got any ideas? Thanks, Laurie
Nov 21 '05 #3

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.