473,320 Members | 1,950 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++ equivalent for VB TypeOf operator?

Hi all,

What's the C++ equivalent for the VB TypeOf operator. For example:

void ReportError(System::Exception^ ex)
{
// Psuedo-VB syntax
if (TypeOf ex Is System::OperationCanceledException) return;

// Decode the exception and tell the user about it...
<...>
}

TIA - Bob
Nov 11 '08 #1
6 4251
Bob Altman wrote:
Hi all,

What's the C++ equivalent for the VB TypeOf operator. For example:
dynamic_cast (http://msdn.microsoft.com/en-us/library/cby9kycs.aspx)

The operator returns 0 (or nullptr in C++ CLI) if the convertion fails.
void ReportError(System::Exception^ ex)
{
// Psuedo-VB syntax
if (TypeOf ex Is System::OperationCanceledException) return;

// Decode the exception and tell the user about it...
<...>
}
The general pattern to use dynamic_cast is:

if (System::OperationCanceledException^ opCanEx = dynamic_cast<System::OperationCanceledException^>( ex)) {
// Use opCanEx
...
}

Regards

--
Cholo Lennon
Bs.As.
ARG
Nov 11 '08 #2
Good morning Bob and Cholo

If you would like to handle System::OperationCanceledException and all the
subclasses of System::OperationCanceledException in the if statement, we
can use Cholo's suggestion of dynamic_cast:

if (dynamic_cast<System::OperationCanceledException^> (ex))
{
// if ex belongs to a subclass of System::OperationCanceledException, the
above if statement will still return true.
// Decode the exception and tell the user about it
}

If you would like to only handle System::OperationCanceledException and
disregard the subclasses of OperationCanceledException, the 'typeid'
keyword will be proper:
http://msdn.microsoft.com/en-us/library/kwd9abya.aspx

void ReportError(System::Exception^ ex)
{
if (ex->GetType() == System::OperationCanceledException::typeid)
{
// Decode the exception and tell the user about it
}
}

Please let me know whether the above information is helpful to you or not.
If you have any other questions/concerns, feel free to tell me.

Have a very nice day!

Regards,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 11 '08 #3
Thanks a million!
Nov 11 '08 #4
"Cholo Lennon" <ch*********@hotmail.comkirjutas:
Bob Altman wrote:
>Hi all,

What's the C++ equivalent for the VB TypeOf operator. For example:

dynamic_cast (http://msdn.microsoft.com/en-us/library/cby9kycs.aspx)

The operator returns 0 (or nullptr in C++ CLI) if the convertion
fails.
>void ReportError(System::Exception^ ex)
{
// Psuedo-VB syntax
if (TypeOf ex Is System::OperationCanceledException) return;

// Decode the exception and tell the user about it...
<...>
dynamic_cast only works on polymorphic pointers/references (i.e. having
at least one virtual function). While it's true that std::exception is
polymorphic, there might be other exception objects which are not. In C++
it is possible to distinguish them, but only if you know the types in
advance.

In such a situation, it is often the case that one does not have an idea
which exceptions might arise, so I give an example with a catch(...)
clause. This by itself does not give any information about the exception
type so it has to be "resurrected" by another function:

std::string ResurrectException();

int f() {
try {
// actual code ...
} catch(...) {
std::string msg = ResurrectException();
std::cerr << "Exception: " << msg << "\n";
}
}
std::string ResurrectException() {
try {
throw; // valid only when called from inside a catch block
} catch(const std::exception& e) {
return e.what();
} catch(int e) {
std::ostringstream os;
os << e;
return "Int exception: " + os.str();
} catch(const my_private_exception& e) {
return e.ToString();
} catch(...) {
return "Unknown exception";
}
}

Nov 11 '08 #5
Paavo Helde wrote:
"Cholo Lennon" <ch*********@hotmail.comkirjutas:
>Bob Altman wrote:
>>Hi all,

What's the C++ equivalent for the VB TypeOf operator. For example:

dynamic_cast (http://msdn.microsoft.com/en-us/library/cby9kycs.aspx)

The operator returns 0 (or nullptr in C++ CLI) if the convertion
fails.
>>void ReportError(System::Exception^ ex)
{
// Psuedo-VB syntax
if (TypeOf ex Is System::OperationCanceledException) return;

// Decode the exception and tell the user about it...
<...>

dynamic_cast only works on polymorphic pointers/references (i.e.
having at least one virtual function). While it's true that
std::exception is polymorphic, there might be other exception objects
which are not. In C++ it is possible to distinguish them, but only if
you know the types in advance.
You're right, but just a clarification: the OP asked about "TypeOf" with a .Net exception (not std::exception). BTW all .Net
exceptions are polymorphics.

Regards

>
In such a situation, it is often the case that one does not have an
idea which exceptions might arise, so I give an example with a
catch(...) clause. This by itself does not give any information about
the exception type so it has to be "resurrected" by another function:

std::string ResurrectException();

int f() {
try {
// actual code ...
} catch(...) {
std::string msg = ResurrectException();
std::cerr << "Exception: " << msg << "\n";
}
}
std::string ResurrectException() {
try {
throw; // valid only when called from inside a catch block
} catch(const std::exception& e) {
return e.what();
} catch(int e) {
std::ostringstream os;
os << e;
return "Int exception: " + os.str();
} catch(const my_private_exception& e) {
return e.ToString();
} catch(...) {
return "Unknown exception";
}
}
--
Cholo Lennon
Bs.As.
ARG
Nov 12 '08 #6
"Cholo Lennon" <ch*********@hotmail.comkirjutas:
>
You're right, but just a clarification: the OP asked about "TypeOf"
with a .Net exception (not std::exception).
Yes sorry, I have to check more carefully in which ng I am posting!

Cheers
Paavo
Nov 12 '08 #7

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

Similar topics

3
by: effendi | last post by:
Hi Can any tell me what is the javascript equivalent of CSS border? I would like to change the border of my cell when it is set on focus. I have tried onFocus="style.border='3px'" but it is not...
4
by: Eric | last post by:
I need to do the following but it doesn't compile if(typeof(listBox1.Items) == typeof(string)){ return; } typeof(listBox1.Items) doesn't work at all. My listBox has 2 types of items in...
3
by: Alberto | last post by:
Can somebody tell me why this typeof doesn't work? foreach (Control myControl in Controls) if (typeof(myControl) == "TextBox") ((TextBox)myControl).Text = string.Empty; Thank you very much
11
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
14
by: grid | last post by:
Hi, I have a certain situation where a particular piece of code works on a particular compiler but fails on another proprietary compiler.It seems to have been fixed but I just want to confirm if...
9
by: Klaus Johannes Rusch | last post by:
IE7 returns "unknown" instead of "undefined" when querying the type of an unknown property of an object, for example document.write(typeof window.missingproperty); Has "unknown" been defined...
20
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues =...
32
by: Andrew Poulos | last post by:
I'm writing some ASP using js and I need to do a case sensitive SQL select. Googling gave me this: SELECT * FROM User WHERE Strcomp("Blue",,vbBinaryCompare)=0 Strcomp is from vbs. Is there a...
5
by: Greg | last post by:
I can see there is no simple way to define a type like just a simple C++ typedef, such as typdef signaltype double; Using #define seems like a bad idea if it works atall. Using a struct...
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...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.