I have two questions;
1) When executing the sub routine "TestStructure", I'm trying to update the
member "intTyres" in the structure "structureCar" dynamically using
System.Reflection. The code executes, but the value does not change, nor is
there an exception thrown.
2) Read comments in sub routine "TestIntegerProperty"
Imports System.Reflection
Imports System.Windows.Forms
Public Class cReflection
Public Structure structureCar
Public intTyres As Integer
Public strName As String
End Structure
Dim mTestStructure As New structureCar
Dim mTestIntegerProperty As New System.Windows.Forms.Button
Public Sub TestStructure()
Dim myType As Type = GetType(structureCar)
Dim myFieldInfo As FieldInfo = myType.GetField("intTyres",
BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.InvokeMethod Or
BindingFlags.Instance Or BindingFlags.SetField)
'The next line of code is supposed to change the value of the
"intTyres" member to 5, but it stays zero.
'When a class is used instead of a structure it works. (this is not
an option)
myFieldInfo.SetValue(mTestStructure, 5) 'This does not generate an
exeception
End Sub
Public Sub TestIntegerProperty()
Dim objValue As Object
objValue = "200"
Dim myType As Type = GetType(System.Windows.Forms.Button)
Dim myFieldInfo As PropertyInfo = myType.GetProperty("Left")
'The following line of code does not change the value of the "Left"
property, becuase objValue is seen as string.
'Assigning an integer to objValue does however work, but this is not
an option for us for various reasons. How do I get the method "SetValue" to
accept the object, "objValue", as a string, but convert it to an integer(if a
number is a string, declared as an object, does the object not automatically
get converted to an integer, because the property expects an integer?).
myFieldInfo.SetValue(mTestIntegerProperty, objValue, Nothing)
'Exception gets generated.
'The following doesn't work as well
'I am trying to ctype the "string" objValue to the PropertyType of
the "Left" property
'Uncomment line 41 and 42 to see error
'----------------------------------------------------------------------------------------------------------------------
'Dim tType As Type = Type.GetType(myFieldInfo.PropertyType.FullName)
'How do I create a dynamic type so that "Ctype" can see the type
"tType"
'myFieldInfo.SetValue(mTestIntegerProperty, CType(objValue, tType),
Nothing)
'----------------------------------------------------------------------------------------------------------------------
End Sub
End Class 5 5611
Andre <An***@discussions.microsoft.com> wrote: I have two questions;
1) When executing the sub routine "TestStructure", I'm trying to update the member "intTyres" in the structure "structureCar" dynamically using System.Reflection. The code executes, but the value does not change, nor is there an exception thrown.
Of course the value doesn't change - you're passing a value type, which
is then being boxed, and you're not keeping a reference to the boxed
version.
Try declaring a variable of type Object, setting its value to the boxed
version of mTestStructure, then calling SetValue, then unboxing back to
mTestStructure.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Hi Jon
Thanks for your reply. I'm not familiar with the terms boxed and unboxed.
Could you please give me a quick example, using my example.
Regards
Andre
"Jon Skeet [C# MVP]" wrote: Andre <An***@discussions.microsoft.com> wrote: I have two questions;
1) When executing the sub routine "TestStructure", I'm trying to update the member "intTyres" in the structure "structureCar" dynamically using System.Reflection. The code executes, but the value does not change, nor is there an exception thrown.
Of course the value doesn't change - you're passing a value type, which is then being boxed, and you're not keeping a reference to the boxed version.
Try declaring a variable of type Object, setting its value to the boxed version of mTestStructure, then calling SetValue, then unboxing back to mTestStructure.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Andre <An***@discussions.microsoft.com> wrote: Thanks for your reply. I'm not familiar with the terms boxed and unboxed. Could you please give me a quick example, using my example.
Boxing is what happens when you use a value type as a reference type,
and unboxing is the taking the value out of the box object.
I can't easily come up with the proper example in VB.NET, as I'm mainly
a C# coder, but in C# it would be:
object o = mTestStructure;
myFieldInfo.SetValue(o, 5);
mTestStructure = (structureCar) o;
Here's a complete example in 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);
}
}
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Hi Jon
Thank-you for the example.
I've converted every line of code to Visual Basic .net and I have the exact
same problem as before. When I ran the C# code, the example worked, but when
I ran the VB code, the cursor steps over the code, but does not change the
value of x in the structure. Is this a Visual basic bug? Please copy this
code and paste it in a Visual Basic Console project and you will see what I
mean. Please help!
Regards
Andre
Imports System
Imports System.Reflection
Public Structure Foo
Public x As Integer
End Structure
Public Class Test
Public 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
"Jon Skeet [C# MVP]" wrote: Andre <An***@discussions.microsoft.com> wrote: Thanks for your reply. I'm not familiar with the terms boxed and unboxed. Could you please give me a quick example, using my example.
Boxing is what happens when you use a value type as a reference type, and unboxing is the taking the value out of the box object.
I can't easily come up with the proper example in VB.NET, as I'm mainly a C# coder, but in C# it would be:
object o = mTestStructure; myFieldInfo.SetValue(o, 5); mTestStructure = (structureCar) o;
Here's a complete example in 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); } }
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Andre <An***@discussions.microsoft.com> wrote: Thank-you for the example.
I've converted every line of code to Visual Basic .net and I have the exact same problem as before. When I ran the C# code, the example worked, but when I ran the VB code, the cursor steps over the code, but does not change the value of x in the structure. Is this a Visual basic bug? Please copy this code and paste it in a Visual Basic Console project and you will see what I mean. Please help!
This is very odd. The VB compiler seems to be calling
RuntimeHelpers.GetObjectValue for no reason that I can make out.
I would ask on the VB.NET group if I were you, with the sample program
you've just posted. It looks like it's a VB.NET quirk to me :(
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andre |
last post by:
I have two questions;
1) When executing the sub routine "TestStructure", I'm trying to update the
member "intTyres" in the structure "structureCar" dynamically using
System.Reflection. The code...
|
by: Cat |
last post by:
I have class Base, and class Derived.
I am serializing Derived objects from XML, and these Derived objects are
allowed to 'inherit' the values from other named Base objects already defined
in...
|
by: Opa |
last post by:
Hi,
I'm trying to get some private class members using reflection, but am having
trouble: Example
using System;
using System.Reflection;
public class Customer
{
|
by: Ken Varn |
last post by:
I have an ASP.NET application that is calling a custom class that is trying
to parse all of the members of my Page object using Type.GetMembers(). The
problem that I am having is that private...
|
by: Lucas Tam |
last post by:
Hi all,
I need to set a property on a subclass such as:
Report.ExportOptions.ExportFormatType = ExportFormatType.Excel
AND
Report.ExportOptions.FormatOptions = excelFormatOpts
|
by: Charles Law |
last post by:
I have adapted the following code from the MSDN help for PropertyInfo
SetValue. In the original code, the structure MyStructure is defined as a
class MyProperty, and it works as expected. There is...
|
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...
|
by: bill |
last post by:
All,
I have the following:
......
unsafe public struct Example
{
public double we;
|
by: |
last post by:
I have made some user controls with custom properties. I can set those
properties on instances of my user controls, and I have programmed my user
control to do useful visual things in response to...
|
by: Frinavale |
last post by:
I'm using a VB6 library in my VB.NET application.
This library has a function that retrieves report records (as structures).
One of the structures in this library contains 105 public Boolean...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |