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

System.Reflection Question

P: n/a
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);
}
}

--
Erol
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
See http://thedotnet.com/howto/work325581.aspx

--

Carlos J. Quintero

The MZ-Tools all-in-one add-in, now for .NET: http://www.mztools.com
"Erol" <Er**@discussions.microsoft.com> escribió en el mensaje
news:D3**********************************@microsof t.com...
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);
}
}

--
Erol

Nov 21 '05 #2

P: n/a
Thank-you very much Carlos, it worked

"Carlos J. Quintero [.NET MVP]" wrote:
See http://thedotnet.com/howto/work325581.aspx

--

Carlos J. Quintero

The MZ-Tools all-in-one add-in, now for .NET: http://www.mztools.com
"Erol" <Er**@discussions.microsoft.com> escribió en el mensaje
news:D3**********************************@microsof t.com...
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);
}
}

--
Erol


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.