Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2004

Tip: Looking for answers? Try searching our database.

Attempting to SetValue in FieldInfo Reflection class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bruce Parker - 11 Aug 2004 16:09 GMT
I am attempting to set a value through the FieldInfo and the value does not
appear to be stored.  Here is a snippet of the code:

'This is the Base Class
Imports System.Reflection

Public Class DummyBase

   Protected moElements As Object

   Public Sub New()

   End Sub

   Public Overridable Sub MoveToGlobal()
       Dim oFieldType As System.Type
       Dim value As Object

       Dim myFieldInfo() As FieldInfo
       Dim myType As Type = moElements.GetType
       ' Get the type and fields of FieldInfoClass.
       myFieldInfo = myType.GetFields(BindingFlags.NonPublic Or _
                     BindingFlags.Instance Or BindingFlags.Public)

       oFieldType = myFieldInfo(0).FieldType
       value = 9
       Select Case oFieldType.Name
           Case "Int32"
               myFieldInfo(0).SetValue(moElements, CType(value, Int32))
       End Select
   End Sub
End Class

'This is the inherited class
Public Class Dummy
   Inherits DummyBase

   Public Structure Elements
       Dim DatabaseID As Int32
   End Structure

   Public TableElements As Elements

   Public Sub New()

       MyBase.New()
       TableElements.DatabaseID = 10
       moElements = TableElements
   End Sub

End Class

'This is the class for Testing
Public Class Test

   Public Sub New()
       Dim DatabaseID As Integer
       Dim oDummy As New Dummy
       oDummy.MoveToGlobal()
       DatabaseID = oDummy.TableElements.DatabaseID
   End Sub
End Class

The value is never 9.  Right after I perform a SetValue, I do a GetValue in
the Command Window and it stills says 10.  The DatabaseID is also still 10.
Mattias Sj?gren - 11 Aug 2004 17:04 GMT
Bruce,

That's because Elements is a Structure, and you end up modifying a
boxed copy of TableElements. It would probably work better of Elements
was a Class.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.