471,319 Members | 3,299 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 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 5975
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tony Tullemans | last post: by
2 posts views Thread by Hugo Leonardo | last post: by
1 post views Thread by Paul | last post: by
reply views Thread by tatilou | last post: by
9 posts views Thread by FourEyes | last post: by
1 post views Thread by damiensawyer | last post: by

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.