473,725 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Nov 15 '05 #1
6 5664

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

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.mi crosoft.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.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


Nov 15 '05 #3

Hi Kerry,

Thanks for you feedback.
TypeConverter.C reateInstance 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.C reateInstance
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.as p

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****@NOSPAMy ahoo.com>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| Subject: Re: Issue implementing type converter
| Reply-To: di****@NOSPAMya hoo.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.bellso uth.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.ph x.gbl!TK2MSFTNG P08.phx.gbl!new sfeed00.sul.t-online.de!t-onlin
e.de!newsfeed.f reenet.de!194.1 68.4.91.MISMATC H!newspeer1-gui.server.ntli .net
!ntli.net!peer0 1.cox.net!cox.n et!bigfeed.bell south.net!bignu mb.bellsouth.ne t
!news.bellsouth .net!bignews4.b ellsouth.net.PO STED!not-for-mail
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1945 67
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.mi crosoft.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.IConvert ible 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/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/ht

m
| >l/cpconimplementi ngtypeconverter .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****@NOSPAMy ahoo.com>
| Sender: "Kerry Sanders" <di****@NOSPAMy ahoo.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: AcOdZUbgSAmuxLo 6QZSC3BtRrMjU+g ==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| Path: cpmsftngxa06.ph x.gbl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1947 21
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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****@NOSPAMy ahoo.com>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| Subject: Re: Issue implementing type converter
| Reply-To: di****@NOSPAMya hoo.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:
npbhgpngjbkmjfe gdbdpiflmbceked mfhojhikkbagflh cboepmgcnocambf lkllicabflmaplm e
clooghdcopgoocd mbhompfaamkfakh galebkbgjhmnnmp gicogmacelkjlll gbdgjlhhnimjije e
fldcoilp
| NNTP-Posting-Date: Tue, 11 Nov 2003 23:57:13 EST
| Date: Tue, 11 Nov 2003 22:58:02 -0600
| Path:
cpmsftngxa06.ph x.gbl!TK2MSFTNG XA06.phx.gbl!TK 2MSFTNGXA05.phx .gbl!TK2MSFTNGP 0
8.phx.gbl!newsf eed00.sul.t-online.de!newsf eed01.sul.t-online.de!t-online.de!
newsfeed.arcor-online.net!npee r.de.kpn-eurorings.net!n ews-xfer.cox.net!pe er0
1.cox.net!cox.n et!bigfeed.bell south.net!bignu mb.bellsouth.ne t!news.bellsout h
..net!bignews4. bellsouth.net.P OSTED!not-for-mail
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1985 83
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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
2937
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 System.Net.IPAddress. The type System.Net.IPAddress, as you know, is provided by Microsoft and I have no choice but to apply the type converter on the IPAddress property of the component itself (instead of the type System.Net.IPAddress). All normal...
0
1453
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 it works! but not in the way I expect, I explain myself: 1. The returned XML is not properly sorted as I've especified in the ORDER BY clause in the SQL_stmt section.
2
1586
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
2312
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 T, typename U > Boolean isinst(U u) {
8
2378
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 check if they are ultimately the same type (at the very bottom of the inheritance hirarchy) but in case myTypeInstance is "TypeDerivedFromAnotherType" the above evaluates to false.. myTypeInstance.BaseType == typeof(AnotherType)
3
5807
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 true TypeCheck("fred",datetime) -returns false
3
1098
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 have an assembly written in C++/CLI with a public function: int MyClass::MyFunction(String ^x) {
14
3185
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> using namespace std; void binary(int number) { int remainder; if(number <= 1)
1
13303
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 up as "selection does not contain a main type" Please help me out in resolving the above issue to run the .java file. I am facing same issue with other .java files inside the project The code is as shown below: package...
0
8888
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
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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
9174
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
9111
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8096
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...
0
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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.