473,385 Members | 1,730 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,385 software developers and data experts.

Easy casting question (hopefully)


I'm getting a boolean property through reflection. That
part works fine. Since PropertyInfo->GetValue returns
Object*, to use it I need to cast it to a bool.

This seems like (and likely is) the simplest thing, but
i've been looking far and wide but can't seem to find the
answer. The docs seem to say that:

*dynamic_cast<__box Boolean*>(myBoolObject)

would do the trick. That works fine for my Int32
properties...but bombs on bools. Can anyone point me in
the right direction? (i'm relegated to using ToString and
comparing it to "True", and that's just cheezy :-)

Thanks in advance!
Chad
Nov 16 '05 #1
6 1118
Hmmmm

I tried it out and it seems to work fine :-

Boolean b1 = false;
Object* o = __box(b1);
Boolean b2 = *dynamic_cast<__box Boolean*>(o);
Console::WriteLine(b1);
Console::WriteLine(b2);

If you change the first line to b1 = true, even then it works fine.
What error are you seeing?

--
Regards,
Nish [VC++ MVP]

"Chad Silva" <fl**@online.alias.com> wrote in message
news:0a****************************@phx.gbl...

I'm getting a boolean property through reflection. That
part works fine. Since PropertyInfo->GetValue returns
Object*, to use it I need to cast it to a bool.

This seems like (and likely is) the simplest thing, but
i've been looking far and wide but can't seem to find the
answer. The docs seem to say that:

*dynamic_cast<__box Boolean*>(myBoolObject)

would do the trick. That works fine for my Int32
properties...but bombs on bools. Can anyone point me in
the right direction? (i'm relegated to using ToString and
comparing it to "True", and that's just cheezy :-)

Thanks in advance!
Chad


Nov 16 '05 #2
Hi Chad,

Nish is correct and, based on my experience, I did not find any known issue
that dynamic_case does not work for BOOL value.

For further research, would you please tell us the detailed error message
you received? Could you post some code snippet which is able to reproduce
the problem? I will be glad to check it on my side.

I look forward to hearing from you.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #3
Thanks Nishant and Tian for looking into this for me!

I did another test (one I didn't think about, but wish I
had). I wrote this snippet of code (included at the
bottom of the post, with error message below as well) that
illustrates the problem. Then I got a copy of VC++ 2003
(we're not able to use it in production yet so I wasn't
using it) and tried it there...and it worked. So i'm
guessing it's a problem with VC++ 2002. Is there a
way/workaround in 2002 that I can use without converting
it to a string and doing a string compare?

Thanks again!
Chad

#include "stdafx.h"

#using <mscorlib.dll>
#include <tchar.h>

using namespace System;
using namespace System::Collections;

int _tmain(void)
{
Stack * s = new Stack();
s->Push(__box(true));

Object * o = s->Pop();

try
{
Boolean b = *dynamic_cast<__box Boolean*>(o);
Console::WriteLine(b);
}
catch (Exception * e)
{
Console::WriteLine(e->ToString());
}

Console::WriteLine("Press any key to continue");

Console::Read();

return 0;
}

"System.NullReferenceException: Object reference not set
to an instance of an object.
at main() in c:\projects\vc2002
\boolcasttest\boolcasttest.cpp:line 22"

-----Original Message-----
Hi Chad,

Nish is correct and, based on my experience, I did not find any known issuethat dynamic_case does not work for BOOL value.

For further research, would you please tell us the detailed error messageyou received? Could you post some code snippet which is able to reproducethe problem? I will be glad to check it on my side.

I look forward to hearing from you.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
.

Nov 16 '05 #4
Hi Chad,

Thanks for your information. I am checking this issue and will update you
with my information.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #5
Hello Chad,

After further research, I found that this is a known issue in VC++ .NET
2002. When I delete "__box" inside dynamic_cast, there is no runtime
exception. I think that you can use this method to work around this problem.

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #6

That did the trick. Thanks again, Tian!

Chad
-----Original Message-----
Hello Chad,

After further research, I found that this is a known issue in VC++ .NET2002. When I delete "__box" inside dynamic_cast, there is no runtimeexception. I think that you can use this method to work around this problem.
Please feel free to let me know if you have any problems or concerns.
Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
.

Nov 16 '05 #7

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

Similar topics

4
by: David Rager | last post by:
Howdy, Put briefly, I have an array of chars, which I would like to access in pairs of bytes via casting the array to an array of shorts. I'm trying to be as elegant as possible. Below is a...
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
8
by: Razak | last post by:
Hi, I have a class which basically do Impersonation in my web application. From MS KB sample:- ++++++++++++++++++++code starts Dim impersonationContext As...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
1
by: Alex | last post by:
Hi I'm wondering if there is an easy way to analize log files in php? I'm sure there is a program somethere on the internet. If anyone can point me to one it would be great. I have a log file...
3
by: Ron | last post by:
Hi all, I've come across a problem when casting user controls in ASP.NET 2.0, hopefully someone can help. I have a master page with a user control on it (myMenu) that I wish to programmatically...
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
1
by: conchur | last post by:
Is there an elegant way to have a class utilise an inherited method to return a Typed Collection of itself? Example: class MyList<T> : List<T> where T : MyRoot { Add... Remove... }
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.