Reference Casting question.
-
Hello I am having real trouble with the following code.
System::Boolean vDataEvent16::IsSet::get()
{
System::Boolean data = (System::Boolean)(((* _Event) & 0x01) != false);
return data;
}I get an invalid cast exception. How would I overcome this? :omg: _Event is defined as a System::UInt16 ^ _Event and is passed in from C# assembly to the class constructor. I wanted to use unions with some structs to view bits in the struct as individual properties but I could not cast the System::UInt16 to an unsigned short or rather to an unmanaged value type either. Any suggesions Really appreciate the response. thanks -Peter
-
Hello I am having real trouble with the following code.
System::Boolean vDataEvent16::IsSet::get()
{
System::Boolean data = (System::Boolean)(((* _Event) & 0x01) != false);
return data;
}I get an invalid cast exception. How would I overcome this? :omg: _Event is defined as a System::UInt16 ^ _Event and is passed in from C# assembly to the class constructor. I wanted to use unions with some structs to view bits in the struct as individual properties but I could not cast the System::UInt16 to an unsigned short or rather to an unmanaged value type either. Any suggesions Really appreciate the response. thanks -Peter
-
Hello I am having real trouble with the following code.
System::Boolean vDataEvent16::IsSet::get()
{
System::Boolean data = (System::Boolean)(((* _Event) & 0x01) != false);
return data;
}I get an invalid cast exception. How would I overcome this? :omg: _Event is defined as a System::UInt16 ^ _Event and is passed in from C# assembly to the class constructor. I wanted to use unions with some structs to view bits in the struct as individual properties but I could not cast the System::UInt16 to an unsigned short or rather to an unmanaged value type either. Any suggesions Really appreciate the response. thanks -Peter
peterdrozd wrote:
!= false
You're comparing a UInt16 with false. That should be != 0
System::Boolean vDataEvent16::IsSet::get()
{
return (((*_Event) & 0x01) != 0);
}Mark Salsbery Microsoft MVP - Visual C++ :java:
-
peterdrozd wrote:
!= false
You're comparing a UInt16 with false. That should be != 0
System::Boolean vDataEvent16::IsSet::get()
{
return (((*_Event) & 0x01) != 0);
}Mark Salsbery Microsoft MVP - Visual C++ :java:
-
led mike wrote:
Did you reproduce the exception?
No sir (VS 2008 SP1). I don't know what's up with that - not enough info for me. :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
peterdrozd wrote:
!= false
You're comparing a UInt16 with false. That should be != 0
System::Boolean vDataEvent16::IsSet::get()
{
return (((*_Event) & 0x01) != 0);
}Mark Salsbery Microsoft MVP - Visual C++ :java:
I tried it and it does not work. It may be that I separated the class to a header file from the implemenation.
System::Boolean DataEvent16::IsActive::get()
{
System::Boolean data = (System::Boolean)(((* _Event) & 0x02) != 0); <--fails here.
return data;
}header file
property System::Boolean IsActive
{
System::Boolean get();
}this still fails. I am trying to get bits from an unsigned short and pass back to C# the value of the bits. I tried a union with a structure to define the bits and tried to set a variable that had all the bits as a System::UInt16 but it would not let me assign the * (System::Uint16 ^ ) to the unmanaged data type so I had to switch gears and try this method. doing a return when the class is implemented inline works fine however I can not use the class in another CPP file as a class variable because I could not make one class aware of the other with out a header. so I separated them. this led to problems with visual studio 2005 C++ CLI This should be a simple item but it's starting to take up so much time. :sigh: thanks -- Pete :)
modified on Thursday, September 11, 2008 11:34 AM
-
led mike wrote:
Did you reproduce the exception?
No sir (VS 2008 SP1). I don't know what's up with that - not enough info for me. :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I tried it and it does not work. It may be that I separated the class to a header file from the implemenation.
System::Boolean DataEvent16::IsActive::get()
{
System::Boolean data = (System::Boolean)(((* _Event) & 0x02) != 0); <--fails here.
return data;
}header file
property System::Boolean IsActive
{
System::Boolean get();
}this still fails. I am trying to get bits from an unsigned short and pass back to C# the value of the bits. I tried a union with a structure to define the bits and tried to set a variable that had all the bits as a System::UInt16 but it would not let me assign the * (System::Uint16 ^ ) to the unmanaged data type so I had to switch gears and try this method. doing a return when the class is implemented inline works fine however I can not use the class in another CPP file as a class variable because I could not make one class aware of the other with out a header. so I separated them. this led to problems with visual studio 2005 C++ CLI This should be a simple item but it's starting to take up so much time. :sigh: thanks -- Pete :)
modified on Thursday, September 11, 2008 11:34 AM
led mike and I can't reproduce the error. Here's how I tested:
UInt16 ^_Event = gcnew UInt16(4);
Boolean boolean = (((*_Event) & 0x01) != false);You don't need the (System::Boolean) cast. The != operator already returns a boolean. The only thing I can think of based on the code you've shown is _Event isn't really a UInt16 ^. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Mark Salsbery wrote:
I don't know what's up with that - not enough info for me.
Yeah I know, I plugged this in and ran it just fine
System::Boolean data = (System::Boolean)(((* _Event) & 0x01) != false);
led mike
I did this:
UInt16 ^ _Event = gcnew UInt16(4);
Boolean boolean = (((*_Event) & 0x01) != false);*shrug*
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
led mike and I can't reproduce the error. Here's how I tested:
UInt16 ^_Event = gcnew UInt16(4);
Boolean boolean = (((*_Event) & 0x01) != false);You don't need the (System::Boolean) cast. The != operator already returns a boolean. The only thing I can think of based on the code you've shown is _Event isn't really a UInt16 ^. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
here is part of the header file.
public ref class DataEvent16
{
private:
System::UInt16 ^ _Event;public: DataEvent16(System::UInt16 ^ Event); DataEvent16(); property System::UInt16 RawData { System::UInt16 get(); void set ( System::UInt16 value); } property System::UInt16 IsSet { System::UInt16 get(); } property System::Boolean IsActive { System::Boolean get(); }
};
Implementation file
DataEvent16::vDataEvent16(System::UInt16 ^ Event)
{
_Event = Event;
}
DataEvent16::vDataEvent16()
{
_Event = gcnew System::UInt16(0);
}
System::Boolean vDataEvent16::IsActive::get()
{
System::Boolean data = (System::Boolean)(((* _Event) & 0x02) != 0);
return data;
}this will compile but explode. on the boolean statement. I know it seems like overkill but I have tried several iterations. :)
-
I did this:
UInt16 ^ _Event = gcnew UInt16(4);
Boolean boolean = (((*_Event) & 0x01) != false);*shrug*
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I tried it and it does not work. It may be that I separated the class to a header file from the implemenation.
System::Boolean DataEvent16::IsActive::get()
{
System::Boolean data = (System::Boolean)(((* _Event) & 0x02) != 0); <--fails here.
return data;
}header file
property System::Boolean IsActive
{
System::Boolean get();
}this still fails. I am trying to get bits from an unsigned short and pass back to C# the value of the bits. I tried a union with a structure to define the bits and tried to set a variable that had all the bits as a System::UInt16 but it would not let me assign the * (System::Uint16 ^ ) to the unmanaged data type so I had to switch gears and try this method. doing a return when the class is implemented inline works fine however I can not use the class in another CPP file as a class variable because I could not make one class aware of the other with out a header. so I separated them. this led to problems with visual studio 2005 C++ CLI This should be a simple item but it's starting to take up so much time. :sigh: thanks -- Pete :)
modified on Thursday, September 11, 2008 11:34 AM
Are you sure the C# side is providing a System::Uint16 ^ and not just a System::Uint16? A System::Uint16 ^ (reference to a value type) is kind of unconventional...
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
here is part of the header file.
public ref class DataEvent16
{
private:
System::UInt16 ^ _Event;public: DataEvent16(System::UInt16 ^ Event); DataEvent16(); property System::UInt16 RawData { System::UInt16 get(); void set ( System::UInt16 value); } property System::UInt16 IsSet { System::UInt16 get(); } property System::Boolean IsActive { System::Boolean get(); }
};
Implementation file
DataEvent16::vDataEvent16(System::UInt16 ^ Event)
{
_Event = Event;
}
DataEvent16::vDataEvent16()
{
_Event = gcnew System::UInt16(0);
}
System::Boolean vDataEvent16::IsActive::get()
{
System::Boolean data = (System::Boolean)(((* _Event) & 0x02) != 0);
return data;
}this will compile but explode. on the boolean statement. I know it seems like overkill but I have tried several iterations. :)
I had to remove the 'v's (vDataEvent16, vDataEvent16) to get your code to compile, but it worked fine in VS 2008. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Correctamundo!
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
here is part of the header file.
public ref class DataEvent16
{
private:
System::UInt16 ^ _Event;public: DataEvent16(System::UInt16 ^ Event); DataEvent16(); property System::UInt16 RawData { System::UInt16 get(); void set ( System::UInt16 value); } property System::UInt16 IsSet { System::UInt16 get(); } property System::Boolean IsActive { System::Boolean get(); }
};
Implementation file
DataEvent16::vDataEvent16(System::UInt16 ^ Event)
{
_Event = Event;
}
DataEvent16::vDataEvent16()
{
_Event = gcnew System::UInt16(0);
}
System::Boolean vDataEvent16::IsActive::get()
{
System::Boolean data = (System::Boolean)(((* _Event) & 0x02) != 0);
return data;
}this will compile but explode. on the boolean statement. I know it seems like overkill but I have tried several iterations. :)
BTW thanks for providing code to test with :) What does the calling code look like?
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
BTW thanks for providing code to test with :) What does the calling code look like?
Mark Salsbery Microsoft MVP - Visual C++ :java:
fixed it. I had to remove all references for that data. The problem was that I have some arguments that have to be passed as references. I have others that have to be passed as value types. the _Event is a value type. I was passing it in as a reference so in that function, I was referencing the pointer I think. So it would compile but the value was probably greater than max value. I guess this all came about because I have become accustomed to coding inline classes and not removing the class definition from the implementation. there are not too many good examples online that I found on how to do it right. thanks everyone for your help. -Peter :cool:
-
fixed it. I had to remove all references for that data. The problem was that I have some arguments that have to be passed as references. I have others that have to be passed as value types. the _Event is a value type. I was passing it in as a reference so in that function, I was referencing the pointer I think. So it would compile but the value was probably greater than max value. I guess this all came about because I have become accustomed to coding inline classes and not removing the class definition from the implementation. there are not too many good examples online that I found on how to do it right. thanks everyone for your help. -Peter :cool:
That's what I figured - I just wasn't sure why the C# side didn't complain about a mismatched argument. :) Cheers, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: