473,396 Members | 1,804 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,396 software developers and data experts.

TypeConverter question


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";

Therefore I need an implicit cast operator but this causes the loss of
further information that are hold in the MyColor object stored in
rect1.FillColor before. So I need to transfer further data than just a string
during conversion from the old object to the new object. Lets say the old
FillColor had a property called Remark with a value "Test-Remark". This
property would be empty after static implicit type conversion.

Is it possible to use the TypeConverter.CreateInstance method therefore? And
if yes, how exactly?

Users or other developers should be allowed to write rect1.FillColor =
"green" without calling the TypeConverter explicitly.

Thanks if anyone has help for me.

Matthias
Germany/Austria
Mar 16 '06 #1
1 1470
Matthias,

I don't agree that you should be able to specify the FillColor with a
string named "Red". I think that is bad design, since you have a strongly
typed static color instance on Colors named Red (i.e. Colors.Red).

Furthermore, implicit cast operators are bad news in cases like this
where there is not a direct correlation between the types. Int to long is
one thing, but string to color is not.

If you want to convert the Color instance to a string, use the
ColorConverter class, and it will properly serialize your color instance to
a string, and from a string back to the Color instance.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Matthias Heise" <Ma***********@discussions.microsoft.com> wrote in message
news:52**********************************@microsof t.com...

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";

Therefore I need an implicit cast operator but this causes the loss of
further information that are hold in the MyColor object stored in
rect1.FillColor before. So I need to transfer further data than just a
string
during conversion from the old object to the new object. Lets say the old
FillColor had a property called Remark with a value "Test-Remark". This
property would be empty after static implicit type conversion.

Is it possible to use the TypeConverter.CreateInstance method therefore?
And
if yes, how exactly?

Users or other developers should be allowed to write rect1.FillColor =
"green" without calling the TypeConverter explicitly.

Thanks if anyone has help for me.

Matthias
Germany/Austria

Mar 16 '06 #2

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

Similar topics

2
by: kw | last post by:
I'm getting different behavior from what I would expect and was hoping someone could clue me in. At run time I need to examine a property of an object for a custom TypeConverter, then use that...
5
by: ljlevend | last post by:
Is there a TypeConverter that converts Doubles to percent values in a PropertyGrid? The Windows.Forms.Form.Opacity property seems to use the TypeConverter that I want. Thank you, Lance
0
by: David J | last post by:
Hi, I am trying to use the property grid in an app I am creating. I am currently using the examply I got from the codeproject site with the propertybags, if that rings a bell. I have created a...
0
by: Alex Clark | last post by:
Hi all, Apologies for the cross-post but I can't determine if this is a VS .NET problem or a VB.NET language issue. I'm using .NET 1.1 SP1, VS 2003 EA, VB.NET. I'm coding a custom component...
4
by: Kent Boogaart | last post by:
*tried this already on the buildingcontrols newsgroup but didn't get a response* Hi, As far as I can tell, it is not possible to use the TypeConverter infrastructure with generic types. Say...
1
by: tony | last post by:
Hello! I find it hard to understand this about TypeConverter. I have read some documentation but still it's hard to understand. For example this row below. Can somebody write some small code...
4
by: swartzbill2000 | last post by:
Hello, I have a TypeConverter for converting between this Enum and Strings. Public Enum DeviceNameEnum dnNone dn2500 dnMirror End Enum 'DeviceNameEnum
11
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...
1
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...
6
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.