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. Managed C++/CLI
  4. Covariant return types

Covariant return types

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++databasevisual-studiohelp
8 Posts 3 Posters 11 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.
  • V Offline
    V Offline
    VizOne
    wrote on last edited by
    #1

    Hi! I'd like to implement my own Collection that derives from Collection Base. I want the Item-property to have the proper return type instead of Object*. In C# it is easy to implement a Covariant return type: class MyData { public int Value { get { return 5; } } }; class MyCollection : CollectionBase { public MyData this[int index] // override indexer from IList { get{ return (MyData)List[index]; } set{ List[index] = value; } } }; In VC++.net, however it does not seem to be possible, as covariant return types are not allowed - at least in VC++.net: __gc class MyData { __property int get_Value() { return 5; } }; [Reflection::DefaultMember(S"Item")] // let Item be the indexer __gc class MyCollection : public System::Collections::CollectionBase { __property MyData * get_Item(int in_index) // C2392- Error! { return static_cast<MyData*>(List->Item[in_index]); } }; Is there any attribute or another way to achieve what I'd like to do? I am using VS.net 2002. Might this feature be available in vs.net 2003? Thanks in advance! - Andre

    P 1 Reply Last reply
    0
    • V VizOne

      Hi! I'd like to implement my own Collection that derives from Collection Base. I want the Item-property to have the proper return type instead of Object*. In C# it is easy to implement a Covariant return type: class MyData { public int Value { get { return 5; } } }; class MyCollection : CollectionBase { public MyData this[int index] // override indexer from IList { get{ return (MyData)List[index]; } set{ List[index] = value; } } }; In VC++.net, however it does not seem to be possible, as covariant return types are not allowed - at least in VC++.net: __gc class MyData { __property int get_Value() { return 5; } }; [Reflection::DefaultMember(S"Item")] // let Item be the indexer __gc class MyCollection : public System::Collections::CollectionBase { __property MyData * get_Item(int in_index) // C2392- Error! { return static_cast<MyData*>(List->Item[in_index]); } }; Is there any attribute or another way to achieve what I'd like to do? I am using VS.net 2002. Might this feature be available in vs.net 2003? Thanks in advance! - Andre

      P Offline
      P Offline
      Paul Selormey
      wrote on last edited by
      #2

      Hello Andre, I am not currently on my .NET machine to investigate your case. However, all my collections for a product, we are about to release, does just that without any problem. I use VS.NET 2003 (but I do not think it is the issue). To prove the point, here is a code I have copied for a real .NET component.

      ////////////////////////////////////////////////////////////////////////////
      // The System::Collections collection interface implementations

      public:
      __property MapTheme* get_Item(int index)
      {
      return __try_cast<MapTheme*>(List->Item[index]);
      }

      \_\_property void set\_Item(int index, MapTheme\* value)
      {
      	List->Item\[index\] = value;
      }
      

      So the problem may not be the issue of "Covariant return types". Do you have implementation for the set_Item property too? Also, your code seems to make the get_Item property private, is that what the actual code you compiled is? Best regards, Paul. Jesus Christ is LOVE! Please tell somebody.

      V 1 Reply Last reply
      0
      • P Paul Selormey

        Hello Andre, I am not currently on my .NET machine to investigate your case. However, all my collections for a product, we are about to release, does just that without any problem. I use VS.NET 2003 (but I do not think it is the issue). To prove the point, here is a code I have copied for a real .NET component.

        ////////////////////////////////////////////////////////////////////////////
        // The System::Collections collection interface implementations

        public:
        __property MapTheme* get_Item(int index)
        {
        return __try_cast<MapTheme*>(List->Item[index]);
        }

        \_\_property void set\_Item(int index, MapTheme\* value)
        {
        	List->Item\[index\] = value;
        }
        

        So the problem may not be the issue of "Covariant return types". Do you have implementation for the set_Item property too? Also, your code seems to make the get_Item property private, is that what the actual code you compiled is? Best regards, Paul. Jesus Christ is LOVE! Please tell somebody.

        V Offline
        V Offline
        VizOne
        wrote on last edited by
        #3

        Hi Paul Just to give you an example: public __gc class MyData { /**/ }; public __gc class MyCollection : public CollectionBase { public: __property MyData * get_Item(int in_index) { return static_cast<MyData*>(List->Item[in_index]); } __property void set_Item(int in_index, MyData * in_data) { List->Item[in_index] = in_data; } }; The (German) output is: Covariant4.cpp(19) : error C3815: Der Rückgabetyp der Methode 'MyCollection::get_Item' muss mit dem Typ des letzten Parameters von 'System::Collections::IList::set_Item' übereinstimmen Covariant4.cpp(18) : Siehe Deklaration von 'MyCollection::get_Item' Covariant4.cpp(6) : Siehe Deklaration von 'System::Collections::IList::set_Item' Covariant4.cpp(19) : error C2392: 'MyData __gc *MyCollection::get_Item(int)' : Covariant-Rückgabetypen werden in verwalteten Typen nicht unterstützt Translated: C3815: Return value of 'MyCollection::get_Item' must match the type of the last parameter of 'System::Collections::IList::set_Item' C2392: 'MyData __gc *MyCollection::get_Item(int)': Covariant return types are not supported in managed types It seems to me as if this has been fixed in vs2003. Damn, I'm looking forward to it :) - Andre

        P 2 Replies Last reply
        0
        • V VizOne

          Hi Paul Just to give you an example: public __gc class MyData { /**/ }; public __gc class MyCollection : public CollectionBase { public: __property MyData * get_Item(int in_index) { return static_cast<MyData*>(List->Item[in_index]); } __property void set_Item(int in_index, MyData * in_data) { List->Item[in_index] = in_data; } }; The (German) output is: Covariant4.cpp(19) : error C3815: Der Rückgabetyp der Methode 'MyCollection::get_Item' muss mit dem Typ des letzten Parameters von 'System::Collections::IList::set_Item' übereinstimmen Covariant4.cpp(18) : Siehe Deklaration von 'MyCollection::get_Item' Covariant4.cpp(6) : Siehe Deklaration von 'System::Collections::IList::set_Item' Covariant4.cpp(19) : error C2392: 'MyData __gc *MyCollection::get_Item(int)' : Covariant-Rückgabetypen werden in verwalteten Typen nicht unterstützt Translated: C3815: Return value of 'MyCollection::get_Item' must match the type of the last parameter of 'System::Collections::IList::set_Item' C2392: 'MyData __gc *MyCollection::get_Item(int)': Covariant return types are not supported in managed types It seems to me as if this has been fixed in vs2003. Damn, I'm looking forward to it :) - Andre

          P Offline
          P Offline
          Paul Selormey
          wrote on last edited by
          #4

          I see, I will try it on my VS.NET 2002 tomorrow and report on the result - glad I made the switch before my company could decide ;P In any case, VS.NET 2003 is a product designed for the MC++ developer. There is hardly any new for the C#/VB.NET components. Best regards, Paul. Jesus Christ is LOVE! Please tell somebody.

          V 1 Reply Last reply
          0
          • P Paul Selormey

            I see, I will try it on my VS.NET 2002 tomorrow and report on the result - glad I made the switch before my company could decide ;P In any case, VS.NET 2003 is a product designed for the MC++ developer. There is hardly any new for the C#/VB.NET components. Best regards, Paul. Jesus Christ is LOVE! Please tell somebody.

            V Offline
            V Offline
            VizOne
            wrote on last edited by
            #5

            Paul Selormey wrote: In any case, VS.NET 2003 is a product designed for the MC++ developer. There is hardly any new for the C#/VB.NET components. Yes, I realized that. In fact, I guess vc++.net 2003 is what vc++.net 2002 should have been if the VC++ team had more time. - Andre

            P 1 Reply Last reply
            0
            • V VizOne

              Paul Selormey wrote: In any case, VS.NET 2003 is a product designed for the MC++ developer. There is hardly any new for the C#/VB.NET components. Yes, I realized that. In fact, I guess vc++.net 2003 is what vc++.net 2002 should have been if the VC++ team had more time. - Andre

              P Offline
              P Offline
              Paul Selormey
              wrote on last edited by
              #6

              VizOne wrote: Yes, I realized that. In fact, I guess vc++.net 2003 is what vc++.net 2002 should have been if the VC++ team had more time. I do not think it is an issue of time since it took four years of development from VC++ 6.0 release. With very little improvement in MFC, and some web-stuff updates for ATL four years was enough to do effective work. May be the resources the VC++ team was cut down in favor of C# and when they saw the developer reaction they went back to push MC++ up a bit. Anyway, I am back to work and will check on the problem with VS.NET 2002 today. Best regards, Paul. Jesus Christ is LOVE! Please tell somebody.

              D 1 Reply Last reply
              0
              • V VizOne

                Hi Paul Just to give you an example: public __gc class MyData { /**/ }; public __gc class MyCollection : public CollectionBase { public: __property MyData * get_Item(int in_index) { return static_cast<MyData*>(List->Item[in_index]); } __property void set_Item(int in_index, MyData * in_data) { List->Item[in_index] = in_data; } }; The (German) output is: Covariant4.cpp(19) : error C3815: Der Rückgabetyp der Methode 'MyCollection::get_Item' muss mit dem Typ des letzten Parameters von 'System::Collections::IList::set_Item' übereinstimmen Covariant4.cpp(18) : Siehe Deklaration von 'MyCollection::get_Item' Covariant4.cpp(6) : Siehe Deklaration von 'System::Collections::IList::set_Item' Covariant4.cpp(19) : error C2392: 'MyData __gc *MyCollection::get_Item(int)' : Covariant-Rückgabetypen werden in verwalteten Typen nicht unterstützt Translated: C3815: Return value of 'MyCollection::get_Item' must match the type of the last parameter of 'System::Collections::IList::set_Item' C2392: 'MyData __gc *MyCollection::get_Item(int)': Covariant return types are not supported in managed types It seems to me as if this has been fixed in vs2003. Damn, I'm looking forward to it :) - Andre

                P Offline
                P Offline
                Paul Selormey
                wrote on last edited by
                #7

                Hello Andre, I have just tried this with VS.NET 2002 and 2003. I constructed a VS.NET 2002 project using the CollectionBase class and it failed to compile. Then I opened the project in VS.NET 2003 and it compiled successfully!!! Please look for an upgrade route now to avoid headache. Best regards, Paul. Jesus Christ is LOVE! Please tell somebody.

                1 Reply Last reply
                0
                • P Paul Selormey

                  VizOne wrote: Yes, I realized that. In fact, I guess vc++.net 2003 is what vc++.net 2002 should have been if the VC++ team had more time. I do not think it is an issue of time since it took four years of development from VC++ 6.0 release. With very little improvement in MFC, and some web-stuff updates for ATL four years was enough to do effective work. May be the resources the VC++ team was cut down in favor of C# and when they saw the developer reaction they went back to push MC++ up a bit. Anyway, I am back to work and will check on the problem with VS.NET 2002 today. Best regards, Paul. Jesus Christ is LOVE! Please tell somebody.

                  D Offline
                  D Offline
                  Daniel Turini
                  wrote on last edited by
                  #8

                  Paul Selormey wrote: I do not think it is an issue of time since it took four years of development from VC++ 6.0 release. With very little improvement in MFC, and some web-stuff updates for ATL four years was enough to do effective work. Or maybe they could only start their work after most parts of .NET framework were done.


                  It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)

                  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