Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Casting a base class to a subclass

Posted on 2012-04-10
5
Medium Priority
?
472 Views
Last Modified: 2012-04-20
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.GetChanges() 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.
0
Comment
Question by:thomehm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 37831300
do u get errors when trying to downcast your instance to CCustomer?
0
 

Author Comment

by:thomehm
ID: 37832980
"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.
0
 

Accepted Solution

by:
thomehm earned 0 total points
ID: 37839348
I came up with a work around.  I did a new cCustomer, then copied all of the rows from dtCustomerDataTable using ImportRow.  Seems to work OK.  I'll go with that.

I can't accept my own post as a solution.  How do I close this?  I'll award the Solution to anyone who makes any kind of a useful comment.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37847794
u can request to close this thread without awarding any points, that's legit.
good luck.
0
 

Author Closing Comment

by:thomehm
ID: 37869808
It was an acceptable workaround.
0

Featured Post

Vim Reference Guide

Vim is a powerful text editor favored by many sysadmins and developers - here are some commands that you'll want to keep in your back pocket!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question