Casting up?

R

Rob Reagan

Can someone explain to me why I must explicitly cast UP a
class heiarchy when using option explicit? For instance,
I have a typed dataset (which is derived from the DataSet
class) and I'm passing this typed dataset to a function
that requires a system.data.dataset as an argument. The
compiler complains about a type mismatch and won't let me
compile until I pass the typed dataset as
CType(myTypedDataSet, DataSet). I find this very bizarre.
Maybe I'm really missing something. Any explanation would
be appreciated.

Rob Reagan
 
J

Jay B. Harlow [MVP - Outlook]

Rob,
CType(myTypedDataSet, DataSet). I find this very bizarre.
How is the variable myTypedDataSet defined?

If its defined as:

Dim myTypedDataSet As TypedDataSet

then the CType is not needed, as TypedDataSet is a DataSet and the compiler
knows it.

However if myTypedDataSet is defined as:

Dim myTypedDataSet As Object

Then the CType is needed, although I prefer DirectCast (DirectCast is used
for Casting, CType is used for Converting). It is needed, as an object can
contain any type of variable, and you need to let the compiler know
specifically which type you want.

So to better answer your question we need to know how you defined the
myTypedDataSet variable.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top