I have an array "t" where each element is a structure of type "testing". I
want to use the FieldInfo class to set the values of the array element
fields. I can read the field properties and return the velue using this
technique but cannot set the field value as per below.
Why won't it change the value. If define "testing" as a class where col1
and col2 are fields as opposed to properties, it works fine. Just won't work
when "testing" is a structure.
Public Structure testing
Public col1 As String
Public col2 As Integer
End Structure
Private Sub starttest()
Dim t(1) As testing
t(0).col1 = "AAAA"
t(0).col2 = 16
'Try to reset the value of t(0) using FieldInfo.SetVa lue Method
set_value(t(0), "col1", "XXXXX")
'Runs ok but didn't change t(0).col1
End Sub
Private Sub set_value(ByVal obj As Object, ByVal fldname As String,
ByVal value As Object)
Dim sourcetype As Type = obj.GetType
Dim field As FieldInfo = sourcetype.GetF ield(fldname)
If Not field Is Nothing Then
field.SetValue( obj, value)
End If
End Sub
--
Dennis in Houston 5 3946
Dennis,
Why you want to use structures for this, for the "why not" you can search
this newsgroup, what Jay have endless written about this.
Your sample as this will run.
\\\
Public Sub starttest()
Dim t(0) As testing
t(0) = New testing
t(0).col1 = "AAAA"
t(0).col2 = 16
'Try to reset the value of t(0) using FieldInfo.SetVa lue Method
set_value(t(0), "col1", "XXXXX")
'Runs ok but didn't change t(0).col1
End Sub
Private Sub set_value(ByVal obj As Object, _
ByVal fldname As String, ByVal value As Object)
Dim sourcetype As Type = obj.GetType
Dim field As FieldInfo = sourcetype.GetF ield(fldname)
If Not field Is Nothing Then
field.SetValue( obj, value)
End If
End Sub
Public Class testing
Public col1 As String
Public col2 As Integer
End Class
However I stopped as well with your previous problem, you are trying in my
opinion to use endless late binding, what can in my opinion only lead to the
programs as I not want them.
Cor
Yes, I know it works with classes, not only properties in classes but fields
as well. It just won't work with Structures. As for why I want to do this,
I am working on a control that accepts various types of input from a user. I
believe C++ has generics but I don't know C++ and don't really want to learn
as VB is my choice of languages to learn.
"Cor Ligthert" wrote: Dennis,
Why you want to use structures for this, for the "why not" you can search this newsgroup, what Jay have endless written about this.
Your sample as this will run. \\\ Public Sub starttest() Dim t(0) As testing t(0) = New testing t(0).col1 = "AAAA" t(0).col2 = 16 'Try to reset the value of t(0) using FieldInfo.SetVa lue Method set_value(t(0), "col1", "XXXXX") 'Runs ok but didn't change t(0).col1 End Sub Private Sub set_value(ByVal obj As Object, _ ByVal fldname As String, ByVal value As Object) Dim sourcetype As Type = obj.GetType Dim field As FieldInfo = sourcetype.GetF ield(fldname) If Not field Is Nothing Then field.SetValue( obj, value) End If End Sub Public Class testing Public col1 As String Public col2 As Integer End Class
However I stopped as well with your previous problem, you are trying in my opinion to use endless late binding, what can in my opinion only lead to the programs as I not want them.
Cor
Cor, I've found the solution from Jay's previous replies to the newsgroup,
simply convert it to a value type. It works great.
Public Structure testing
Public col1 As String
Public col2 As Integer
End Structure
Private Sub starttest()
Dim t(1) As testing
t(0).col1 = "AAAA"
t(0).col2 = 16
'Try to reset the value of t(0) using FieldInfo.SetVa lue Method
set_value(t(0), "col1", "XXXXX")
'Runs ok but didn't change t(0).col1
End Sub
Private Sub set_value(ByVal obj As Object, ByVal fldname As String,
ByVal value As Object)
Dim cv1 as valuetype = Ctype(obj,Value Type)
Dim field As FieldInfo = cv1.GetType.Get Field(fldname)
If Not field Is Nothing Then
field.SetValue( obj, value)
End If
obj = cv1
End Sub
"Cor Ligthert" wrote: Dennis,
Why you want to use structures for this, for the "why not" you can search this newsgroup, what Jay have endless written about this.
Your sample as this will run. \\\ Public Sub starttest() Dim t(0) As testing t(0) = New testing t(0).col1 = "AAAA" t(0).col2 = 16 'Try to reset the value of t(0) using FieldInfo.SetVa lue Method set_value(t(0), "col1", "XXXXX") 'Runs ok but didn't change t(0).col1 End Sub Private Sub set_value(ByVal obj As Object, _ ByVal fldname As String, ByVal value As Object) Dim sourcetype As Type = obj.GetType Dim field As FieldInfo = sourcetype.GetF ield(fldname) If Not field Is Nothing Then field.SetValue( obj, value) End If End Sub Public Class testing Public col1 As String Public col2 As Integer End Class
However I stopped as well with your previous problem, you are trying in my opinion to use endless late binding, what can in my opinion only lead to the programs as I not want them.
Cor
Dennis,
I have the idea that you are busy with this, I think that probably Charles
Law is as well busy with something as this, so when you search this
newsgroup for that name you will find probably as well a lot of stuff you
can use. However I don't like this kind of challenges anymore.
Cor
"Dennis" <De****@discuss ions.microsoft. com> Yes, I know it works with classes, not only properties in classes but fields as well. It just won't work with Structures. As for why I want to do this, I am working on a control that accepts various types of input from a user. I believe C++ has generics but I don't know C++ and don't really want to learn as VB is my choice of languages to learn.
"Cor Ligthert" wrote:
Dennis,
Why you want to use structures for this, for the "why not" you can search this newsgroup, what Jay have endless written about this.
Your sample as this will run. \\\ Public Sub starttest() Dim t(0) As testing t(0) = New testing t(0).col1 = "AAAA" t(0).col2 = 16 'Try to reset the value of t(0) using FieldInfo.SetVa lue Method set_value(t(0), "col1", "XXXXX") 'Runs ok but didn't change t(0).col1 End Sub Private Sub set_value(ByVal obj As Object, _ ByVal fldname As String, ByVal value As Object) Dim sourcetype As Type = obj.GetType Dim field As FieldInfo = sourcetype.GetF ield(fldname) If Not field Is Nothing Then field.SetValue( obj, value) End If End Sub Public Class testing Public col1 As String Public col2 As Integer End Class
However I stopped as well with your previous problem, you are trying in my opinion to use endless late binding, what can in my opinion only lead to the programs as I not want them.
Cor
Dennis,
I need your help, your example doesn?t work for me. After the set_value sub the value still "AAAA".
Thanks in advance
--
Message posted via http://www.dotnetmonster.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: James Hadwen |
last post by:
Hi,
I'm trying to do something with reflection that's a little wierd. The idea
is that I call for a field to be updated on a base class. Using reflection
it first updates the value, and then updates the change that has been made
to a dictionary. The code below is just to encourage some ideas, if you look
at most of the code you find that...
|
by: Glen |
last post by:
FieldInfo.SetValue() doesn't appear to work when trying to set the values of fields of a struct. Why? Because structs are passed by value and not reference?
--
Thanks,
Glen
|
by: ORC |
last post by:
I will use FieldInfo to import values from a XML file into the fields of an
objects. But how to convert to the proper type like in this:
public void Load(object MyClass)
{
foreach( FieldInfo field in MyClass.GetType().GetFields() )
{
field.SetValue(MyClass,
String_From_XML_Representing_The_Field_Value);
}
|
by: Laurie |
last post by:
Hi,
I need to be able to manipulate field values within a structure using
FieldInfo.GetValue and FieldInfo.SetValue, in VB.Nt 2003. The GetValue
is working fine and makes it really easy for me to maintain my code.
Unfortunately, SetValue doesn't return any errors but doesn't actually
set the value.
I have read through various newsgroup...
|
by: Brent |
last post by:
I have variables in a structure loaded into a list box.
I thought I could use FieldInfo.SetValue to update the items value when the
user clicks on it, but it is not working.
..
..
..
Dim fi As Reflection.FieldInfo
fi = tp.GetField(Item)
MsgBox(fi.GetValue(InItems)) 'returns the original value
| |
by: jw56578 |
last post by:
Im trying to use FieldInfo.SetValue to set the values of fields in a
class, the class is using reflection on itself to popluate its own
fields. but i keep getting this error.
Object type cannot be converted to target type
here is code
Dim myFieldInfo() As System.Reflection.FieldInfo
Dim myType As Type = Me.GetType
' Get the type and...
|
by: David Gouge |
last post by:
Posting on behalf of a colleague (no, really! ;) )...
Hi,
I was wondering if there is any equivalent of FieldInfo.SetValue(Object,
Object) that is strongly typed or uses generics? I have been looking at
the source for FieldInfo to see if I could implement my own generic
version of this method but unfortunately the code seems to be...
|
by: kernell |
last post by:
Hi everyone,
i'm trying to initialize all control of a UserControl to is appropriate class. The value is set but the event won't raise anymore. See the example:
'Not working
'Normally WithEvents should raise the event but nothing happen
Public Class Process
Inherits SkinProcess
|
by: damiensawyer |
last post by:
Hi,
I need to invoke .getvalue and .setvalue on fieldinfo and propetyinfo
objects. Even though they're both derived from MemberInfo, they don't
share those two methods. I've finding myself writing the following
types of structure over and over. Is there something I'm missing? Or
some 'funky' 3.5 delegate/lambda expression way of doing this?...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |