473,738 Members | 10,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.C reateInstance 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 1483
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.co m

"Matthias Heise" <Ma***********@ discussions.mic rosoft.com> wrote in message
news:52******** *************** ***********@mic rosoft.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.C reateInstance 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
3420
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 converter on a value. Here's the deal: object attrs=(object)PropertyInfo.GetCustomAttributes(typeof(TypeConverterAttribu te),false);
5
2187
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
1287
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 typeconverter that converts a string in to a dropdown list, a common issue. I am now trying to get this list filled with options coming out of my database instead of having to hard code them in the code on the class side as for instance is done in...
0
1195
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 which is designed to be dropped onto a form and have it's properties manipulated at runtime. I have a custom implementation of the TypeConverter class called UniversalTypeConverter which inherits from ExpandableObjectConverter, and for the most...
4
2177
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 you have this type: public struct Id<T> where T : IComparable { ... }
1
1367
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 where it is used so I hopefully understand it better. TypeConverter IntConverter = TypeDescriptor.GetConverter(typeof(Int32));
4
1701
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
2345
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
2158
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 in Test Container.
6
3892
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 class is supposed to be for converting types so "GetProperties()" seems misplaced and redundant (as...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8210
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.