473,675 Members | 3,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Anyone experienced with FieldInfo.SetVa lue

Im trying to use FieldInfo.SetVa lue 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.Reflecti on.FieldInfo
Dim myType As Type = Me.GetType
' Get the type and fields of FieldInfoClass.
myFieldInfo =
myType.GetField s(Reflection.Bi ndingFlags.NonP ublic Or _
Reflection.Bind ingFlags.Instan ce Or
Reflection.Bind ingFlags.Public )

' Display the field information of FieldInfoClass.
Dim i As Integer
For i = 0 To myFieldInfo.Len gth - 1
Dim customatts() As Object =
myFieldInfo(i). GetCustomAttrib utes(True)
If customatts.Leng th > 0 Then
Dim fm As FieldMember = CType(customatt s(0),
FieldMember)
If htFields.Contai ns(fm.DbFieldNa me) Then
myFieldInfo(i). SetValue(me, htFields(fm.DbF ieldName))

End If
Next i

the error occurs on SetValue

Nov 21 '05 #1
1 1868
<jw*****@gmail. com> wrote in message news:11******** *************@f 14g2000cwb.goog legroups.com...
Object type cannot be converted to target type : : If htFields.Contai ns(fm.DbFieldNa me) Then
myFieldInfo(i). SetValue(me, htFields(fm.DbF ieldName))


It's because the Type retrieved from (Hashtable?) htFields with the
key fm.DbFieldName is not convertible to the Type of the field
whose FieldInfo you're using.

You may need to typecast, using CType( ) or CStr( ) or something,
or the types may just be incompatible (you can't put a String into a
field of type Integer - if the String happens to contain a number you
have to Parse it out manually, as an example).

Look at htFields( fm.DbFieldName) .GetType( ) and the type of
the field, and then try to convert between them if possible.
Derek Harmon
Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5813
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); }
3
3103
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 postings, and think the problem might have something to do with "boxing", and passing of structures...
5
3957
by: Dennis | last post by:
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"...
1
2195
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 hidden, is there anyway I can implement my own method if there is no equivalent?
0
8521
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8973
Oralloy
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8722
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4261
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4468
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2867
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2127
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.