PC Review


Reply
Thread Tools Rate Thread

ctype , inherited classes, and "cast not valid"

 
 
Mike Cooper
Guest
Posts: n/a
 
      10th Oct 2003
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
 
Reply With Quote
 
 
 
 
Tom Shelton
Guest
Posts: n/a
 
      10th Oct 2003
In article <(E-Mail Removed)>, 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]
 
Reply With Quote
 
Fergus Cooney
Guest
Posts: n/a
 
      10th Oct 2003
Hi Mike,

There's an explanation in your original thread.

Regards,
Fergus


 
Reply With Quote
 
Mike Cooper
Guest
Posts: n/a
 
      10th Oct 2003
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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> In article <(E-Mail Removed)>, 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

 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      11th Oct 2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 <(E-Mail Removed)> wrote in message

news:<(E-Mail Removed)>...
> > In article <(E-Mail Removed)>, 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
InvalidCastException "Cast from string "6<5" to type 'Boolean is not valid. hazz Microsoft VB .NET 3 7th Jul 2005 10:22 PM
"Specified cast is not valid" error in Crystal Reports 9.0 Develop =?Utf-8?B?Um9uIEhvbG1lcw==?= Microsoft Dot NET 1 18th Dec 2004 06:22 AM
OLEDB Provider throws exception "Specified cast is not valid" HVG Microsoft ADO .NET 4 9th Sep 2004 03:45 AM
Re: Cast from string "2076719" to type 'Long' is not valid rsine Microsoft VB .NET 0 9th Jan 2004 05:28 PM
Re: Cast from string "2076719" to type 'Long' is not valid rsine Microsoft VB .NET 0 9th Jan 2004 01:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:56 AM.