ctype , inherited classes, and "cast not valid"

M

Mike Cooper

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
 
T

Tom Shelton

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
 
M

Mike Cooper

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/d...formsdatagridtextboxcolumnclasspainttopic.asp


Please tell me if I am missing something.
 
J

Jay B. Harlow [MVP - Outlook]

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:
hihi = new 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
 

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