473,322 Members | 1,510 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,322 software developers and data experts.

ctype , inherited classes, and "cast not valid"

There is something about inherited classes I evidently don't know...

I wrote the following class:

Class Class1
inherits System.Windows.Forms.DataGridTextBoxColumn
End Class

There is absolutely no added functionality to it.

Then, in Sub Main, I have the following code.

Dim hihi as Datagridtextboxcolumn
Dim hehe as Class1
hehe = ctype(hihi,class1)

The above code failes with a cast not valid error!!

My real code is considerably more complex ofcourse. But in order to
find out
why my "cast is not valid" I have reduced it to the skeleton code
above. Because class1 is a undistinguished child of
DataGridTextBoxColumn, I see no reason why an object of the parent
cannot be Ctyped into an object of the child. But it can't. Can
anyone tell me why. I am using Visualbasic.net if that makes a
difference. Please let me know.

Thanks,
Mike Cooper
Nov 20 '05 #1
4 3795
In article <8b**************************@posting.google.com >, Mike Cooper wrote:
There is something about inherited classes I evidently don't know...

I wrote the following class:

Class Class1
inherits System.Windows.Forms.DataGridTextBoxColumn
End Class

There is absolutely no added functionality to it.

Then, in Sub Main, I have the following code.

Dim hihi as Datagridtextboxcolumn
Dim hehe as Class1
hehe = ctype(hihi,class1)

The above code failes with a cast not valid error!!

My real code is considerably more complex ofcourse. But in order to
find out
why my "cast is not valid" I have reduced it to the skeleton code
above. Because class1 is a undistinguished child of
DataGridTextBoxColumn, I see no reason why an object of the parent
cannot be Ctyped into an object of the child. But it can't. Can
anyone tell me why. I am using Visualbasic.net if that makes a
difference. Please let me know.

Thanks,
Mike Cooper


You are attempting to downcast the parent object to the childs type.
That is not possible. Basically, you have to think of it as your C1 is
a DataGridTextBoxColumn, but a DataGridTextBoxColumn is not a C1. Now,
if you reverse it...

Dim hihi as DataGridTextBoxColumn
Dim hehe as New C1

hihi = CType(hee, DataGridTextBoxColumn)
That will work. If you think about it, it makes sense. When you
inherit from something it is normally to extend that functionality. If
you were able to cast hihi to hehe - you wold end up with a lot of
problems if you started calling C1 functionality, because it isn't
implemented in the parent class.

HTH
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #2
Hi Mike,

There's an explanation in your original thread.

Regards,
Fergus
Nov 20 '05 #3
Hi Tom. Thanks for responding. Below is the original code I got from
the msdn site which I built my own code off of:

cellRect = grid.GetCellBounds(hi.Row, hi.Column)
' Get the clicked DataGridTextBoxColumn.
Dim gridCol As MyGridColumn = CType _
(dgt.GridColumnStyles(hi.Column), MyGridColumn)
' Get the Graphics object for the form.
Dim g As Graphics = dataGrid1.CreateGraphics()
' Create two new Brush objects: a fore brush and back brush.
Dim fBrush As New SolidBrush(Color.Blue)
Dim bBrush As New SolidBrush(Color.Yellow)
' Invoke the Paint method to paint the cell with the brushes.
gridCol.PaintCol(g, cellRect, cm, hi.Row, bBrush, fBrush,
False)
Public Class MyGridColumn
Inherits DataGridTextBoxColumn
Public Sub PaintCol(g As Graphics , cellRect As Rectangle , _
cm As CurrencyManager , rowNum As integer , bBrush as Brush , _
fBrush As Brush , isVisible As Boolean )
me.Paint(g, cellRect, cm, rowNum, bBrush, fBrush, isVisible)
End Sub
In the above dgt.datagridcolumnstyles(0) points to a
datagridtextboxcolumn object as it does in my porgram as well. Using
the exact name of the object instead of this reference produced the
same result

In the statement:
Dim gridCol As MyGridColumn = CType _
(dgt.GridColumnStyles(hi.Column), MyGridColumn)

We are attempting to convert a datagridtextboxcolumn (Parent object)
into a MyGridColumn object (child) in order to use the paint method.
Is the above code incorrect? Again, I guess I had the wrong idea on
inheritance. I thought its point was to add functionality to more
basic objects.

You can find the above code at:

http://msdn.microsoft.com/library/de...painttopic.asp
Please tell me if I am missing something.

Tom Shelton <to*@mtogden.com> wrote in message news:<Oe**************@TK2MSFTNGP11.phx.gbl>...
In article <8b**************************@posting.google.com >, Mike Cooper wrote:
There is something about inherited classes I evidently don't know...

I wrote the following class:

Class Class1
inherits System.Windows.Forms.DataGridTextBoxColumn
End Class

There is absolutely no added functionality to it.

Then, in Sub Main, I have the following code.

Dim hihi as Datagridtextboxcolumn
Dim hehe as Class1
hehe = ctype(hihi,class1)

The above code failes with a cast not valid error!!

My real code is considerably more complex ofcourse. But in order to
find out
why my "cast is not valid" I have reduced it to the skeleton code
above. Because class1 is a undistinguished child of
DataGridTextBoxColumn, I see no reason why an object of the parent
cannot be Ctyped into an object of the child. But it can't. Can
anyone tell me why. I am using Visualbasic.net if that makes a
difference. Please let me know.

Thanks,
Mike Cooper


You are attempting to downcast the parent object to the childs type.
That is not possible. Basically, you have to think of it as your C1 is
a DataGridTextBoxColumn, but a DataGridTextBoxColumn is not a C1. Now,
if you reverse it...

Dim hihi as DataGridTextBoxColumn
Dim hehe as New C1

hihi = CType(hee, DataGridTextBoxColumn)
That will work. If you think about it, it makes sense. When you
inherit from something it is normally to extend that functionality. If
you were able to cast hihi to hehe - you wold end up with a lot of
problems if you started calling C1 functionality, because it isn't
implemented in the parent class.

HTH

Nov 20 '05 #4
Mike,
You're missing one very important part of the sample! The part where a
MyGridColumn was added to the dgt.DataGridColumnStyles collection.

As Tom stated you can use CType to cast an object from a derived class to a
base class. Technically the CType or DirectCast is not required in this case
as casting to a base is always allowed.

You do need to use CType or DirectCast when you have an object of a derived
class in a variable of a base class and you want to get back to the derived
class.

Notice the you have an object of a derived class in a variable of a base
class!

Something like:
Class Class1
inherits System.Windows.Forms.DataGridTextBoxColumn
End Class
Dim hihi as Datagridtextboxcolumn
Dim hehe as Class1
hihi = new Class1
hehe = DirectCast(hihi,class1)
The above DirectCast works as the hihi variable, although its a
DataGridTextBoxColumn type, contains a Class1 object. It is allowed to
contain Class1 object as Class1 inherits from DataGridTextBoxColumn.

I normally use DirectCast when I am casting variables from a derived type to
a base type. I use CType when I need to convert a variable from one type to
another type. Which effectively means I use DirectCast most of the time, and
use CType when dealing with Enums.

Hope this helps
Jay

"Mike Cooper" <bi******@yahoo.com> wrote in message
news:8b**************************@posting.google.c om...
Hi Tom. Thanks for responding. Below is the original code I got from
the msdn site which I built my own code off of:

cellRect = grid.GetCellBounds(hi.Row, hi.Column)
' Get the clicked DataGridTextBoxColumn.
Dim gridCol As MyGridColumn = CType _
(dgt.GridColumnStyles(hi.Column), MyGridColumn)
' Get the Graphics object for the form.
Dim g As Graphics = dataGrid1.CreateGraphics()
' Create two new Brush objects: a fore brush and back brush.
Dim fBrush As New SolidBrush(Color.Blue)
Dim bBrush As New SolidBrush(Color.Yellow)
' Invoke the Paint method to paint the cell with the brushes.
gridCol.PaintCol(g, cellRect, cm, hi.Row, bBrush, fBrush,
False)
Public Class MyGridColumn
Inherits DataGridTextBoxColumn
Public Sub PaintCol(g As Graphics , cellRect As Rectangle , _
cm As CurrencyManager , rowNum As integer , bBrush as Brush , _
fBrush As Brush , isVisible As Boolean )
me.Paint(g, cellRect, cm, rowNum, bBrush, fBrush, isVisible)
End Sub
In the above dgt.datagridcolumnstyles(0) points to a
datagridtextboxcolumn object as it does in my porgram as well. Using
the exact name of the object instead of this reference produced the
same result

In the statement:
Dim gridCol As MyGridColumn = CType _
(dgt.GridColumnStyles(hi.Column), MyGridColumn)

We are attempting to convert a datagridtextboxcolumn (Parent object)
into a MyGridColumn object (child) in order to use the paint method.
Is the above code incorrect? Again, I guess I had the wrong idea on
inheritance. I thought its point was to add functionality to more
basic objects.

You can find the above code at:

http://msdn.microsoft.com/library/de...painttopic.asp

Please tell me if I am missing something.

Tom Shelton <to*@mtogden.com> wrote in message

news:<Oe**************@TK2MSFTNGP11.phx.gbl>...
In article <8b**************************@posting.google.com >, Mike

Cooper wrote: There is something about inherited classes I evidently don't know...

I wrote the following class:

Class Class1
inherits System.Windows.Forms.DataGridTextBoxColumn
End Class

There is absolutely no added functionality to it.

Then, in Sub Main, I have the following code.

Dim hihi as Datagridtextboxcolumn
Dim hehe as Class1
hehe = ctype(hihi,class1)

The above code failes with a cast not valid error!!

My real code is considerably more complex ofcourse. But in order to
find out
why my "cast is not valid" I have reduced it to the skeleton code
above. Because class1 is a undistinguished child of
DataGridTextBoxColumn, I see no reason why an object of the parent
cannot be Ctyped into an object of the child. But it can't. Can
anyone tell me why. I am using Visualbasic.net if that makes a
difference. Please let me know.

Thanks,
Mike Cooper


You are attempting to downcast the parent object to the childs type.
That is not possible. Basically, you have to think of it as your C1 is
a DataGridTextBoxColumn, but a DataGridTextBoxColumn is not a C1. Now,
if you reverse it...

Dim hihi as DataGridTextBoxColumn
Dim hehe as New C1

hihi = CType(hee, DataGridTextBoxColumn)
That will work. If you think about it, it makes sense. When you
inherit from something it is normally to extend that functionality. If
you were able to cast hihi to hehe - you wold end up with a lot of
problems if you started calling C1 functionality, because it isn't
implemented in the parent class.

HTH

Nov 20 '05 #5

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

Similar topics

0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
0
by: Peter Afonin | last post by:
Hello: When I try to access a SQL server or a network share from an ASP.Net application that I run on my computer, I run into security problems (for instance, I cannot execute DTS package using...
8
by: Charles | last post by:
I do not understand why I am getting a "Specified cast is not valid" error, since it has worked before. Something has changed and I am not really sure what it could be. I am looking for something...
2
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: ...
6
by: SA | last post by:
Hi all: I have an object of a base class that needs to be cast to an object of a specialized class. What is the best way to do this? (I thought about creating a constructor in the specialized...
2
by: tlk | last post by:
Hi, everyone. I'm having a problem that I hope some more experienced developers can help me figure-out. We have a project called Records that has been around for a year or so. We're currently...
3
by: keithb | last post by:
Using a GridView, I get a "Specified cast is not valid" error when binding the Visible propery of a hyperlink control to a DataTable text field. The error goes away if I replace the data binding...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.