473,554 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7308

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_c ast<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_c ast<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******@c lub-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_c ast<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.c om> wrote in message
news:uF******** ******@TK2MSFTN GP10.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 "developeme nt 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******@c lub-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 "developeme nt 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
50349
by: Phil Powell | last post by:
<?php class SuperClass { var $mySuperClassVar; function SuperClass($myVar) { $this->mySuperClassVar = $myVar; echo "super class var = $myVar<p>"; }
9
2053
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
3260
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
6091
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 : A {
5
1281
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 someone has an idea? Thanks in advance
10
6101
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
2247
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
6303
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
8190
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 order to obtain the amount of physical memory on the server, I would do the following in shell: grep ^MemTotal /proc/meminfo | awk '{print $2}' ...
3
1470
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
7579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8013
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7531
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5414
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1994
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1112
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.