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: - Public Class MyTest
-
Inherits System.Web.UI.Control
-
-
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: -
Dim blat As MyTest
-
Dim voon As System.Web.UI.Control
-
-
voon = MyFunctionThatReturnsSystemWebUIControl()
-
-
'Method 1
-
blat = DirectCast(voon, Testframework.MyTest)
-
-
'Method 2
-
blat = CType(voon, Testframework.MyTest)
-
-
'Method 3
-
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.
4 1803
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: - Public Class MyTest
-
Inherits System.Web.UI.Control
-
-
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: -
Dim blat As MyTest
-
Dim voon As System.Web.UI.Control
-
-
voon = MyFunctionThatReturnsSystemWebUIControl()
-
-
'Method 1
-
blat = DirectCast(voon, Testframework.MyTest)
-
-
'Method 2
-
blat = CType(voon, Testframework.MyTest)
-
-
'Method 3
-
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.
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?
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"
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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))
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |