thomehm
asked on
Casting a base class to a subclass
Using the ADO.Net DataSet designer, I create a DataSet named dsUISupport, with a DataTable named dtCustomer. So, the designer autogenerates a strongly typed class something like this:
dtCustomerDataTable : DataTable
Now I want to add some functionality, so I add my own subclass:
cCustomer : dtCustomerDataTable
Now, what I am doing is, in a Windows Form, I have a DataGrid that uses dtCustomerDataTable as a DataSource (indirectly, via BindingSource).
Now, when the user makes a change to the DataGrid, the DataSource generates a RowChanged event, in the event handler, I use a dtCustomerDataTable.GetCha nges() to get a dtCustomerDataTable object with the changes that were made. Now, I want to cast this object to a cCustomer object, which is my own subclass. I understand I can't do this directly, because it isn't really a cCustomer object. So, perhaps casting is not the word. But I think that should in some way be able to get a CCustomer object with the same dtCustomerDataTable base class, if I am prepared to do some fixups. Could I new a cCustomer object and then somehow assign the dtCustomer DataTable to the base class? Or should I do some kind of DataTable.Copy to copy over the members of the base class? Please advise.
dtCustomerDataTable : DataTable
Now I want to add some functionality, so I add my own subclass:
cCustomer : dtCustomerDataTable
Now, what I am doing is, in a Windows Form, I have a DataGrid that uses dtCustomerDataTable as a DataSource (indirectly, via BindingSource).
Now, when the user makes a change to the DataGrid, the DataSource generates a RowChanged event, in the event handler, I use a dtCustomerDataTable.GetCha
do u get errors when trying to downcast your instance to CCustomer?
ASKER
"do u get errors when trying to downcast your instance to CCustomer?"
You don't understand my question. The relationship is:
cCustomer : dtCustomerDataTable
That is the base class is dtCustomerDataTable. The derived class is cCustomer. Given an instance of dtCustomerDataTable, I don't know how to create an instance of cCustomer that has dtCustomerDataTable as it's base. So, given an instance of dtCustomerDataTable t1.
cCustomer c1 = (cCustomer) t1.
won't even compile. And I would not expect it would. Because t1 is not cCustomer. It is the base class dtCustomerDataTable. But, that is what I want to accomplish. I want to embed my dtCustomerDataTable as the base class in a new cCustomer, I expect to have to do some fixup to the variables in the derived class. But I don't know how to do this.
You don't understand my question. The relationship is:
cCustomer : dtCustomerDataTable
That is the base class is dtCustomerDataTable. The derived class is cCustomer. Given an instance of dtCustomerDataTable, I don't know how to create an instance of cCustomer that has dtCustomerDataTable as it's base. So, given an instance of dtCustomerDataTable t1.
cCustomer c1 = (cCustomer) t1.
won't even compile. And I would not expect it would. Because t1 is not cCustomer. It is the base class dtCustomerDataTable. But, that is what I want to accomplish. I want to embed my dtCustomerDataTable as the base class in a new cCustomer, I expect to have to do some fixup to the variables in the derived class. But I don't know how to do this.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
u can request to close this thread without awarding any points, that's legit.
good luck.
good luck.
ASKER
It was an acceptable workaround.