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

Calling InvokeMember on a Structure object does not update its value

P: n/a
I am trying to set a value on a structure object dynamically using
InvokeMember. After I call InvokeMember, the value on the Admin flag
does not get updated. Here is the following sample code to reproduce
the behavior:

Public Class clsTest

Public Structure userGroupStruc
Dim User As Boolean ' User Account
Dim Admin As Boolean ' Administration Account
End Structure

Public objUserGroup As userGroupStruc

Public Sub setupAccess()

Dim currentGroup As String
currentGroup = "Admin" ' For the test, lets set Admin to
True

objUserGroup.GetType().InvokeMember(currentGroup,
Reflection.BindingFlags.SetField, Nothing, objUserGroup, New Object()
{True})

End Sub

End Class
To test the behavior, simple create the class and call the setupAccess
method:
Dim objTest As New clsTest
objTest.setupAccess()

If you then take a look at the value of objUserGroup.Admin, it is
still false! (Even though we called InvokeMember and set it to true.)
I must be setting up the InvokeMember wrong but have been breaking my
head on this one... I would appreciate your help on this one.

Thanks Before Hand,
Adiel
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
<ad*****@hotmail.comschrieb
I am trying to set a value on a structure object dynamically using
InvokeMember. After I call InvokeMember, the value on the Admin
flag does not get updated. Here is the following sample code to
reproduce the behavior:
A structure is a value type. If you pass the object to InvokeMember, you
pass a copy of the object because the argument is passed ByVal. InvokeMember
would change the field value of the copy, but you never get the copy back.
The bottom line is: It does not work with value types.

Found this: (don't know if it helps or should be done)
http://mmarinov.blogspot.com/2007/01...-types-by.html
Armin

Jun 27 '08 #2

P: n/a
Thank You Armin, you have answered my question. I will take a look at
that article.

Adiel
Jun 27 '08 #3

P: n/a
I was able to resolve the problem by converting the structure into a
new class.

Thanks,
Adiel
Jun 27 '08 #4

P: n/a
<ad*****@hotmail.comschrieb:
>I am trying to set a value on a structure object dynamically using
InvokeMember. After I call InvokeMember, the value on the Admin flag
does not get updated. Here is the following sample code to reproduce
the behavior:

Public Class clsTest

Public Structure userGroupStruc
Dim User As Boolean ' User Account
Dim Admin As Boolean ' Administration Account
End Structure

Public objUserGroup As userGroupStruc

Public Sub setupAccess()

Dim currentGroup As String
currentGroup = "Admin" ' For the test, lets set Admin to
True

objUserGroup.GetType().InvokeMember(currentGroup,
Reflection.BindingFlags.SetField, Nothing, objUserGroup, New Object()
{True})

End Sub

End Class
[...]
If you then take a look at the value of objUserGroup.Admin, it is
still false!
Sample for 'SetValue', which can be easily adapted to be used with
'InvokeMember':

\\\
Dim o As <structure type>
Dim x As ValueType = o
GetType(...).GetField(...).SetValue(x, <value>)
o = CType(x, <structure type>)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.