unable to cast object of type 'X' to type 'X'
-
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
-
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
Martin# wrote:
Vikram A Punathambekar wrote: Second, when you say Type1 reference1 = new Type2(); Did I?
Hee hee, when I said "When you say..." I did not imply that you said those words. I was presenting a hypothetical scenario, not saying you said something. BTW, there are far too many languages spoken in India to list. While Hindi is something I'm proficient in, I'm neither very fond of it nor is it my native tongue. About the casting, I think there's just a misunderstanding. Forget all we talked about in this thread and keep up the good work. :)
Cheers, Vıkram.
After all is said and done, much is said and little is done.
-
Martin# wrote:
Vikram A Punathambekar wrote: Second, when you say Type1 reference1 = new Type2(); Did I?
Hee hee, when I said "When you say..." I did not imply that you said those words. I was presenting a hypothetical scenario, not saying you said something. BTW, there are far too many languages spoken in India to list. While Hindi is something I'm proficient in, I'm neither very fond of it nor is it my native tongue. About the casting, I think there's just a misunderstanding. Forget all we talked about in this thread and keep up the good work. :)
Cheers, Vıkram.
After all is said and done, much is said and little is done.
Vikram A Punathambekar wrote:
Hee hee, when I said "When you say..." I did not imply that you said those words. I was presenting a hypothetical scenario, not saying you said something.
Vikram A Punathambekar wrote:
About the casting, I think there's just a misunderstanding. Forget all we talked about in this thread and keep up the good work.
:)
Vikram A Punathambekar wrote:
Forget all we talked about in this thread and keep up the good work.
This perfectly fitts to you Signature (Which allways make me laugh when reading) :laugh:
Vikram A Punathambekar wrote:
BTW, there are far too many languages spoken in India to list
Actually, I looked it up bevore answering you! I choosed the one with the most percentage :-D Here is the link to the German Wikipedia page, for your training. http://de.wikipedia.org/wiki/Indische_Sprachen[^]
All the best, Martin
-
Vikram A Punathambekar wrote:
Hee hee, when I said "When you say..." I did not imply that you said those words. I was presenting a hypothetical scenario, not saying you said something.
Vikram A Punathambekar wrote:
About the casting, I think there's just a misunderstanding. Forget all we talked about in this thread and keep up the good work.
:)
Vikram A Punathambekar wrote:
Forget all we talked about in this thread and keep up the good work.
This perfectly fitts to you Signature (Which allways make me laugh when reading) :laugh:
Vikram A Punathambekar wrote:
BTW, there are far too many languages spoken in India to list
Actually, I looked it up bevore answering you! I choosed the one with the most percentage :-D Here is the link to the German Wikipedia page, for your training. http://de.wikipedia.org/wiki/Indische_Sprachen[^]
All the best, Martin
Martin# wrote:
This perfectly fitts to you Signature (Which allways make me laugh when reading)
I thought my sig was profound and thought-provoking. :~
Martin# wrote:
I choosed the one with the most percentage
My mother tongue is Marathi. The Marathas were one of the most powerful groups in medieval India, resisting the Muslim invaders and even driving them back.
Martin# wrote:
Here is the link to the German Wikipedia page, for your training.
Seeing words like sprachwissenschaftliche and gleichberechtigten makes my head spin. :-D German seems to have a lot of long words. Is it true that there is no neuter gender in German and everything is male/female?
Cheers, Vıkram.
After all is said and done, much is said and little is done.
-
Martin# wrote:
This perfectly fitts to you Signature (Which allways make me laugh when reading)
I thought my sig was profound and thought-provoking. :~
Martin# wrote:
I choosed the one with the most percentage
My mother tongue is Marathi. The Marathas were one of the most powerful groups in medieval India, resisting the Muslim invaders and even driving them back.
Martin# wrote:
Here is the link to the German Wikipedia page, for your training.
Seeing words like sprachwissenschaftliche and gleichberechtigten makes my head spin. :-D German seems to have a lot of long words. Is it true that there is no neuter gender in German and everything is male/female?
Cheers, Vıkram.
After all is said and done, much is said and little is done.
-
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?