473,320 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 7278

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 = $myVar<p>"; }
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 methods in derived classes?
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 { private void Init() { // code } } class B...
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 equivalent keyword of partial in VC++ does...
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#? Thank you.
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 Linux server using the /proc FS. For example, in...
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' AS type FROM table WHERE column2 = 'foo'
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.