473,725 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.DataGridT extBoxColumn
End Class

There is absolutely no added functionality to it.

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

Dim hihi as Datagridtextbox column
Dim hehe as Class1
hehe = ctype(hihi,clas s1)

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
DataGridTextBox Column, 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 3837
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.DataGridT extBoxColumn
End Class

There is absolutely no added functionality to it.

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

Dim hihi as Datagridtextbox column
Dim hehe as Class1
hehe = ctype(hihi,clas s1)

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
DataGridTextBox Column, 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 DataGridTextBox Column, but a DataGridTextBox Column is not a C1. Now,
if you reverse it...

Dim hihi as DataGridTextBox Column
Dim hehe as New C1

hihi = CType(hee, DataGridTextBox Column)
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.GetCellBou nds(hi.Row, hi.Column)
' Get the clicked DataGridTextBox Column.
Dim gridCol As MyGridColumn = CType _
(dgt.GridColumn Styles(hi.Colum n), MyGridColumn)
' Get the Graphics object for the form.
Dim g As Graphics = dataGrid1.Creat eGraphics()
' Create two new Brush objects: a fore brush and back brush.
Dim fBrush As New SolidBrush(Colo r.Blue)
Dim bBrush As New SolidBrush(Colo r.Yellow)
' Invoke the Paint method to paint the cell with the brushes.
gridCol.PaintCo l(g, cellRect, cm, hi.Row, bBrush, fBrush,
False)
Public Class MyGridColumn
Inherits DataGridTextBox Column
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.datagridcol umnstyles(0) points to a
datagridtextbox column 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.GridColumn Styles(hi.Colum n), MyGridColumn)

We are attempting to convert a datagridtextbox column (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.co m> wrote in message news:<Oe******* *******@TK2MSFT NGP11.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.DataGridT extBoxColumn
End Class

There is absolutely no added functionality to it.

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

Dim hihi as Datagridtextbox column
Dim hehe as Class1
hehe = ctype(hihi,clas s1)

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
DataGridTextBox Column, 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 DataGridTextBox Column, but a DataGridTextBox Column is not a C1. Now,
if you reverse it...

Dim hihi as DataGridTextBox Column
Dim hehe as New C1

hihi = CType(hee, DataGridTextBox Column)
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.DataGridCol umnStyles 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.DataGridT extBoxColumn
End Class
Dim hihi as Datagridtextbox column
Dim hehe as Class1
hihi = new Class1
hehe = DirectCast(hihi ,class1)
The above DirectCast works as the hihi variable, although its a
DataGridTextBox Column type, contains a Class1 object. It is allowed to
contain Class1 object as Class1 inherits from DataGridTextBox Column.

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.goo gle.com...
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.GetCellBou nds(hi.Row, hi.Column)
' Get the clicked DataGridTextBox Column.
Dim gridCol As MyGridColumn = CType _
(dgt.GridColumn Styles(hi.Colum n), MyGridColumn)
' Get the Graphics object for the form.
Dim g As Graphics = dataGrid1.Creat eGraphics()
' Create two new Brush objects: a fore brush and back brush.
Dim fBrush As New SolidBrush(Colo r.Blue)
Dim bBrush As New SolidBrush(Colo r.Yellow)
' Invoke the Paint method to paint the cell with the brushes.
gridCol.PaintCo l(g, cellRect, cm, hi.Row, bBrush, fBrush,
False)
Public Class MyGridColumn
Inherits DataGridTextBox Column
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.datagridcol umnstyles(0) points to a
datagridtextbox column 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.GridColumn Styles(hi.Colum n), MyGridColumn)

We are attempting to convert a datagridtextbox column (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.co m> wrote in message

news:<Oe******* *******@TK2MSFT NGP11.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.DataGridT extBoxColumn
End Class

There is absolutely no added functionality to it.

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

Dim hihi as Datagridtextbox column
Dim hehe as Class1
hehe = ctype(hihi,clas s1)

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
DataGridTextBox Column, 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 DataGridTextBox Column, but a DataGridTextBox Column is not a C1. Now,
if you reverse it...

Dim hihi as DataGridTextBox Column
Dim hehe as New C1

hihi = CType(hee, DataGridTextBox Column)
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
3635
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: "Specified cast is not valid." The only thing i put there was a "test this." inside the form. What might be the problem here? Thanks in advance. The Exception:
10
2612
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 of that software. That subject seems not to be addressed, at least not directly, in the C FAQ where FAQ 5.2 seems most relevant. References: * C FAQ 5.2 Null pointers (Including conditions where "casting" of null pointer...
0
1586
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 Trusted connection or get a file information using FileInfo class). This is probably because my application is running under PETER\ASPNET account, where PETER is my computer's name. I can solve this problem by using Impersonation. However, when...
8
1803
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 along the lines of did you check this, have you did this, or this is the wrong way of doing this. I can access the CColPositions and the CPosition class, but when I try to use the two classes in a For...Next loop I get the error message above. ...
2
3091
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: try {
6
11300
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 class that takes an argument of the type of the base class and then copy property values over, but that seems like a hassle) --
2
1620
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 coding an add-on to this project called Lessons. In creating Lessons we're obviously trying to re-use existing code from Records. And, of course, for political reasons I'm not free to make any changes to Records. The project Records contains a...
3
3696
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 statement with "true" or "false". This code causes the error: <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("c2Hyperlink1Link") %>' Target="_blank"
3
12434
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: ====================== Message: Specified cast is not valid. Type: System.InvalidCastException Source: System.Data.Linq TargetSite: Boolean TryCreateKeyFromValues(System.Object, V ByRef)
0
8750
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9168
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6701
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6010
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2154
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.