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 / Windows Forms / WinForm General / December 2004

Tip: Looking for answers? Try searching our database.

System.Reflection Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Erol - 14 Dec 2004 08:53 GMT
I have two code snipets, one in VB and the other in C#. Why does the visual
basic example not change the structure member "x", but the C# code does? I
need the visual basic code to work as I am a visual basic coder, but I don't
know what is wrong. Is there a bug in visual basic.net? Please help!

'VB Code
Imports System
Imports System.Reflection

Structure Foo
    Public x As Integer
End Structure

Class Test

    Shared Sub Main()
        Dim f As New Foo
        f.x = 10

        Dim fi As FieldInfo = GetType(Foo).GetField("x")

        Dim o As Object = f
        fi.SetValue(o, 3)
        f = CType(o, Foo)

        Console.WriteLine(f.x)
    End Sub
End Class

//C#
using System;
using System.Reflection;

struct Foo
{
    public int x;
}

Class Test
{
    static void Main()
    {
        Foo f = new Foo();
        f.x = 10;

        FieldInfo fi = typeof(Foo).GetField("x");

        object o = f;
        fi.SetValue (o, 3);
        f = (Foo)o;

        Console.WriteLine (f.x);
    }
}

Signature

Erol

Bruce Wood - 15 Dec 2004 01:44 GMT
I took a long, hard look at your code but I can't see any difference
between the two. The only thing I can suggest is to use ildasm to look
at the code these two generate to see if there is some crucial
difference. Perhaps VB is doing boxing and unboxing differently when
you say fi.SetValue(o, 3). I would have to disassemble it to find out.

That said, you do realize that having setters on structure fields is an
extremely dodgy thing to do in .NET? It can lead to all sorts of
unexpected behaviour. I'm wondering if you're a seasoned .NET
programmer trying to do something clever, or someone learning the
framework who maybe chose struct when a class would have been a better
idea?

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.