473,388 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Behaviour of CType

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
Apr 29 '06 #1
1 7858

"Bert Tuijl (thuis)" <b.****@chello.nl> wrote in message
news:dc***************************@news.chello.nl. ..
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
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'


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?
Apr 29 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Douglas Buchanan | last post by:
I am using the following code instead of a very lengthly select case statement. (I have a lot of lookup tables in a settings form that are selected from a ListBox. The data adapters are given a...
29
by: Enrico `Trippo' Porreca | last post by:
Both K&R book and Steve Summit's tutorial define a getline() function correctly testing the return value of getchar() against EOF. I know that getchar() returns EOF or the character value cast to...
29
by: Charles Law | last post by:
Further to my issue about user controls, I have a problem with DesignMode. Here is the project hierarchy: MainApp |_ Project1 |_ SubProject (UserControl) SubProject has a default constructor...
3
by: Neil Wallace | last post by:
Hi, This is an odd one. I've been struggling to get "double click" to work well for my controls. The same event handler works perfectly for buttons, but not for labels. Can anyone tell me...
3
by: serge calderara | last post by:
Dear all I have a strange behaviour using session. ON my form load when the page is first display I create a session variable to stor connection objects as follow : If not IsPostBack then...
1
by: tedd_n_alex | last post by:
I have a problem with a program i have that only shows when the code is compiled in "Release" mode and ONLY when the .NET debugger is not attached to the process. I have some drawing code that...
23
by: gribouille | last post by:
Hi, via fgets() i create a array containing a text file. fp = fopen(argv, "r"); while ((c = fgetc(fp)) != EOF) { line = c; when i want to print it
12
by: Michael Windsor | last post by:
I've been trying to integrate some PHP pages of my own with some existing code. The details of this are for the support forums for that code (where I have been asking questions), but I wonder if...
12
by: Krumble Bunk | last post by:
Hi all, Having some trouble with a seemingly-simple task. Need to have a basic CLI functionality as per: prompt <- \n prompt <- \n promptquit <- should quit as per my snippet, but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.