Issue implementing type converter

K

Kerry Sanders

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; }
}
}
 
J

Jeffrey Tan[MSFT]

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/default.asp?url=/library/en-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/default.asp?url=/library/en-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 <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: Issue implementing type converter
| Reply-To: (e-mail address removed)
| Message-ID: <[email protected]>
| 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:
ldjgbllpbapjglppdbdpiflmbcekedmfhojhikkbagflhcbofachioobchmjnfogjaaegiopdjee
lgcighdcopgoocdmbhomolbaoecancgepjpmkamohehnohkmchjekkahbepaapmgfphnfhmnjcld
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!TK2MSFTNGP08.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!bignumb.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; }
| }
| }
|
|
 
K

Kerry Sanders

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.
 
J

Jeffrey Tan[MSFT]

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/default.asp?url=/library/en-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 <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: Re: Issue implementing type converter
| Reply-To: (e-mail address removed)
| Message-ID: <[email protected]>
| References: <[email protected]>
<[email protected]>
| 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!newsfeed00.sul.t-online.de!t-onlin
e.de!newsfeed.freenet.de!194.168.4.91.MISMATCH!newspeer1-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, (e-mail address removed) ("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:
|
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 :
|
m
| >l/cpconimplementingtypeconverter.asp
| >
| >Hope this helps,
| >
| >Best regards,
| >Jeffrey Tan
| >Microsoft Online Partner Support
|
|
 
J

Jeffrey Tan[MSFT]

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" <[email protected]>
| Sender: "Kerry Sanders" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Issue implementing type converter
| Date: Tue, 28 Oct 2003 07:08:00 -0800
| Lines: 4
| Message-ID: <[email protected]>
| 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.
|
|
 
K

Kerry Sanders

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.
 
J

Jeffrey Tan[MSFT]

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 <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: Re: Issue implementing type converter
| Reply-To: (e-mail address removed)
| Message-ID: <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| 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:
npbhgpngjbkmjfegdbdpiflmbcekedmfhojhikkbagflhcboepmgcnocambflkllicabflmaplme
clooghdcopgoocdmbhompfaamkfakhgalebkbgjhmnnmpgicogmacelkjlllgbdgjlhhnimjijee
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!TK2MSFTNGXA05.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.bellsouth.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.
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top