unable to cast object of type 'X' to type 'X'
-
I have the following line of code
ResponseProcessingCSP rpCSP = (ResponseProcessingCSP)CSPManager.Instance.GetResponseProcessesingCSP(ChannelID);
GetResponseProcessesingCSP returns an object of type IResponseProcessingCSP ResponseProcessingCSP inherits from type IResponseProcessingCSP as well. This throws an error saying "Unable to cast object of type 'ResponseProcessingCSP' to 'ResponseProcessingCSP' What would be causing this? -
I have the following line of code
ResponseProcessingCSP rpCSP = (ResponseProcessingCSP)CSPManager.Instance.GetResponseProcessesingCSP(ChannelID);
GetResponseProcessesingCSP returns an object of type IResponseProcessingCSP ResponseProcessingCSP inherits from type IResponseProcessingCSP as well. This throws an error saying "Unable to cast object of type 'ResponseProcessingCSP' to 'ResponseProcessingCSP' What would be causing this?Could the two types have the same name, but come from different assemblies (or namespaces)? Does fully qualifying the type with the namespace help? e.g.
MyCompany.MyComponent.ResponseProcessingCSP
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website
-
I have the following line of code
ResponseProcessingCSP rpCSP = (ResponseProcessingCSP)CSPManager.Instance.GetResponseProcessesingCSP(ChannelID);
GetResponseProcessesingCSP returns an object of type IResponseProcessingCSP ResponseProcessingCSP inherits from type IResponseProcessingCSP as well. This throws an error saying "Unable to cast object of type 'ResponseProcessingCSP' to 'ResponseProcessingCSP' What would be causing this?Hello,
joelbasson wrote:
"Unable to cast object of type 'ResponseProcessingCSP' to 'ResponseProcessingCSP'
Doesn't it say "Unable to cast object of type 'IResponseProcessingCSP' to 'ResponseProcessingCSP'"?
joelbasson wrote:
ResponseProcessingCSP inherits from type IResponseProcessingCSP
But it was instanciated as IResponseProcessingCSP, and you can not Up'cast' it , I think! Maybe this two examples help you: 1) Your problem (You cannot make a button out of a control)
Control _control = new Control();
Button _button = _control as Button;
if(_button!=null)
{
}
else
{
//this part of the code is executed, cause _button = null;
}2)But you can make a Button, hold it as Control and when you need it cast it back to Button
Button _button = new Button();
Control _control = _button;
Button _newbutton = _control as Button;
if(_newbutton!=null)
{
//this part of the code is executed, cause _newbutton != null;
}
else
{
}Hope it helps! P.S.: You should use the 'as' operator, which will not throw an exception, if the casting is not possible!
All the best, Martin
-
Hello,
joelbasson wrote:
"Unable to cast object of type 'ResponseProcessingCSP' to 'ResponseProcessingCSP'
Doesn't it say "Unable to cast object of type 'IResponseProcessingCSP' to 'ResponseProcessingCSP'"?
joelbasson wrote:
ResponseProcessingCSP inherits from type IResponseProcessingCSP
But it was instanciated as IResponseProcessingCSP, and you can not Up'cast' it , I think! Maybe this two examples help you: 1) Your problem (You cannot make a button out of a control)
Control _control = new Control();
Button _button = _control as Button;
if(_button!=null)
{
}
else
{
//this part of the code is executed, cause _button = null;
}2)But you can make a Button, hold it as Control and when you need it cast it back to Button
Button _button = new Button();
Control _control = _button;
Button _newbutton = _control as Button;
if(_newbutton!=null)
{
//this part of the code is executed, cause _newbutton != null;
}
else
{
}Hope it helps! P.S.: You should use the 'as' operator, which will not throw an exception, if the casting is not possible!
All the best, Martin
Martin# wrote:
But it was instanciated as IResponseProcessingCSP, and you can not Up'cast' it , I think!
You can't instantiate an interface.
Martin# wrote:
You should use the 'as' operator, which will not throw an exception, if the casting is not possible!
Perhaps there would be a situation where you do want an exception. Perhaps a case where the situation should never exist and so will be an exceptional case. Use "as" only when the posibility is frequent and expected.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website
-
Martin# wrote:
But it was instanciated as IResponseProcessingCSP, and you can not Up'cast' it , I think!
You can't instantiate an interface.
Martin# wrote:
You should use the 'as' operator, which will not throw an exception, if the casting is not possible!
Perhaps there would be a situation where you do want an exception. Perhaps a case where the situation should never exist and so will be an exceptional case. Use "as" only when the posibility is frequent and expected.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website
So far I've never used
as
, I useis
to test and then cast as appropriate. -
So far I've never used
as
, I useis
to test and then cast as appropriate.Hi, functionally as + nulltest is equivalent to is + (cast) but performance wise as + nulltest is bound to be faster since is+(cast) is checking the type twice. So as has more value for me than is; I will use as all the time, except when the casted object is not needed at all, then is will do. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi, functionally as + nulltest is equivalent to is + (cast) but performance wise as + nulltest is bound to be faster since is+(cast) is checking the type twice. So as has more value for me than is; I will use as all the time, except when the casted object is not needed at all, then is will do. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Martin# wrote:
But it was instanciated as IResponseProcessingCSP, and you can not Up'cast' it , I think!
You can't instantiate an interface.
Martin# wrote:
You should use the 'as' operator, which will not throw an exception, if the casting is not possible!
Perhaps there would be a situation where you do want an exception. Perhaps a case where the situation should never exist and so will be an exceptional case. Use "as" only when the posibility is frequent and expected.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website
Hello,
Colin Angus Mackay wrote:
You can't instantiate an interface.
True, but what does that mean to his problem. Hmmm! The method returns an IResponsableProcessingCSP instance and can not be casted too an derived class. Would it have the same effect (exception) if it was originaly instanciated as on other derived class from IResponsableProcessingCSP?
Colin Angus Mackay wrote:
Perhaps there would be a situation where you do want an exception. Perhaps a case where the situation should never exist and so will be an exceptional case. Use "as" only when the posibility is frequent and expected.
Personaly, I would also use the as operator for that and check !=null, and throw my own Exception! But that's maybe a matter of taste.
All the best, Martin
-
Hello,
Colin Angus Mackay wrote:
You can't instantiate an interface.
True, but what does that mean to his problem. Hmmm! The method returns an IResponsableProcessingCSP instance and can not be casted too an derived class. Would it have the same effect (exception) if it was originaly instanciated as on other derived class from IResponsableProcessingCSP?
Colin Angus Mackay wrote:
Perhaps there would be a situation where you do want an exception. Perhaps a case where the situation should never exist and so will be an exceptional case. Use "as" only when the posibility is frequent and expected.
Personaly, I would also use the as operator for that and check !=null, and throw my own Exception! But that's maybe a matter of taste.
All the best, Martin
Martin# wrote:
Colin Angus Mackay wrote: You can't instantiate an interface. True
Martin# wrote:
The method returns an IResponsableProcessingCSP instance
You argree with me, then contradict yourself. You cannot have an instance of an interface. You can only have a reference to one. The instance is of some concrete object that happens to implement the interface.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website
-
Hi, functionally as + nulltest is equivalent to is + (cast) but performance wise as + nulltest is bound to be faster since is+(cast) is checking the type twice. So as has more value for me than is; I will use as all the time, except when the casted object is not needed at all, then is will do. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
Very true, and I'll have to keep that in mind, but one of my more complicated uses of
is
+ cast is this sort of thing:if ( o is typeA ) do something with (typeA) o
else if ( o is typeB ) do something with (typeB) o
else if ( o is typeC ) do something with (typeC) o
...I don't think that would translate well to using
as
:typeA tempA ;
typeB tempB ;
typeC tempC ;
...if ( ( tempA = o as typeA ) != null ) do something with tempA
else if ( ( tempB = o as typeB ) != null ) do something with tempB
else if ( ( tempC = o as typeC ) != null ) do something with tempC
...And
is
+ cast works with all types, not just reference types. -
Very true, and I'll have to keep that in mind, but one of my more complicated uses of
is
+ cast is this sort of thing:if ( o is typeA ) do something with (typeA) o
else if ( o is typeB ) do something with (typeB) o
else if ( o is typeC ) do something with (typeC) o
...I don't think that would translate well to using
as
:typeA tempA ;
typeB tempB ;
typeC tempC ;
...if ( ( tempA = o as typeA ) != null ) do something with tempA
else if ( ( tempB = o as typeB ) != null ) do something with tempB
else if ( ( tempC = o as typeC ) != null ) do something with tempC
...And
is
+ cast works with all types, not just reference types.Hi, OK, I guess that's why both forms exist. My use is mostly simple, as in find all Buttons in myForm.Controls (So I would appreciate a forgiving variant of foreach that does this automatically without throwing when some non-Button Controls are present) Type checking value types is something I havent needed much, if at all. If and when I need it, the compiler will remind me to use is (and then I will remember this thread). Regards.
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hello,
joelbasson wrote:
"Unable to cast object of type 'ResponseProcessingCSP' to 'ResponseProcessingCSP'
Doesn't it say "Unable to cast object of type 'IResponseProcessingCSP' to 'ResponseProcessingCSP'"?
joelbasson wrote:
ResponseProcessingCSP inherits from type IResponseProcessingCSP
But it was instanciated as IResponseProcessingCSP, and you can not Up'cast' it , I think! Maybe this two examples help you: 1) Your problem (You cannot make a button out of a control)
Control _control = new Control();
Button _button = _control as Button;
if(_button!=null)
{
}
else
{
//this part of the code is executed, cause _button = null;
}2)But you can make a Button, hold it as Control and when you need it cast it back to Button
Button _button = new Button();
Control _control = _button;
Button _newbutton = _control as Button;
if(_newbutton!=null)
{
//this part of the code is executed, cause _newbutton != null;
}
else
{
}Hope it helps! P.S.: You should use the 'as' operator, which will not throw an exception, if the casting is not possible!
All the best, Martin
Martin# wrote:
But it was instanciated as IResponseProcessingCSP
Apart from the fact that an interface cannot be instantiated, this code will work perfectly fine:
ITheInterface iface = new ClassThatImplementsITheInterface();
As far as the underlying object is concerned, it does NOT matter what type you use to reference it*, what matters is the ctor used to instantiate the type. * As long as it represents a valid cast, of course.
Cheers, Vıkram.
After all is said and done, much is said and little is done.
-
Martin# wrote:
Colin Angus Mackay wrote: You can't instantiate an interface. True
Martin# wrote:
The method returns an IResponsableProcessingCSP instance
You argree with me, then contradict yourself. You cannot have an instance of an interface. You can only have a reference to one. The instance is of some concrete object that happens to implement the interface.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website
Hello Colin,
Colin Angus Mackay wrote:
You cannot have an instance of an interface. You can only have a reference to one.
Ok, ok! It should be obvious that I'm not using the write programming vocabulary!
Colin Angus Mackay wrote:
The instance is of some concrete object that happens to implement the interface.
That's what I was trying to explain! (Did it in a poor way, off course!) So again my question: Would it have the same effect (Exception): The method returns a reference to the interface, which was implemented from classA but casted from classB (which also implements the interface)? -- modified at 1:31 Tuesday 10th July, 2007 Wa sjust reading the answere from Vikram A Punathambekar [^] Thanks for your time and patience!
All the best, Martin
-
Martin# wrote:
But it was instanciated as IResponseProcessingCSP
Apart from the fact that an interface cannot be instantiated, this code will work perfectly fine:
ITheInterface iface = new ClassThatImplementsITheInterface();
As far as the underlying object is concerned, it does NOT matter what type you use to reference it*, what matters is the ctor used to instantiate the type. * As long as it represents a valid cast, of course.
Cheers, Vıkram.
After all is said and done, much is said and little is done.
-
Martin# wrote:
But it was instanciated as IResponseProcessingCSP
Apart from the fact that an interface cannot be instantiated, this code will work perfectly fine:
ITheInterface iface = new ClassThatImplementsITheInterface();
As far as the underlying object is concerned, it does NOT matter what type you use to reference it*, what matters is the ctor used to instantiate the type. * As long as it represents a valid cast, of course.
Cheers, Vıkram.
After all is said and done, much is said and little is done.
I just did the test project and found out what I allready assumed! My interface:
public interface ITest { bool PTest { get; set; } }
My ClassA (derived from Control, implementing ITest plus an additional property)
public class CTest\_A : System.Windows.Forms.Control, ITest { public CTest\_A() { } #region ITest Member private bool \_ptest=false; public bool PTest { get { return \_ptest; } set { \_ptest = value; } } #endregion private bool \_padditional\_A=false; public bool PAdditional\_A { get { return \_padditional\_A; } set { \_padditional\_A = value; } } }
My ClassB (also derived from Control, also implementing ITest plus an additional property)
public class CTest\_B : System.Windows.Forms.Control, ITest { public CTest\_B() { } #region ITest Member private bool \_ptest=false; public bool PTest { get { return \_ptest; } set { \_ptest = value; } } #endregion private bool \_padditional\_B=false; public bool PAdditional\_B { get { return \_padditional\_B; } set { \_padditional\_B = value; } } }
My test class: Has a an instance of CTest_B and whant's to cast it to CTest_A over the Interface, which is returned by a method!
CTest\_B C\_B = new CTest\_B(); private ITest GetIReference() { return C\_B as ITest; } public Form1() { InitializeComponent(); CTest\_A C\_A = (CTest\_A)GetIReference(); //Throws an invalid cast exception here }
Do I get something totaly wrong, or is it like I assumed? Please help!
All the best, Martin
-
Martin# wrote:
Colin Angus Mackay wrote: You can't instantiate an interface. True
Martin# wrote:
The method returns an IResponsableProcessingCSP instance
You argree with me, then contradict yourself. You cannot have an instance of an interface. You can only have a reference to one. The instance is of some concrete object that happens to implement the interface.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website
-
I just did the test project and found out what I allready assumed! My interface:
public interface ITest { bool PTest { get; set; } }
My ClassA (derived from Control, implementing ITest plus an additional property)
public class CTest\_A : System.Windows.Forms.Control, ITest { public CTest\_A() { } #region ITest Member private bool \_ptest=false; public bool PTest { get { return \_ptest; } set { \_ptest = value; } } #endregion private bool \_padditional\_A=false; public bool PAdditional\_A { get { return \_padditional\_A; } set { \_padditional\_A = value; } } }
My ClassB (also derived from Control, also implementing ITest plus an additional property)
public class CTest\_B : System.Windows.Forms.Control, ITest { public CTest\_B() { } #region ITest Member private bool \_ptest=false; public bool PTest { get { return \_ptest; } set { \_ptest = value; } } #endregion private bool \_padditional\_B=false; public bool PAdditional\_B { get { return \_padditional\_B; } set { \_padditional\_B = value; } } }
My test class: Has a an instance of CTest_B and whant's to cast it to CTest_A over the Interface, which is returned by a method!
CTest\_B C\_B = new CTest\_B(); private ITest GetIReference() { return C\_B as ITest; } public Form1() { InitializeComponent(); CTest\_A C\_A = (CTest\_A)GetIReference(); //Throws an invalid cast exception here }
Do I get something totaly wrong, or is it like I assumed? Please help!
All the best, Martin
Full disclosure: I didn't read all of your code. :) That said... Your GetIReference() returns an ITest reference, according to the sig. What it actually returns is a CTest_B object. No problems there. However, when you say
CTest_A C_A = (CTest_A)GetIReference();
you are essentially trying to cast a CTest_B object to a CTest_A reference. This will work only if CTest_A is an ancestor of CTest_B or a cast is defined. You haven't, so it bombs. :) Try making GetIReference() return an instance of CTest_A (don't change the sig!) and you will see what I meant. Greetings to Österreich. :)Cheers, Vıkram.
After all is said and done, much is said and little is done.
-
Full disclosure: I didn't read all of your code. :) That said... Your GetIReference() returns an ITest reference, according to the sig. What it actually returns is a CTest_B object. No problems there. However, when you say
CTest_A C_A = (CTest_A)GetIReference();
you are essentially trying to cast a CTest_B object to a CTest_A reference. This will work only if CTest_A is an ancestor of CTest_B or a cast is defined. You haven't, so it bombs. :) Try making GetIReference() return an instance of CTest_A (don't change the sig!) and you will see what I meant. Greetings to Österreich. :)Cheers, Vıkram.
After all is said and done, much is said and little is done.
Hello,
Vikram A Punathambekar wrote:
you are essentially trying to cast a CTest_B object to a CTest_A reference. This will work only if CTest_A is an ancestor of CTest_B or a cast is defined. You haven't, so it bombs.
YEP, That is what I was assuming from the beginning on and trying to explain the whole time! Because I think that's the problem of joelbasson[^].
Vikram A Punathambekar wrote:
Try making GetIReference() return an instance of CTest_A (don't change the sig!) and you will see what I meant
You mean that the compiler tells me that it's not possible?
Vikram A Punathambekar wrote:
Greetings to Österreich.
Thanks very much! Nice greetings to India as well.
All the best, Martin
-
Hello,
Vikram A Punathambekar wrote:
you are essentially trying to cast a CTest_B object to a CTest_A reference. This will work only if CTest_A is an ancestor of CTest_B or a cast is defined. You haven't, so it bombs.
YEP, That is what I was assuming from the beginning on and trying to explain the whole time! Because I think that's the problem of joelbasson[^].
Vikram A Punathambekar wrote:
Try making GetIReference() return an instance of CTest_A (don't change the sig!) and you will see what I meant
You mean that the compiler tells me that it's not possible?
Vikram A Punathambekar wrote:
Greetings to Österreich.
Thanks very much! Nice greetings to India as well.
All the best, Martin
Martin, I was just commenting on your original assertion - "But it was instanciated as IResponseProcessingCSP, and you can not Up'cast' it , I think!" This is wrong on two counts. First, you cannot instantiate an interface, as Colin pointed out. I'm sure you know this; it's probably just that you weren't using the correct terminology. Don't worry, your English is far better than my German. Second, when you say
Type1 reference1 = new Type2();
what is created is always Type2. It simply doesn't matter what type you use to hold the reference (in this case, Type1); the underlying object type is ALWAYS Type2. Therefore, assuming Type2 inherits from Type1 (directly or indirectly, it doesn't matter), a subsequent cast like
Type1 reference2 = reference1;
will always work. (You don't even need a cast!) Hope this clears things up. :)
Cheers, Vıkram.
After all is said and done, much is said and little is done.
-
Martin, I was just commenting on your original assertion - "But it was instanciated as IResponseProcessingCSP, and you can not Up'cast' it , I think!" This is wrong on two counts. First, you cannot instantiate an interface, as Colin pointed out. I'm sure you know this; it's probably just that you weren't using the correct terminology. Don't worry, your English is far better than my German. Second, when you say
Type1 reference1 = new Type2();
what is created is always Type2. It simply doesn't matter what type you use to hold the reference (in this case, Type1); the underlying object type is ALWAYS Type2. Therefore, assuming Type2 inherits from Type1 (directly or indirectly, it doesn't matter), a subsequent cast like
Type1 reference2 = reference1;
will always work. (You don't even need a cast!) Hope this clears things up. :)
Cheers, Vıkram.
After all is said and done, much is said and little is done.
Hello,
Vikram A Punathambekar wrote:
I'm sure you know this; it's probably just that you weren't using the correct terminology.
YEP!
Vikram A Punathambekar wrote:
Don't worry, your English is far better than my German
But I'm sure your German is much better than my Hindi. So the point goes to you! :-D
Vikram A Punathambekar wrote:
Second, when you say Type1 reference1 = new Type2();
Did I?
Vikram A Punathambekar wrote:
Therefore, assuming Type2 inherits from Type1 (directly or indirectly, it doesn't matter), a subsequent cast like Type1 reference2 = reference1; will always work. (You don't even need a cast!)
I hope I never suggested something else! I just wanted to find a possible explanation for the original problem, and tried to proof it with a project which is not working (means the project throws an exception, therefor the proof was ok). Too bad that the one who had the problem seems not to care any more. -- modified at 5:05 Tuesday 10th July, 2007
All the best, Martin