Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2004

Tip: Looking for answers? Try searching our database.

translate c# to c++

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
thwei - 14 Jul 2004 17:07 GMT
How to translate this to c++

public new Bar this[int nIndex]

{

get{ return (Bar)base[nIndex]; }

}
Brandon Bray [MSFT] - 14 Jul 2004 19:31 GMT
> How to translate this to c++
>
[quoted text clipped - 5 lines]
>
> }

In the new C++ syntax, this translates to (assuming Bar is a ref class):

property Bar^ default [int] {
 Bar^ get(int nIndex) { return (Bar^)BaseClassName::default[nIndex]; }
}

In the old syntax, it would be something like this:

__property Bar* get_Item(int nIndex) {
 return __try_cast<Bar*>(this->BaseClassName::Item[nIndex]; }
}

In the old syntax, you also need to put this attribute on the containing
__gc class: [System::Reflection::DefaultMember(S"Item")]

Signature

Brandon Bray, Visual C++ Compiler         http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.

thwei - 14 Jul 2004 20:52 GMT
> In the old syntax, it would be something like this:
>
[quoted text clipped - 4 lines]
> In the old syntax, you also need to put this attribute on the containing
> __gc class: [System::Reflection::DefaultMember(S"Item")]

Type Item of base class is Object and this generated Error C2392 "covariant
returns types are not supported in managed types"
Brandon Bray [MSFT] - 17 Jul 2004 00:45 GMT
> Type Item of base class is Object and this generated Error C2392
> "covariant returns types are not supported in managed types"

Then the property type needs to be Object. The CLR doesn't support covariant
returns from virtual functions.

Signature

Brandon Bray, Visual C++ Compiler         http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.

Hasani \(remove nospam from address\) - 14 Jul 2004 20:26 GMT
Assuming the class that contains that property derives from a class called
Bar.

[System::Reflection::DefaultMember(S"Item")]
public __gc class Bar
{
   public:
 __property Bar* get_Item(int index)
 {
  return NULL;
 }
};

public __gc class Foo : public Bar
{
public:
__property Bar* get_Item(int index)
{
 return Bar::get_Item(index);
}
};

> How to translate this to c++
>
[quoted text clipped - 5 lines]
>
> }
thwei - 14 Jul 2004 20:43 GMT
U?ytkownik "Hasani (remove nospam from address)"
<hblackwell@n0sp4m.popstick.com> napisa? w wiadomo?ci
news:eXzJJidaEHA.3988@tk2msftngp13.phx.gbl...
> Assuming the class that contains that property derives from a class called
> Bar.
[quoted text clipped - 17 lines]
>  }
> };

Yes, but this one problem:
public __gc class Bar
{
    public:
  __property Object* get_Item(int index)
  {
   return NULL;
  }
};

public __gc class Foo : public Bar
{
public:
 __property Bar* get_Item(int index) //Error C2392 covariant returns types
are not supported in managed types
 {
  return Bar::get_Item(index);
 }
};

Exactly:

public __gc class Foo : public ArrayList
{
public:
 __property Bar* get_Item(int index) //Error C2392 covariant returns types
are not supported in managed types
 {
  return Bar::get_Item(index);
 }
};
Hasani \(remove nospam from address\) - 14 Jul 2004 21:16 GMT
If you're intention is to derive Foo from ArrayList, you should instead
derive from CollectionBase. The problem is, an array list can store anything
derived from System.Object. In c#, you can use the 'new' keyword to overload
a method, but if your derived class is casted to an IList or an ArrayList,
nothing is to stop the user from calling IList[X] = Foo, or ArrayList[Y] =
Y.

> U?ytkownik "Hasani (remove nospam from address)"
> <hblackwell@n0sp4m.popstick.com> napisa? w wiadomo?ci
[quoted text clipped - 52 lines]
>   }
>  };
thwei - 15 Jul 2004 09:17 GMT
Thanks. Its must derived over CollectionBase and work ok. Thanks again.

U?ytkownik "Hasani (remove nospam from address)"
<hblackwell@n0sp4m.popstick.com> napisa? w wiadomo?ci
news:OnESF%23daEHA.3112@tk2msftngp13.phx.gbl...
> If you're intention is to derive Foo from ArrayList, you should instead
> derive from CollectionBase. The problem is, an array list can store anything
> derived from System.Object. In c#, you can use the 'new' keyword to overload
> a method, but if your derived class is casted to an IList or an ArrayList,
> nothing is to stop the user from calling IList[X] = Foo, or ArrayList[Y] =
> Y.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.