472,353 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

C++/CLI equivalent to as keyword?

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
10 7153

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

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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
<?php class SuperClass { var $mySuperClassVar; function SuperClass($myVar) { $this->mySuperClassVar = $myVar; echo "super class var =...
9
by: Krishnan | last post by:
Hi, Just curious to know if there's a keyword equivalent to "Myclass" in VB.Net in C# TIA Krishnan
8
by: FDude | last post by:
In otherwords, how do I force a method in my class to call the method implementations in the SAME class and not use any potentially overriden...
9
by: Chazza | last post by:
I would like to override a method from an inherited class, but the new method has a different signature to the inherited class. Example: class A...
5
by: tlemcenvisit | last post by:
Hello in C# : Partial type definitions allow the definition of a class, struct or interface to be split into multiple files. I'd like to have the...
10
by: Bishoy | last post by:
Hi, In VB.NET there is a keyword called "My" which has a lot of properties collected at it. Is there any equivalent to this "My" in C#? ...
12
by: rodchar | last post by:
hey all, vb has a static keyword for variables, what is charp's equivalent,please? static tempVar as String thanks, rodchar
1
by: trialproduct2004 | last post by:
Hi all, can any one tell me use of shadow keyword in vb.net and its equivalent in c# with e.g. Please help me. thanks in advance.
15
by: tereglow | last post by:
Hello all, I come from a shell/perl background and have just to learn python. To start with, I'm trying to obtain system information from a...
3
by: Friedman, Jason | last post by:
I have lines that look like this: select column1, 'select' as type from table where column2 = 'foo' I want to return: SELECT column1, 'select'...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.