I want to create a class that will add some new methods helpful in using a DataTable class. I want to avoid static classes, so I created a class
MyDataTable : DataTable
and my methods there.
How can I convert DataTable objects to MyDataTable objects?
I already tried
MyDataTable dt2 = (MyDataTable)dt;
But it returns a InvalidCastException.
I know now that it doesn’t work this way. But I also have no idea how can I solve this. Can anyone help me with this?
Of course it causes an
InvalidCastExceptionsince not everyDataTableis of typeMyDataTable. You need to create an instance of your type:Normally i provide the most used constructors of the type i’m inheriting from. You can call the base constructor from the constructor of your type. So for example: