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

Issue implementing type converter

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 : ExpandableObjectConverter
{
public override bool
GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}

public override object CreateInstance(ITypeDescriptorContext
context, IDictionary propertyValues)
{
return new Pizza((string)propertyValues["Maker"],
(int)propertyValues["NumberOfPizzas"]);
}

public override bool CanConvertFrom(ITypeDescriptorContext
context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom (context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext
context, System.Globalization.CultureInfo culture, object value)
{
}
}

[TypeConverter(typeof(PizzaConverter))]
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.mNumberOfPizzas; }
set { this.mNumberOfPizzas = value; }
}
}

Nov 15 '05 #1
6 5621

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.IConvertible interface
For more information about the comparison of these 2 mechanisms, please
refer to:
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpcongeneralizedtypeconversion.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/cpconimplementingtypeconverter.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****@NOSPAMyahoo.com>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: Issue implementing type converter
| Reply-To: di****@NOSPAMyahoo.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:
ldjgbllpbapjglppdbdpiflmbcekedmfhojhikkbagflhcbofa chioobchmjnfogjaaegiopdjee
lgcighdcopgoocdmbhomolbaoecancgepjpmkamohehnohkmch jekkahbepaapmgfphnfhmnjcld
kjelblnp
| NNTP-Posting-Date: Sat, 25 Oct 2003 23:08:20 EDT
| Date: Sat, 25 Oct 2003 22:10:31 -0500
| Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTN GP08.phx.gbl!newsfeed00.su
l.t-online.de!t-online.de!news-lei1.dfn.de!newsfeed.freenet.de!newsfeed.news
2me.com!newsfeed2.easynews.com!newsfeed1.easynews. com!easynews.com!easynews!
bigfeed2.bellsouth.net!bigfeed.bellsouth.net!bignu mb.bellsouth.net!news.bell
south.net!bignews5.bellsouth.net.POSTED!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:194102
| X-Tomcat-NG: microsoft.public.dotnet.languages.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 : ExpandableObjectConverter
| {
| public override bool
| GetCreateInstanceSupported(ITypeDescriptorContext context)
| {
| return true;
| }
|
| public override object CreateInstance(ITypeDescriptorContext
| context, IDictionary propertyValues)
| {
| return new Pizza((string)propertyValues["Maker"],
| (int)propertyValues["NumberOfPizzas"]);
| }
|
| public override bool CanConvertFrom(ITypeDescriptorContext
| context, Type sourceType)
| {
| if (sourceType == typeof(string))
| return true;
| return base.CanConvertFrom (context, sourceType);
| }
|
| public override object ConvertFrom(ITypeDescriptorContext
| context, System.Globalization.CultureInfo culture, object value)
| {
| }
| }
|
| [TypeConverter(typeof(PizzaConverter))]
| 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.mNumberOfPizzas; }
| set { this.mNumberOfPizzas = value; }
| }
| }
|
|

Nov 15 '05 #2
Thanks for your reply, Jeffrey. I had already read both of these articles from
the October MSDN DVD that I have. Unfortunately, they are what led to my
original question. The examples are a bit limited and do not fully explain how
to implement and use the CreateInstance method from the TypeConverter class,
which was my original question.

What I need to do, which I may have not fully made understandable before was to
take a string value that I get after a user edits a cell in a grid and then
update the object which is associated with that cell. Well, I say associated,
but it is not directly associated. I am using the DevExpress XtraGrid which
allows me to throw object classes at the columns and it will use the ToString()
method if it is overriden. In my case, that is what I is taking place.

Once the string is updated in the grid, I need to update the original object
which contains that string. From what I have read so far, the CreateInstance is
the way to go if you need to converter to a user type from a value type such as
string, but I am at a loss, unfortunately.


On Mon, 27 Oct 2003 06:46:25 GMT, v-*****@online.microsoft.com ("Jeffrey
Tan[MSFT]") wrote:

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.IConvertible interface
For more information about the comparison of these 2 mechanisms, please
refer to:
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpcongeneralizedtypeconversion.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/cpconimplementingtypeconverter.asp

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support


Nov 15 '05 #3

Hi Kerry,

Thanks for you feedback.
TypeConverter.CreateInstance method take IDictionary paramter which has
series of name/value pairs, one for each property returned from
GetProperties.
So I think you should get every property from this parameter and cast it
into its actual type, then you can re-create your instance through the
constructor.
I think your original virtual code goes the correct way, is there anything
wrong with your orignal code?

Beside, there is a sample refer to how to use TypeConverter.CreateInstance
in the article below, you can refer to its "In Through the Out Door:
Persistence Through Code" section:
http://msdn.microsoft.com/library/de...us/dndotnet/ht
ml/pdc_vsdescmp.asp

Hope this helps,
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****@NOSPAMyahoo.com>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: Re: Issue implementing type converter
| Reply-To: di****@NOSPAMyahoo.com
| Message-ID: <4j********************************@4ax.com>
| References: <if********************************@4ax.com>
<7N**************@cpmsftngxa06.phx.gbl>
| 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: 47
| Date: Mon, 27 Oct 2003 18:42:22 -0600
| NNTP-Posting-Host: 68.17.162.138
| X-Trace: bignews4.bellsouth.net 1067301699 68.17.162.138 (Mon, 27 Oct
2003 19:41:39 EST)
| NNTP-Posting-Date: Mon, 27 Oct 2003 19:41:39 EST
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-onlin
e.de!newsfeed.freenet.de!194.168.4.91.MISMATCH!new speer1-gui.server.ntli.net
!ntli.net!peer01.cox.net!cox.net!bigfeed.bellsouth .net!bignumb.bellsouth.net
!news.bellsouth.net!bignews4.bellsouth.net.POSTED! not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:194567
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks for your reply, Jeffrey. I had already read both of these
articles from
| the October MSDN DVD that I have. Unfortunately, they are what led to my
| original question. The examples are a bit limited and do not fully
explain how
| to implement and use the CreateInstance method from the TypeConverter
class,
| which was my original question.
|
| What I need to do, which I may have not fully made understandable before
was to
| take a string value that I get after a user edits a cell in a grid and
then
| update the object which is associated with that cell. Well, I say
associated,
| but it is not directly associated. I am using the DevExpress XtraGrid
which
| allows me to throw object classes at the columns and it will use the
ToString()
| method if it is overriden. In my case, that is what I is taking place.
|
| Once the string is updated in the grid, I need to update the original
object
| which contains that string. From what I have read so far, the
CreateInstance is
| the way to go if you need to converter to a user type from a value type
such as
| string, but I am at a loss, unfortunately.
|
|
|
|
| On Mon, 27 Oct 2003 06:46:25 GMT, v-*****@online.microsoft.com ("Jeffrey
| Tan[MSFT]") wrote:
|
| >
| >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.IConvertible interface
| >For more information about the comparison of these 2 mechanisms, please
| >refer to:
|
http://msdn.microsoft.com/library/de...-us/cpguide/ht m
| >l/cpcongeneralizedtypeconversion.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/ht

m
| >l/cpconimplementingtypeconverter.asp
| >
| >Hope this helps,
| >
| >Best regards,
| >Jeffrey Tan
| >Microsoft Online Partner Support
|
|

Nov 15 '05 #4

Hi Kerry,

Is your problem resolved?
If you still have any question, please feel free to tell me.

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.

--------------------
| Content-Class: urn:content-classes:message
| From: "Kerry Sanders" <di****@NOSPAMyahoo.com>
| Sender: "Kerry Sanders" <di****@NOSPAMyahoo.com>
| References: <if********************************@4ax.com>
<7N**************@cpmsftngxa06.phx.gbl>
<4j********************************@4ax.com>
<AA**************@cpmsftngxa06.phx.gbl>
| Subject: Re: Issue implementing type converter
| Date: Tue, 28 Oct 2003 07:08:00 -0800
| Lines: 4
| Message-ID: <0e****************************@phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcOdZUbgSAmuxLo6QZSC3BtRrMjU+g==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:194721
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks for the reply, Jeffrey. I will take a look at the
| IDictionary interface a little more and check out the
| article that you referenced.
|
|

Nov 15 '05 #5
I have not had a chance to look at this situation yet, Jeffrey. Other things
have diverted my attention the past week or so. I hope to look at it again over
the next few days.

Thanks.

Nov 15 '05 #6

Hi Kerry,

Thanks for your reply.
If you still have any question after research, please feel free to let me
know.

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****@NOSPAMyahoo.com>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: Re: Issue implementing type converter
| Reply-To: di****@NOSPAMyahoo.com
| Message-ID: <me********************************@4ax.com>
| References: <if********************************@4ax.com>
<7N**************@cpmsftngxa06.phx.gbl>
<4j********************************@4ax.com>
<AA**************@cpmsftngxa06.phx.gbl>
<0e****************************@phx.gbl>
<XO**************@cpmsftngxa06.phx.gbl>
| 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: 6
| X-Trace:
npbhgpngjbkmjfegdbdpiflmbcekedmfhojhikkbagflhcboep mgcnocambflkllicabflmaplme
clooghdcopgoocdmbhompfaamkfakhgalebkbgjhmnnmpgicog macelkjlllgbdgjlhhnimjijee
fldcoilp
| NNTP-Posting-Date: Tue, 11 Nov 2003 23:57:13 EST
| Date: Tue, 11 Nov 2003 22:58:02 -0600
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGXA06.phx.gbl!TK2MSFT NGXA05.phx.gbl!TK2MSFTNGP0
8.phx.gbl!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!
newsfeed.arcor-online.net!npeer.de.kpn-eurorings.net!news-xfer.cox.net!peer0
1.cox.net!cox.net!bigfeed.bellsouth.net!bignumb.be llsouth.net!news.bellsouth
..net!bignews4.bellsouth.net.POSTED!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:198583
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I have not had a chance to look at this situation yet, Jeffrey. Other
things
| have diverted my attention the past week or so. I hope to look at it
again over
| the next few days.
|
| Thanks.
|
|

Nov 15 '05 #7

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

Similar topics

5
by: jorfei | last post by:
I have written a component with a property IPAdrress of type System.Net.IPAddress. To ease the configuration of the component at design time, I have written a type converter for the type...
0
by: Jose Walker | last post by:
Hello everybody.... I have a big question... I could manage to generate an XML document thru the dxxGenXmlClob stored procedure... My DAD File seems to be ok... (herewith --> see below), and...
2
by: Claire | last post by:
I want to pass the type of an object as a field in an object. public class FileEventArgs : EventArgs { public object Data = null; public Type ClassType = null;
17
by: nicolas.hilaire | last post by:
Hi all, i've read this article http://msdn2.microsoft.com/en-us/library/85af44e9.aspx who first interest me much. I've translated it to use generic instead of template : generic < typename ...
8
by: bonk | last post by:
When I have an instance of an object wich is of type System.Type, how can I find out if it directly or indirecly derives from another type? myTypeInstance == typeof(AnotherType) only seems to...
3
by: AAJ | last post by:
Hi has anyone come across a function to check if a particular string can be safely converted to a datatype i.e. i would like to check things like TypeCheck("1/1/2006",datetime) -returns...
3
by: Vinz | last post by:
Hello everyone, My question is part C# and part C++. Please don't kill me for bringing C# inhere :-) Also please don't kill me for asking a n00b question, I'm still new to C++/CLI and C#. I...
14
by: Ellipsis | last post by:
Ok so I am converting to hexadecimal from decimal and I can only cout the reverse order of the hexadecimal?! How could I reverse this so its the right order? Heres my code: #include <iostream>...
1
by: HarishAdea | last post by:
Hi, I am trying to run the JAVA pgm, but it is giving error as "selection does not contain a main type". The filename is "ScoreLeadSummary.java" when i try to run it or debug,it gives the pop...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.