Assignment Method

  • Thread starter Thread starter Christopher Weaver
  • Start date Start date
C

Christopher Weaver

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

System.Data.Odbc.OdbcCommand cmd;
cmd = new OdbcCommand();
cmd.Connection = odbc_Categories;
cmd.CommandText = "SELECT * FROM \"tblCategory\" ORDER BY \"Category\"";
odbcDA_CatSubCats.SelectCommand = 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_CatSubCats.UpdateCommand = 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_CatSubCats.UpdateCommand = (OdbcCommand)cmd.MemberwiseClone();

I get this:

Cannot access protected member 'object.MemberwiseClone()' 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.
 
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.
 
Bruce Wood said:
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.

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

using System;
using System.Data.Odbc;

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

OdbcConnection conn2 = (OdbcConnection)
((ICloneable)conn).Clone();
}
}
 
Excellent. I applied this to the OdbcCommand class and it worked fine.

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

using System;
using System.Data.Odbc;

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

OdbcConnection conn2 = (OdbcConnection)
((ICloneable)conn).Clone();
}
}

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
 
Mythran said:
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 :)

Oops - good catch.

The same goes for OdbcCommand, fortunately :)
 
Jon Skeet said:
Oops - good catch.

The same goes for OdbcCommand, fortunately :)

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

Mythran
 
Back
Top