473,657 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ equivalent for VB TypeOf operator?

Hi all,

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

void ReportError(Sys tem::Exception^ ex)
{
// Psuedo-VB syntax
if (TypeOf ex Is System::Operati onCanceledExcep tion) return;

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

TIA - Bob
Nov 11 '08 #1
6 4265
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(Sys tem::Exception^ ex)
{
// Psuedo-VB syntax
if (TypeOf ex Is System::Operati onCanceledExcep tion) return;

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

if (System::Operat ionCanceledExce ption^ opCanEx = dynamic_cast<Sy stem::Operation CanceledExcepti on^>(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::Operati onCanceledExcep tion and all the
subclasses of System::Operati onCanceledExcep tion in the if statement, we
can use Cholo's suggestion of dynamic_cast:

if (dynamic_cast<S ystem::Operatio nCanceledExcept ion^>(ex))
{
// if ex belongs to a subclass of System::Operati onCanceledExcep tion, 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::Operati onCanceledExcep tion and
disregard the subclasses of OperationCancel edException, the 'typeid'
keyword will be proper:
http://msdn.microsoft.com/en-us/library/kwd9abya.aspx

void ReportError(Sys tem::Exception^ ex)
{
if (ex->GetType() == System::Operati onCanceledExcep tion::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****@microsof t.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*********@ho tmail.comkirjut as:
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(Sys tem::Exception^ ex)
{
// Psuedo-VB syntax
if (TypeOf ex Is System::Operati onCanceledExcep tion) 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 "resurrecte d" by another function:

std::string ResurrectExcept ion();

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

Nov 11 '08 #5
Paavo Helde wrote:
"Cholo Lennon" <ch*********@ho tmail.comkirjut as:
>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(Sys tem::Exception^ ex)
{
// Psuedo-VB syntax
if (TypeOf ex Is System::Operati onCanceledExcep tion) 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 "resurrecte d" by another function:

std::string ResurrectExcept ion();

int f() {
try {
// actual code ...
} catch(...) {
std::string msg = ResurrectExcept ion();
std::cerr << "Exception: " << msg << "\n";
}
}
std::string ResurrectExcept ion() {
try {
throw; // valid only when called from inside a catch block
} catch(const std::exception& e) {
return e.what();
} catch(int e) {
std::ostringstr eam os;
os << e;
return "Int exception: " + os.str();
} catch(const my_private_exce ption& e) {
return e.ToString();
} catch(...) {
return "Unknown exception";
}
}
--
Cholo Lennon
Bs.As.
ARG
Nov 12 '08 #6
"Cholo Lennon" <ch*********@ho tmail.comkirjut as:
>
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
58247
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 working. Thanks
4
299
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 them and I need to know what kind of item a user clicked on so I can use it for drag and drop
3
950
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
5609
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
14
2540
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 both statements are similar : *((char **)v)++ == *((char **)v++) Where v is a pointer to an array of characters,defined as char *v;
9
10720
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 as a valid return value for the typeof operator in a later version of ECMAScript or is this a JScript "feature"? -- Klaus Johannes Rusch
20
9278
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues = displayValuesArray.length; for (i=0; i<nDisplayValues; i++) {
32
4039
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 js equivalent, or some other way to handle this? Andrew Poulos
5
5731
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 looks promising but then is there a way of referncing a value without having to use the dot operator.
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8723
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8602
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7316
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
2
1601
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.