Easy casting question (hopefully)

  • Thread starter Thread starter Chad Silva
  • Start date Start date
C

Chad Silva

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
 
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?
 
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.
 
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"
 
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.
 
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.
 
Back
Top