473,249 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,249 software developers and data experts.


I am trying to save/retreive some settings from a database using
reflection an my custom attributes. The field in teh database is a
string, but my property is a boolen. I have the following code:

pi.SetValue(settingsObject, GetSettingFromDb(attribute.SettingName),
When I run this, I receive the following error:

Object of type 'System.String' cannot be converted to type

I've not yet figured out how to determine if the property is a Boolean
thus have to parse the string that comes back, or if there is a way to
use SetValue and have it do this for me.

Any help would be appreciated.

Dec 10 '05 #1
3 6157
Hi there,

I've had this issue to, and as I recall I used a TypeConverter to
interpret the string value to the proper type. Property's can be
assigned TypeConverters through their attributes, so in this situation
you'll need to implement a TypeConverter capable of converting a String
to a Boolean value.

Once Implemented it would look something like this ...

<TypeConverter(GetType(PointConverter))> _
Public Property MyLocation() As Point
Hope this helps... start looking into TypeConverter first.


Dec 10 '05 #2
I found that in System.ComponentModel there is a BooleanConverter and a
StringConverter, I have tried adding both of these to my property with
the TypeconvertorAttribute, but no luck.

I have also the following code, and it works, but is not what I want as
it is limited to ONLY the boolean conversion, or to a big IF statement
checking the types and creating the correct convertor. I'd like to have
it be able to use any of the type convertors based on the property.
BooleanConverter temp = new BooleanConverter();
pi.SetValue(settings, temp.ConvertFrom(value), null);

Given the previous post I would expected the following to have worked:


but when I call pi.SetValue I still receive the "Object of type
'System.String' cannot be converted to type
'System.Boolean'." exception.

Any info on what I'm doing wrong?


Dec 11 '05 #3
Hey Wayne,

You are correct about the "[TypeConverter(TypeOf(BooleanConverter))]"
not working.

I am assuming from your code snippets that you are using
PropertyDescriptors and the like. It seems that calling "SetValue"
bypasses those TypeConverter attributes...sorry about that. Anyway...i
wrote up some simple code to simulate your problem and it worked. I
did it using PropertyDescriptors and the BooleanConverter.

btw, I am using VS2003.

Here is the code, I hope this may help you a bit. If this doesnt help
you still, could you post some snippets of your code?

good luck...

//********************* START CODE ************************\\
using System;
using System.ComponentModel;

namespace CS_TypeConversion

public class Person
private bool m_IsTall;

public bool IsTall
return m_IsTall;
m_IsTall = value;

static void Main(string[] args)
//Create a Person "record"
Person personRecord = new Person();
personRecord.IsTall = false;

//Print original value
Console.WriteLine("IsTall : " + personRecord.IsTall.ToString());

//Create our converter
BooleanConverter bc = new BooleanConverter();

//Search for the property
PropertyDescriptorCollection pdc =
TypeDescriptor.GetProperties(personRecord.GetType( ));
PropertyDescriptor pd = pdc.Find("IsTall", true);

//Set the property using the converter.
pd.SetValue(personRecord, bc.ConvertFromString("true"));

//Print new value
Console.WriteLine("IsTall : " + personRecord.IsTall.ToString());


Dec 11 '05 #4

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

Similar topics

by: Tony Tullemans | last post by:
I am trying to write a subroutine that will examine all the properties of a form to determine which of those are SqlCommand objects, and then set the Connection property of those SqlCommands to a...
by: Hugo Leonardo | last post by:
I'll big problem. I have tree class: public Class Column { public Column {...} string _Value; public bool HasValue = false;
by: Bob | last post by:
This works only when the property return type is string: SomeObjectsPropertyInfo.SetValue(SomeObject, SomeStringValue, Nothing) Is there a good way to try to automatically cast the string value...
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: Paul | last post by:
This part is easy: Dim oControl As Control oControl = Me.Page.FindControl("TextBox1") Dim oPropertyInfo As PropertyInfo oPropertyInfo = oControl.GetType.GetProperty("Text") ...
by: tatilou | last post by:
Hello, I need add new method to my properties. these methods should give me some attributes coming from my database. I think, to create a class with implement PropertyInfo but I don't know...
by: Carlos Rodriguez | last post by:
I have the following function in C#public void Undo(IDesignerHost host) { if (!this.componentName.Equals(string.Empty) && (this.member != null)) { IContainer container1 = (IContainer)...
by: FourEyes | last post by:
I've read that Reflection is slow and shouldn't be used when performance is a concern. In my case, performance is definitely a concern, so I was thinking that I could collect the PropertyInfo...
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...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.