By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,775 Members | 1,345 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,775 IT Pros & Developers. It's quick & easy.

TypeConverter::ConvertFrom ignores implicit cast

P: n/a
I have an Angle class that I store angles in. It's basically just a
bunch of fancy functions for manipulating a double. It has implicit
casts for converting to/from double. Due to the project structure and
where it stores the preferred SI units for display, I wanted to have
the TypeConverter in a different project that has no reference to the
angle class. However, returning a double from the ConvertFrom function
throws an error that it can't convert from "System.Double" to my Angle
type. Originally, before the SI units where unaccessable, I had this
in there:

double angle;
...
if (context.PropertyDescriptor.PropertyType.Equals(ty peof(Angle)))
return new Angle(angle);
return angle;

Can I do something similar even if I don't have a reference to the
Angle type? Why would it not accept my implicit cast to double?

Feb 2 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi,

The problem is that the ConvertFrom method returns an object, not a double.
The runtime will only call your implicit cast operator overload for double
if the value being cast isn't boxed.

Unfortunately, there isn't much you can do here since you can't overload the
implicit cast operator from Object.

Your best bet is to make the Angle class, which BTW should probably be a
struct, visible to the TypeConverter class that requires it. It's strange
to separate them unless you're planning on using the same TypeConverter
class to convert values other than just your Angle type, but that wouldn't
make much sense either.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"not_a_commie" <no********@gmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
>I have an Angle class that I store angles in. It's basically just a
bunch of fancy functions for manipulating a double. It has implicit
casts for converting to/from double. Due to the project structure and
where it stores the preferred SI units for display, I wanted to have
the TypeConverter in a different project that has no reference to the
angle class. However, returning a double from the ConvertFrom function
throws an error that it can't convert from "System.Double" to my Angle
type. Originally, before the SI units where unaccessable, I had this
in there:

double angle;
...
if (context.PropertyDescriptor.PropertyType.Equals(ty peof(Angle)))
return new Angle(angle);
return angle;

Can I do something similar even if I don't have a reference to the
Angle type? Why would it not accept my implicit cast to double?

Feb 3 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.