Issue implementing type converter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kerry Sanders

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

  • Jeffrey Tan[MSFT]

    #2
    RE: Issue implementing type converter


    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:

    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 :

    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 <dirgon@NOSPAMy ahoo.com>
    | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
    | Subject: Issue implementing type converter
    | Reply-To: dirgon@NOSPAMya hoo.com
    | Message-ID: <ifempv0r44eot8 e6djvm6aafj0534 adub5@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; }
    | }
    | }
    |
    |

    Comment

    • Kerry Sanders

      #3
      Re: Issue implementing type converter

      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-jetan@online.mi crosoft.com ("Jeffrey
      Tan[MSFT]") wrote:
      [color=blue]
      >
      >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[/color]

      Comment

      • Jeffrey Tan[MSFT]

        #4
        Re: Issue implementing type converter


        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:

        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 <dirgon@NOSPAMy ahoo.com>
        | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
        | Subject: Re: Issue implementing type converter
        | Reply-To: dirgon@NOSPAMya hoo.com
        | Message-ID: <4jerpv4vr8t4jo 2g1l5dl8p30mmd1 50dlm@4ax.com>
        | References: <ifempv0r44eot8 e6djvm6aafj0534 adub5@4ax.com>
        <7NrENYFnDHA.18 04@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-jetan@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:
        |[color=blue]
        >http://msdn.microsoft.com/library/de...-us/cpguide/ht[/color]
        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 :
        |[color=blue]
        >http://msdn.microsoft.com/library/de...-us/cpguide/ht[/color]
        m
        | >l/cpconimplementi ngtypeconverter .asp
        | >
        | >Hope this helps,
        | >
        | >Best regards,
        | >Jeffrey Tan
        | >Microsoft Online Partner Support
        |
        |

        Comment

        • Jeffrey Tan[MSFT]

          #5
          Re: Issue implementing type converter


          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" <dirgon@NOSPAMy ahoo.com>
          | Sender: "Kerry Sanders" <dirgon@NOSPAMy ahoo.com>
          | References: <ifempv0r44eot8 e6djvm6aafj0534 adub5@4ax.com>
          <7NrENYFnDHA.18 04@cpmsftngxa06 .phx.gbl>
          <4jerpv4vr8t4jo 2g1l5dl8p30mmd1 50dlm@4ax.com>
          <AAnPGEUnDHA.28 96@cpmsftngxa06 .phx.gbl>
          | Subject: Re: Issue implementing type converter
          | Date: Tue, 28 Oct 2003 07:08:00 -0800
          | Lines: 4
          | Message-ID: <0e0801c39d65$4 6e004d0$a401280 a@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.
          |
          |

          Comment

          • Kerry Sanders

            #6
            Re: Issue implementing type converter

            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.

            Comment

            • Jeffrey Tan[MSFT]

              #7
              Re: Issue implementing type converter


              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 <dirgon@NOSPAMy ahoo.com>
              | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
              | Subject: Re: Issue implementing type converter
              | Reply-To: dirgon@NOSPAMya hoo.com
              | Message-ID: <mef3rvceer6nk8 fl22ckajhv85vd3 7f77t@4ax.com>
              | References: <ifempv0r44eot8 e6djvm6aafj0534 adub5@4ax.com>
              <7NrENYFnDHA.18 04@cpmsftngxa06 .phx.gbl>
              <4jerpv4vr8t4jo 2g1l5dl8p30mmd1 50dlm@4ax.com>
              <AAnPGEUnDHA.28 96@cpmsftngxa06 .phx.gbl>
              <0e0801c39d65$4 6e004d0$a401280 a@phx.gbl>
              <XO35iP4oDHA.25 28@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.
              |
              |

              Comment

              Working...