By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,610 Members | 1,601 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,610 IT Pros & Developers. It's quick & easy.

ctype , inherited classes, and "cast not valid"

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
Hi Mike,

There's an explanation in your original thread.

Regards,
Fergus
Nov 20 '05 #3

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.