Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. unable to cast object of type 'X' to type 'X'

unable to cast object of type 'X' to type 'X'

Scheduled Pinned Locked Moved C#
helpquestion
25 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joelbasson
    wrote on last edited by
    #1

    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?

    C M A 3 Replies Last reply
    0
    • J joelbasson

      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?

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • J joelbasson

        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?

        M Offline
        M Offline
        Martin 0
        wrote on last edited by
        #3

        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

        C V 2 Replies Last reply
        0
        • M Martin 0

          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

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          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

          P M 2 Replies Last reply
          0
          • C Colin Angus Mackay

            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

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            So far I've never used as, I use is to test and then cast as appropriate.

            L 1 Reply Last reply
            0
            • P PIEBALDconsult

              So far I've never used as, I use is to test and then cast as appropriate.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              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] }


              M P 2 Replies Last reply
              0
              • L Luc Pattyn

                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] }


                M Offline
                M Offline
                Martin 0
                wrote on last edited by
                #7

                YES, I also whant to prevent the double typecheck! And there are very few situations where I do not whant to cast and only make the check. (1 out of 100 in my project)

                All the best, Martin

                1 Reply Last reply
                0
                • C Colin Angus Mackay

                  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

                  M Offline
                  M Offline
                  Martin 0
                  wrote on last edited by
                  #8

                  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

                  C 1 Reply Last reply
                  0
                  • M Martin 0

                    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

                    C Offline
                    C Offline
                    Colin Angus Mackay
                    wrote on last edited by
                    #9

                    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

                    M 2 Replies Last reply
                    0
                    • L Luc Pattyn

                      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] }


                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      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.

                      L 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        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.

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        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] }


                        1 Reply Last reply
                        0
                        • M Martin 0

                          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

                          V Offline
                          V Offline
                          Vikram A Punathambekar
                          wrote on last edited by
                          #12

                          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.

                          M 2 Replies Last reply
                          0
                          • C Colin Angus Mackay

                            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

                            M Offline
                            M Offline
                            Martin 0
                            wrote on last edited by
                            #13

                            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

                            1 Reply Last reply
                            0
                            • V Vikram A Punathambekar

                              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.

                              M Offline
                              M Offline
                              Martin 0
                              wrote on last edited by
                              #14

                              Hello, Thanks for your help! I think I have to do a little test programm to try something out!

                              All the best, Martin

                              1 Reply Last reply
                              0
                              • V Vikram A Punathambekar

                                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.

                                M Offline
                                M Offline
                                Martin 0
                                wrote on last edited by
                                #15

                                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

                                V 1 Reply Last reply
                                0
                                • C Colin Angus Mackay

                                  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

                                  M Offline
                                  M Offline
                                  Martin 0
                                  wrote on last edited by
                                  #16

                                  Hello Colin, Maybe you find time to look threw my example[^]! Hope you can help me! Thanks again for your time!

                                  All the best, Martin

                                  1 Reply Last reply
                                  0
                                  • M Martin 0

                                    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

                                    V Offline
                                    V Offline
                                    Vikram A Punathambekar
                                    wrote on last edited by
                                    #17

                                    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.

                                    M 1 Reply Last reply
                                    0
                                    • V Vikram A Punathambekar

                                      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.

                                      M Offline
                                      M Offline
                                      Martin 0
                                      wrote on last edited by
                                      #18

                                      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

                                      V 1 Reply Last reply
                                      0
                                      • M Martin 0

                                        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

                                        V Offline
                                        V Offline
                                        Vikram A Punathambekar
                                        wrote on last edited by
                                        #19

                                        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.

                                        M 1 Reply Last reply
                                        0
                                        • V Vikram A Punathambekar

                                          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.

                                          M Offline
                                          M Offline
                                          Martin 0
                                          wrote on last edited by
                                          #20

                                          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

                                          V 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups