Casting Problem

atmosphere

Member
Joined
Jun 14, 2005
Messages
8
Programming Experience
5-10
Hey

Im having casting problems.. should the following work?

VB.NET:
dim dTable as DataTable = ...
dim enumTable as EnumeratedTable = CType(dTable, EnumeratedTable)
 
 
[size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Class[/color][/size][size=2] EnumeratedTable[/size]
[size=2][color=#0000ff][color=#000000]		[/color]Inherits[/color][/size][size=2] DataTable[/size]
[size=2]End Class[/size]
 
 
 
 
[size=2]
[/size]
 
No. You can cast the other way, because every EnumeratedTable is a DataTable. Not every DataTable is an EnumeratedTable, though, so to allow a cast is not appropriate. You could create a constructor for your class that takes a DataTable as an argument, then populates the remaining properties in some default fashion. Then you can use code like:
VB.NET:
Dim myDataTable As New DataTable
Dim myEnumeratedTable As New EnumeratedTable(myDataTable)
Keep in mind, though, that a DataRow can only be in one DataTable at a time, so you couldn't just add the same rows. Also keep in mind that, because every EnumeratedTable is a DataTable, one can be used in any place that a DataTable can, e.g. a standard DataSet could contain EnumeratedTable objects, and you can Fill an EnumeratedTable directly using a DataAdapter.
 
Back
Top