By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,187 Members | 1,594 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,187 IT Pros & Developers. It's quick & easy.

C++/CLI equivalent to as keyword?

P: n/a
How would this C# contruct be represented in C++/CLI? Or is it even
possible?

PolicyLevel level = (enumerator->Current) as PolicyLevel;

Thanks,
Karch
Nov 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a

karch wrote:
How would this C# contruct be represented in C++/CLI? Or is it even
possible?

PolicyLevel level = (enumerator->Current) as PolicyLevel;


PolicyLevel* level=dynamic_cast<PolicyLevel*>(enumerator->Current);

The equivalent of a cast in C# is __try_cast.

Arnaud
MVP - VC

Nov 17 '05 #2

P: n/a
ad******@club-internet.fr schrieb:
karch wrote:
How would this C# contruct be represented in C++/CLI? Or is it even
possible?

PolicyLevel level = (enumerator->Current) as PolicyLevel;

PolicyLevel* level=dynamic_cast<PolicyLevel*>(enumerator->Current);

The equivalent of a cast in C# is __try_cast.

Arnaud
MVP - VC

is __try_cast c++/CLI or managed c++ ? Or both ?
Nov 17 '05 #3

P: n/a

bonk wrote:
is __try_cast c++/CLI or managed c++ ? Or both ?


Managed C++. For C++/CLI, use safe_cast : Check MSDN for details
(a.k.a. RTFM)

Arnaud
MVP - VC

Nov 17 '05 #4

P: n/a
dynamic_cast is the equivalent to the C# "as".

static_cast is the equivalent in 2003 to basic C# casting (__try_cast is
strictly 2003, but is more of a development tool, not a production casting
technique).

safe_cast is the equivalent to basic C# casting in CLI.

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter

"ad******@club-internet.fr" wrote:

karch wrote:
How would this C# contruct be represented in C++/CLI? Or is it even
possible?

PolicyLevel level = (enumerator->Current) as PolicyLevel;


PolicyLevel* level=dynamic_cast<PolicyLevel*>(enumerator->Current);

The equivalent of a cast in C# is __try_cast.

Arnaud
MVP - VC

Nov 17 '05 #5

P: n/a
See : - How to: Implement is and as C# Keywords in C++
http://msdn2.microsoft.com/en-us/library/85af44e9

--
Regards,
Nish [VC++ MVP]
"karch" <no****@spamu.com> wrote in message
news:uF**************@TK2MSFTNGP10.phx.gbl...
How would this C# contruct be represented in C++/CLI? Or is it even
possible?

PolicyLevel level = (enumerator->Current) as PolicyLevel;

Thanks,
Karch

Nov 17 '05 #6

P: n/a

David Anton wrote:
dynamic_cast is the equivalent to the C# "as".

static_cast is the equivalent in 2003 to basic C# casting (__try_cast is
strictly 2003, but is more of a development tool, not a production casting
technique).


Why a "developement tool"? It gives you exactly the same behaviour as
C# casting, whereas static_cast result is undefined if the cast is
invalid. static_cast documentation for MC++ states explictely :
"Unchecked pointer casts can break the type safety of __gc pointers,
and cause the garbage collector to fail."

Arnaud
MVP - VC

Nov 17 '05 #7

P: n/a
In 'Managed C++ and .NET Development', Stephen Fraser gives a good
explanation for why you should regard __try_cast as a development tool -
basically, __try_cast exceptions will only occur if there are actual mistakes
in your code. But perhaps this is also close to what C# does.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter

"ad******@club-internet.fr" wrote:

David Anton wrote:
dynamic_cast is the equivalent to the C# "as".

static_cast is the equivalent in 2003 to basic C# casting (__try_cast is
strictly 2003, but is more of a development tool, not a production casting
technique).


Why a "developement tool"? It gives you exactly the same behaviour as
C# casting, whereas static_cast result is undefined if the cast is
invalid. static_cast documentation for MC++ states explictely :
"Unchecked pointer casts can break the type safety of __gc pointers,
and cause the garbage collector to fail."

Arnaud
MVP - VC

Nov 17 '05 #8

P: n/a
David Anton wrote:
In 'Managed C++ and .NET Development', Stephen Fraser gives a good
explanation for why you should regard __try_cast as a development
tool - basically, __try_cast exceptions will only occur if there are
actual mistakes in your code. We agree; that's the wole point : It's better to have an exception that
unmanaged behaviour and GC failure. Of course, it's still better to have an
error at build time, but you don't always have what you want ;-)

Now, if you are *really* performance worried (as much C++ developpers are,
generally without any measurement to prove that their worries are justified
;-), you can replace __try_cast by static_cast and *pray* not to have an
unnoticed invalid cast that will break hell loose in front of the client.
But perhaps this is also close to what
C# does.

AFAIK, __try_cast and C# casts do strictly the same thing.

Arnaud
MVP - VC
Nov 17 '05 #9

P: n/a
Good point Arnaud.
It's apparent that we need to change conversion of the basic C# cast to use
__try_cast for the 2003 target. (It's in today's build).

Our conversion to the CLI target is correct in using safe_cast.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter

"Arnaud Debaene" wrote:
David Anton wrote:
In 'Managed C++ and .NET Development', Stephen Fraser gives a good
explanation for why you should regard __try_cast as a development
tool - basically, __try_cast exceptions will only occur if there are
actual mistakes in your code.

We agree; that's the wole point : It's better to have an exception that
unmanaged behaviour and GC failure. Of course, it's still better to have an
error at build time, but you don't always have what you want ;-)

Now, if you are *really* performance worried (as much C++ developpers are,
generally without any measurement to prove that their worries are justified
;-), you can replace __try_cast by static_cast and *pray* not to have an
unnoticed invalid cast that will break hell loose in front of the client.
But perhaps this is also close to what
C# does.

AFAIK, __try_cast and C# casts do strictly the same thing.

Arnaud
MVP - VC

Nov 17 '05 #10

P: n/a

"Arnaud Debaene" <ad******@club-internet.fr> wrote in message
news:OF****************@TK2MSFTNGP12.phx.gbl...
David Anton wrote:
In 'Managed C++ and .NET Development', Stephen Fraser gives a good
explanation for why you should regard __try_cast as a development
tool - basically, __try_cast exceptions will only occur if there are
actual mistakes in your code.

We agree; that's the wole point : It's better to have an exception that
unmanaged behaviour and GC failure. Of course, it's still better to have
an error at build time, but you don't always have what you want ;-)

Now, if you are *really* performance worried (as much C++ developpers are,
generally without any measurement to prove that their worries are
justified ;-), you can replace __try_cast by static_cast and *pray* not to
have an unnoticed invalid cast that will break hell loose in front of the
client.
But perhaps this is also close to what
C# does.

AFAIK, __try_cast and C# casts do strictly the same thing.

Arnaud
MVP - VC


__try_cast (MC++) is recast into safe_cast ( C++/CLI) , both generate
exactly the same IL instruction (castclass) just like an ordinary cast in
C#.

Willy.

Nov 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.