Assignment Method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Weaver

    Assignment Method

    Seems like there ought to be a method that copies an object. If I do
    something like this:

    System.Data.Odb c.OdbcCommand cmd;
    cmd = new OdbcCommand();
    cmd.Connection = odbc_Categories ;
    cmd.CommandText = "SELECT * FROM \"tblCategor y\" ORDER BY \"Category\" ";
    odbcDA_CatSubCa ts.SelectComman d = cmd;

    All is well. But if I want to use cmd again, say for the Update command by
    redefining the CommandText property and then doing this:

    odbcDA_CatSubCa ts.UpdateComman d = cmd;

    I would redefine the SelectCommand as well. I would like to copy the
    property values of the cmd object to the various Command objects of the DA.

    I thought that MemberwiseClone () might handle this, but when I try this:

    odbcDA_CatSubCa ts.UpdateComman d = (OdbcCommand)cm d.MemberwiseClo ne();

    I get this:

    Cannot access protected member 'object.Memberw iseClone()' via a qualifier
    of type 'OdbcCommand'; the qualifier must be of type 'frmCategories' (or
    derived from it)

    'frmCategories' is the name of the form's class within which I'm working.

    Is this because OdbcCommand is not derived from Object? Is there a way to
    get around this? I'm thinking that a little refactoring might do the trick,
    but I would like to know if there is an assignment method (or copy method)
    for this object class.


  • Bruce Wood

    #2
    Re: Assignment Method

    My immediate reaction was simply to call cmd.Clone(), because the doc
    says that OdbcCommand implements ICloneable. Only that... it doesn't!
    The doc is lying.

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Assignment Method

      Bruce Wood <brucewood@cana da.com> wrote:[color=blue]
      > My immediate reaction was simply to call cmd.Clone(), because the doc
      > says that OdbcCommand implements ICloneable. Only that... it doesn't!
      > The doc is lying.[/color]

      No it's not. It implements it with explicit interface implementation.
      Try the following:

      using System;
      using System.Data.Odb c;

      class Test
      {
      static void Main()
      {
      OdbcConnection conn = new OdbcConnection( );

      OdbcConnection conn2 = (OdbcConnection )
      ((ICloneable)co nn).Clone();
      }
      }

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Christopher Weaver

        #4
        Re: Assignment Method

        Excellent. I applied this to the OdbcCommand class and it worked fine.

        Thanks.


        "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
        news:MPG.1cff0a d13fcbf18498c1c d@msnews.micros oft.com...[color=blue]
        > Bruce Wood <brucewood@cana da.com> wrote:[color=green]
        >> My immediate reaction was simply to call cmd.Clone(), because the doc
        >> says that OdbcCommand implements ICloneable. Only that... it doesn't!
        >> The doc is lying.[/color]
        >
        > No it's not. It implements it with explicit interface implementation.
        > Try the following:
        >
        > using System;
        > using System.Data.Odb c;
        >
        > class Test
        > {
        > static void Main()
        > {
        > OdbcConnection conn = new OdbcConnection( );
        >
        > OdbcConnection conn2 = (OdbcConnection )
        > ((ICloneable)co nn).Clone();
        > }
        > }
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet
        > If replying to the group, please do not mail me too[/color]


        Comment

        • Mythran

          #5
          Re: Assignment Method


          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.1cff0a d13fcbf18498c1c d@msnews.micros oft.com...[color=blue]
          > Bruce Wood <brucewood@cana da.com> wrote:[color=green]
          >> My immediate reaction was simply to call cmd.Clone(), because the doc
          >> says that OdbcCommand implements ICloneable. Only that... it doesn't!
          >> The doc is lying.[/color]
          >
          > No it's not. It implements it with explicit interface implementation.
          > Try the following:
          >
          > using System;
          > using System.Data.Odb c;
          >
          > class Test
          > {
          > static void Main()
          > {
          > OdbcConnection conn = new OdbcConnection( );
          >
          > OdbcConnection conn2 = (OdbcConnection )
          > ((ICloneable)co nn).Clone();
          > }
          > }
          >[/color]

          How does that help, btw? They are talking about the OdbcCommand, not
          OdbcConnection :P I haven't checked myself to see if OdbcCommand does in
          fact implement it, but just pointing it out which object they are referring
          to :)

          Mythran

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Assignment Method

            Mythran <kip_potter@hot mail.comREMOVET RAIL> wrote:[color=blue]
            > How does that help, btw? They are talking about the OdbcCommand, not
            > OdbcConnection :P I haven't checked myself to see if OdbcCommand does in
            > fact implement it, but just pointing it out which object they are referring
            > to :)[/color]

            Oops - good catch.

            The same goes for OdbcCommand, fortunately :)

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Mythran

              #7
              Re: Assignment Method


              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
              news:MPG.1cff17 52b8cf8f2b98c1c f@msnews.micros oft.com...[color=blue]
              > Mythran <kip_potter@hot mail.comREMOVET RAIL> wrote:[color=green]
              >> How does that help, btw? They are talking about the OdbcCommand, not
              >> OdbcConnection :P I haven't checked myself to see if OdbcCommand does in
              >> fact implement it, but just pointing it out which object they are
              >> referring
              >> to :)[/color]
              >
              > Oops - good catch.
              >
              > The same goes for OdbcCommand, fortunately :)
              >
              > --
              > Jon Skeet - <skeet@pobox.co m>
              > http://www.pobox.com/~skeet
              > If replying to the group, please do not mail me too[/color]

              lol, I noticed a bit after I posted the message that your message helped
              them solve the problem, so all is good :)

              Mythran

              Comment

              Working...