Hi Kerry,
The .NET Framework provides the following two mechanisms for converting
user-defined data types (custom types) to other data types:
1. Extending TypeConverter class
2. Implementing the System.IConvert ible interface
For more information about the comparison of these 2 mechanisms, please
refer to:
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpcongeneralize dtypeconversion .asp
For your problem about extend TypeConverter class, there is a general
article talks about implement of TypeConverter's members, please refer to :
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconimplementi ngtypeconverter .asp
Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
| From: Kerry Sanders <di****@NOSPAMy ahoo.com>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| Subject: Issue implementing type converter
| Reply-To:
di****@NOSPAMya hoo.com
| Message-ID: <if************ *************** *****@4ax.com>
| X-Newsreader: Forte Agent 1.93/32.576 English (American)
| MIME-Version: 1.0
| Content-Type: text/plain; charset=us-ascii
| Content-Transfer-Encoding: 7bit
| Lines: 69
| X-Trace:
ldjgbllpbapjglp pdbdpiflmbceked mfhojhikkbagflh cbofachioobchmj nfogjaaegiopdje e
lgcighdcopgoocd mbhomolbaoecanc gepjpmkamohehno hkmchjekkahbepa apmgfphnfhmnjcl d
kjelblnp
| NNTP-Posting-Date: Sat, 25 Oct 2003 23:08:20 EDT
| Date: Sat, 25 Oct 2003 22:10:31 -0500
| Path:
cpmsftngxa06.ph x.gbl!cpmsftngx a09.phx.gbl!TK2 MSFTNGP08.phx.g bl!newsfeed00.s u
l.t-online.de!t-online.de!news-lei1.dfn.de!new sfeed.freenet.d e!newsfeed.news
2me.com!newsfee d2.easynews.com !newsfeed1.easy news.com!easyne ws.com!easynews !
bigfeed2.bellso uth.net!bigfeed .bellsouth.net! bignumb.bellsou th.net!news.bel l
south.net!bigne ws5.bellsouth.n et.POSTED!not-for-mail
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1941 02
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
|
| I am working on a project for work where I need a specialized type
| converter to convert the value of a string which is edited in a grid
| back to the underlying object type from the cell. The value in the
| cell is displayed as a string from the ToString() method in the class.
|
| Anyway, I am trying to implement my converter, but I am having a
| little trouble understanding it fully and how exactly to implement
| some of the methods, especially the ConvertFrom and whether or not I
| have properly implemented the CreateInstance method.
|
| I have written some code below. This code is not the actual code, but
| it gives a good representation of what I am trying to do.
|
| Can someone give me some help on this?
|
|
| public class PizzaConverter : ExpandableObjec tConverter
| {
| public override bool
| GetCreateInstan ceSupported(ITy peDescriptorCon text context)
| {
| return true;
| }
|
| public override object CreateInstance( ITypeDescriptor Context
| context, IDictionary propertyValues)
| {
| return new Pizza((string)p ropertyValues["Maker"],
| (int)propertyVa lues["NumberOfPizzas "]);
| }
|
| public override bool CanConvertFrom( ITypeDescriptor Context
| context, Type sourceType)
| {
| if (sourceType == typeof(string))
| return true;
| return base.CanConvert From (context, sourceType);
| }
|
| public override object ConvertFrom(ITy peDescriptorCon text
| context, System.Globaliz ation.CultureIn fo culture, object value)
| {
| }
| }
|
| [TypeConverter(t ypeof(PizzaConv erter))]
| public class Pizza
| {
| public Pizza(string PizzaMaker, int NumberOrdered)
| {
| Maker = PizzaMaker;
| NumberOfPizzas = NumberOrdered;
| }
|
| private string sMaker = "";
| public string Maker
| {
| get { return this.sMaker; }
| set { this.sMaker = value; }
| }
|
| private int mNumberOfPizzas = 0;
| public int NumberOfPizzas
| {
| get { return this.mNumberOfP izzas; }
| set { this.mNumberOfP izzas = value; }
| }
| }
|
|