PC Review


Reply
Thread Tools Rate Thread

Casting Exception. Simple Code... Why! Why! Why!

 
 
samadams_2006@yahoo.ca
Guest
Posts: n/a
 
      16th Feb 2007

I have what seems to be a very simple straight-forward program (shown
below), but I'm getting an error:

Unable to cast object of type 'TestApp1.ParentTable' to type
'TestApp1.ChildTable'.

on the line: objTable = .SelectedOptions (the fifth line from
the top in the following code).

Why is this? I should be able to go from a parent to the child,
correct? .SelectedOptions is of type ParentTable, and objTable is of
type ChildTable. ChildTable inherits from ParentTable. I've also
tried the commands Ctype and DirectCast, and this does not solve the
problem.

Can anyone tell me how to resolve this?

Thanks


--------------------------------------------------
Module Toolbars

Public Sub Main()

Dim objTable As ChildTable

With frmClass
objTable = .SelectedOptions
End With

End Sub

End Module
--------------------------------------------------
Public Class frmClass

Public ReadOnly Property SelectedOptions() As ParentTable
Get

Dim objTable As New ParentTable

With objTable
.strParent = "SelectedOption"
End With

Return objTable

End Get
End Property
End Class
--------------------------------------------------
Option Explicit On

Public Class ChildTable
Inherits ParentTable

Public ReadOnly Property strChild() As String
Get
Return "strChild"
End Get
End Property

End Class

Public Class ParentTable
Public strParent As String
End Class
--------------------------------------------------

 
Reply With Quote
 
 
 
 
RobinS
Guest
Posts: n/a
 
      16th Feb 2007
This doesn't make sense. First, you obviously don't have Option Strict On
or it wouldn't even compile, because you are not instantiating your class
before trying to use it, i.e. set the property.

Second, you can't define objTable as a ChildTable and then try to assign a
ParentTable to it. They are two different classes, even if the ChildTable
inherits ParentTable.

You can cast ChildTable back to ParentTable, but then you can access the
properties that ParentTable has (strParent), but not the properties that
ChildTable has that ParentTable does not have (strChild).


Robin S.
---------------------------
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> I have what seems to be a very simple straight-forward program (shown
> below), but I'm getting an error:
>
> Unable to cast object of type 'TestApp1.ParentTable' to type
> 'TestApp1.ChildTable'.
>
> on the line: objTable = .SelectedOptions (the fifth line from
> the top in the following code).
>
> Why is this? I should be able to go from a parent to the child,
> correct? .SelectedOptions is of type ParentTable, and objTable is of
> type ChildTable. ChildTable inherits from ParentTable. I've also
> tried the commands Ctype and DirectCast, and this does not solve the
> problem.
>
> Can anyone tell me how to resolve this?
>
> Thanks
>
>
> --------------------------------------------------
> Module Toolbars
>
> Public Sub Main()
>
> Dim objTable As ChildTable
>
> With frmClass
> objTable = .SelectedOptions
> End With
>
> End Sub
>
> End Module
> --------------------------------------------------
> Public Class frmClass
>
> Public ReadOnly Property SelectedOptions() As ParentTable
> Get
>
> Dim objTable As New ParentTable
>
> With objTable
> .strParent = "SelectedOption"
> End With
>
> Return objTable
>
> End Get
> End Property
> End Class
> --------------------------------------------------
> Option Explicit On
>
> Public Class ChildTable
> Inherits ParentTable
>
> Public ReadOnly Property strChild() As String
> Get
> Return "strChild"
> End Get
> End Property
>
> End Class
>
> Public Class ParentTable
> Public strParent As String
> End Class
> --------------------------------------------------
>



 
Reply With Quote
 
Patrick Steele
Guest
Posts: n/a
 
      16th Feb 2007
In article <(E-Mail Removed)>,
(E-Mail Removed) says...
> I should be able to go from a parent to the child,
> correct?


No, it's the other way around. You can always go from a child to a
parent. A child is a descendant of a parent -- so it gets whatever the
parent had, plus "more" (anything else that is added).

But, at the hear of it, the "child" is like the parent in that it has
all of the same methods -- which is why you can always go from a child
to a parent.

--
Patrick Steele
http://weblogs.asp.net/psteele
 
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
.NET 3.5 SP1 causes exception when casting arrays (using .Cast<>) Richard Everett Microsoft Dot NET Framework 6 15th Aug 2008 08:49 AM
Casting numeric enumeration value does not throw exception Steve Wilkinson Microsoft Dot NET Framework 3 19th May 2008 12:59 PM
simple casting problem (with small code example) buzzweetman@gmail.com Microsoft C# .NET 8 12th May 2007 07:22 PM
Base class casting exception =?Utf-8?B?TWljaGVs?= Microsoft C# .NET 0 19th Jan 2005 11:55 AM
Deserialization Casting Exception After Project Rebuild Dave L Microsoft C# .NET 2 11th Aug 2004 01:59 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:36 AM.