Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General 2 / April 2006



Tip: Looking for answers? Try searching our database.

Behaviour of CType

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bert Tuijl (thuis) - 29 Apr 2006 23:54 GMT
Can someone explain this new-to-visualbasic programmer why my statement
throws an exception?

I have defined a class:
Class myDataSet
   Inherits System.Data.DataSet
   Public Sub fun(ByVal someparm As String)
       dosomething()
   End Sub
End Class

And I created a webservice function that returns a System.Data.DataSet
I try to stuff this dataset into my own dataset-type
dim myDataSet mds as myDataSet = new myDataSet
'mds = dswebsvc(arg) ' vbcompiler does not allow this but rather:
mds = CType( dswebsvc(arg), myDataSet) ' compiler allows this

But at runtime, this statement throws an exception with the message:
Unable to cast object of type 'System.Data.DataSet' to type 'myDataSet'

To overcome this problem I tried to overload the CType() function in the
myDataSet class but that is not allowed since apparently the compiler
automatically generates this overload!

What can I do to get rid of this nasty exception?

Bert
Steve Gerrard - 30 Apr 2006 00:40 GMT
> Can someone explain this new-to-visualbasic programmer why my statement throws
> an exception?
[quoted text clipped - 10 lines]
> But at runtime, this statement throws an exception with the message:
> Unable to cast object of type 'System.Data.DataSet' to type 'myDataSet'

First, you should ask your .Net questions in a dotnet group, not a VB6 group.

Second, since myDataSet inherits from DataSet, that means that:
   All instance of myDataSet are also an instance of DataSet, but
   Not all instances of DataSet are also an instance of myDataSet.

You can't "get rid of this nasty exception"; you actually can't cast a generic
DataSet to myDataSet, since it isn't one!

Perhaps you meant for your web service function dswebsvc() to return an instance
of myDataSet?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.