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

Calling InvokeMember on a Structure object does not update its value

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
4 1942
<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
Thank You Armin, you have answered my question. I will take a look at
that article.

Adiel
Jun 27 '08 #3
I was able to resolve the problem by converting the structure into a
new class.

Thanks,
Adiel
Jun 27 '08 #4
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
5
by: Chris | last post by:
boolResult = (bool)typeofobj.InvokeMember("ReadString",BindingFlags.InvokeMethod, null, newobj, new object{HKEY_CURRENT_USER, "Software\\Example Reg Path", "Example String Value", strResult}); ...
0
by: Jared Hagel | last post by:
I can't use Type.InvokeMember(...) on a remote object. But when I use Type.InvokeMember(...) on a non-remote object in the exact same way, I don't have a problem. Does someone know what I'm...
3
by: Patrick Ireland | last post by:
I am dynamically loading a class. One of the methods of the class takes a short * (by reference) argument. However, the InvokeMember call used to invoke the method of the class passings arguments...
3
by: Pat Ireland | last post by:
The following code shows what I am trying to do. The normal invocation of the CalcByRef produces the correct results, however, using the InvokeMember fails to correctly pass the single argument by...
1
by: Amanda | last post by:
Let me see if I can explain this correctly. In VB.NET, I want to call classes based on a recordset I get back from a SQL Server database. Basically, I have a value sending in a table called...
4
by: Howard Kaikow | last post by:
I am trying to retrive some WMI properties using Option Strict On. This requires the use of InvokeMember. I know that there are alternative ways to get the values, but I want to learn how to...
0
by: Qwert | last post by:
Hello, why does the following code work for a CLASS, but not for a STRUCTURE? (The first MsgBox returns '0,0', but I expected it to be '100,0'.) (The second MsgBox returns 'Hello', as...
4
by: eBob.com | last post by:
In my class which contains the code for my worker thread I have ... Public MustInherit Class Base_Miner #Region " Delegates for accessing main UI form " Delegate Sub DelegAddProgressBar(ByVal...
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.