473,569 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TypeConverter:: ConvertFrom ignores implicit cast

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.Dou ble" to my Angle
type. Originally, before the SI units where unaccessable, I had this
in there:

double angle;
...
if (context.Proper tyDescriptor.Pr opertyType.Equa ls(typeof(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
1 3186
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_comm ie" <no********@gma il.comwrote in message
news:11******** **************@ a75g2000cwd.goo glegroups.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.Dou ble" to my Angle
type. Originally, before the SI units where unaccessable, I had this
in there:

double angle;
...
if (context.Proper tyDescriptor.Pr opertyType.Equa ls(typeof(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
12737
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
1
1691
by: Sky Sigal | last post by:
(PS: Cross post from microsoft.pulic.dotnet.framework.aspnet.webcontrols) I've been looking lately for a way to keep the Properties panel for Controls 'clean'... My goal is to keep similar public properties of a custom Control neatly tied together -- rather than all over the IDE. One such set of values that will rarely be changed, so...
0
1202
by: Eric Hoch | last post by:
Hello, I'm writing a TypeConverter for a class which inherits arraylist, but deserializing it always results in an arraylist, which causes an InvalidCastException. If anyone can point out what I'm doing wrong here, I'd really appreciate it. The class: <TypeConverter(GetType(CXMLObjectListConverter))> _
0
1277
by: R. Nachtsturm | last post by:
Hi, I would like to create a workable ExpandableObjectConverter for a String Collection (class inheriting from CollectionBase for example and only accepting/returning Strings via Add/Remove/Item/DefaultProprty) and a fitting Descriptor (implementing ICustomTypeDescriptor)... now i created it and it somewhat works.. my big problem is that i...
1
1477
by: Matthias Heise | last post by:
Hello everybody, I have a problem towards the following topic. There is a class like MyColor and a class like MyRectangle that has a property called FillColor which is of type MyColor. Now it should be able to say MyRectangle rect1 = new MyRectangle(); rect1.FillColor = "Red";
5
5722
by: Karsten Schramm | last post by:
Hi, what kind of object has this method to return? Generally the MSDN says return a list of the original data types. If it comes to an example the instruction says return a list of strings. But then the code example (in most cases an array of ints) returns a list of ints. Unfortunately ints are implicitly castable to string. So let's...
11
2326
by: Rolf Welskes | last post by:
Hello, the problem seems to be complex and is in all developments of web-controls which uses own TypeConverter. For this I have here a simple demo-program of the problem: The Control-code: A class MyString which is a class which is similar to a string public class MyString {
1
2148
by: --== Alain ==-- | last post by:
Hi, I 'm facing an interesting issue regarding a property and its TypeConverter. When i do not attach a TypeConverter to this property, all custom properties of my custom control are displayed in Test Container. Since i attached this TypeConverter to my property, ALL custom properties (properties implemented by myself) are not displayed...
6
3883
by: Larry Smith | last post by:
Hi there, Can anyone provide any insight on why MSFT introduced "TypeConverter.GetProperties()". There are two classes for dealing with metadata in .NET, 'Type" and "TypeDescriptor". Each has a "GetProperites()" method so why complicate the situation even more than it already is by adding a "GetProperties()" method to "TypeConverter". The...
0
7618
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...
0
7926
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. ...
1
7679
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...
0
7983
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...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
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...
0
3657
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...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
946
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...

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.