473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Can't cast from System.Web.UI.Control to derived type - Why?

Hi,
I've got a function that returns an object of type: system.web.ui.control - Now I've created a very simple class, derived from system.web.ui.control:

Expand|Select|Wrap|Line Numbers
  1. Public Class MyTest
  2.           Inherits System.Web.UI.Control
  3.  
  4. End Class
However, when I try to cast the result of the function into an instance of MyTest - it throws an 'Unable to cast' exception. I frankly can't understand why - the only thing I think it might be is that 'MyTest' class exists in a different namespace: Testframework. A GetType on MyTest reveals that it is of type: Testframework.MyTest - but I can't see why this causes casting to fail. FYI: I've tried the following ways to cast:
Expand|Select|Wrap|Line Numbers
  1. Dim blat As MyTest
  2. Dim voon As System.Web.UI.Control
  3.  
  4. voon = MyFunctionThatReturnsSystemWebUIControl()
  5.  
  6. 'Method 1
  7. blat = DirectCast(voon, Testframework.MyTest)
  8.  
  9. 'Method 2
  10. blat = CType(voon, Testframework.MyTest)
  11.  
  12. 'Method 3
  13. blat = voon
- I've also tried declaring 'blat' and 'voon' as New instances, prior to assignment - e.g. just omitting the function call, and seeing if I can assign a new object of type System.Web.UI.Control to a new object of type Testframework.MyTest.

Any help appericated,

Mat.
Mar 26 '08 #1
4 1803
r035198x
13,262 8TB
Hi,
I've got a function that returns an object of type: system.web.ui.control - Now I've created a very simple class, derived from system.web.ui.control:

Expand|Select|Wrap|Line Numbers
  1. Public Class MyTest
  2.           Inherits System.Web.UI.Control
  3.  
  4. End Class
However, when I try to cast the result of the function into an instance of MyTest - it throws an 'Unable to cast' exception. I frankly can't understand why - the only thing I think it might be is that 'MyTest' class exists in a different namespace: Testframework. A GetType on MyTest reveals that it is of type: Testframework.MyTest - but I can't see why this causes casting to fail. FYI: I've tried the following ways to cast:
Expand|Select|Wrap|Line Numbers
  1. Dim blat As MyTest
  2. Dim voon As System.Web.UI.Control
  3.  
  4. voon = MyFunctionThatReturnsSystemWebUIControl()
  5.  
  6. 'Method 1
  7. blat = DirectCast(voon, Testframework.MyTest)
  8.  
  9. 'Method 2
  10. blat = CType(voon, Testframework.MyTest)
  11.  
  12. 'Method 3
  13. blat = voon
- I've also tried declaring 'blat' and 'voon' as New instances, prior to assignment - e.g. just omitting the function call, and seeing if I can assign a new object of type System.Web.UI.Control to a new object of type Testframework.MyTest.

Any help appericated,

Mat.
An object of type MyTest is a System.Web.UI.Control but not all System.Web.UI.Controls are MyTest type objects.
Mar 26 '08 #2
An object of type MyTest is a System.Web.UI.Control but not all System.Web.UI.Controls are MyTest type objects.
I'm not sure I quite understand - I mean, I see what you're saying - I just can't see how a class that seems logically identical to System.Web.UI.Control (bar the name), cannot cast objects that are of type System.Web.UI.Control?
Mar 26 '08 #3
Plater
7,872 Expert 4TB
You are trying to cast in the wrong direction.
You can only cast "up" the heirarchy (to a base class).
Your class can be cast as a System.Web.UI.Control, but not the other way around.

To extend the previous example:
Even though a "Red Toyota Camry" is a car, not all cars are red. Nor are they all Toyota's, etc.
You can refer to your "Red Toyota Camry" as a car.
You could also refer to a "Blue Chevy Malibu" as a car.
But, even though you call your "Red Toyota Camry" a "car", you could not then go and call it a "Blue Chevy Malibu"
Mar 26 '08 #4
You are trying to cast in the wrong direction.
You can only cast "up" the heirarchy (to a base class).
Your class can be cast as a System.Web.UI.Control, but not the other way around.

To extend the previous example:
Even though a "Red Toyota Camry" is a car, not all cars are red. Nor are they all Toyota's, etc.
You can refer to your "Red Toyota Camry" as a car.
You could also refer to a "Blue Chevy Malibu" as a car.
But you would not refer to the "Red Toyota Camry" as a "Blue Chevy Malibu"
Aaaah. Yep, clear now. Many Thanks
Mar 26 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Pankaj Jain | last post by:
Hi All, I have a class A which is derived from ServicesComponent to participate in automatic transaction with falg Transaction.Required. Class A is exposed to client through remoting on Http...
1
by: ypjofficial | last post by:
Dear All, According to OOPs , a base class pointer can to point to derived class object....call this as fact1 But somehow I am not comfortable while understanding this concept. The explanaition...
8
by: CA | last post by:
Hi, I have a function where I would like to test whether an object is of a certain type. Here is my code so far. public bool HasValidType(Type t, object val) { try { if (t==typeof(double))
10
by: Dave Leach | last post by:
I am writing a Windows MDI application in C#. I would like to cast an object to a specific type. I know that the object is of a compatible type. The code below shows what I would like to do, but...
18
by: Mike Bartels | last post by:
Hi Everyone! I have two Arrays A and B. Both arrays are byte arrays with 7 bytes each. The contents of array A and B are the same A = {1, 2, 3, 4, 5, 6, 7}; B = {1, 2, 3, 4, 5, 6, 7}; When...
6
by: Lore Leunoeg | last post by:
Hello I derived a class MyControl from the Control class. Public Class MyControl Inherits Control Sub New() MyBase.New() End Sub End Class
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
6
by: Ron M. Newman | last post by:
Hi, Under what circumstances can I ever get an exception about not being able to cast type "ABC" to type "ABC" if there's only one ABC class in the system and it's loaded from a dynamic...
11
by: Dinsdale | last post by:
I am trying to determine what the current cast of an object is. Essentially, if I use GetType, it always returns the original type of the object created but I want to know what the current cast of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.