Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Reference Casting question.

Reference Casting question.

Scheduled Pinned Locked Moved Managed C++/CLI
questioncsharp
17 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Mark Salsbery

    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:

    L Offline
    L Offline
    led mike
    wrote on last edited by
    #7

    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

    M 1 Reply Last reply
    0
    • P peterdrozd

      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

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #8

      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:

      P 1 Reply Last reply
      0
      • L led mike

        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

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #9

        I did this:

        UInt16 ^ _Event = gcnew UInt16(4);
        Boolean boolean = (((*_Event) & 0x01) != false);

        *shrug*

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        L 1 Reply Last reply
        0
        • M Mark Salsbery

          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:

          P Offline
          P Offline
          peterdrozd
          wrote on last edited by
          #10

          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. :)

          M 2 Replies Last reply
          0
          • P peterdrozd

            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

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #11

            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:

            1 Reply Last reply
            0
            • M Mark Salsbery

              I did this:

              UInt16 ^ _Event = gcnew UInt16(4);
              Boolean boolean = (((*_Event) & 0x01) != false);

              *shrug*

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #12

              Mark Salsbery wrote:

              I did this:

              and it didn't throw any exception right?

              led mike

              M 1 Reply Last reply
              0
              • P peterdrozd

                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. :)

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #13

                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:

                1 Reply Last reply
                0
                • L led mike

                  Mark Salsbery wrote:

                  I did this:

                  and it didn't throw any exception right?

                  led mike

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #14

                  Correctamundo!

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  1 Reply Last reply
                  0
                  • P peterdrozd

                    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. :)

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #15

                    BTW thanks for providing code to test with :) What does the calling code look like?

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    P 1 Reply Last reply
                    0
                    • M Mark Salsbery

                      BTW thanks for providing code to test with :) What does the calling code look like?

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      P Offline
                      P Offline
                      peterdrozd
                      wrote on last edited by
                      #16

                      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:

                      M 1 Reply Last reply
                      0
                      • P peterdrozd

                        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:

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #17

                        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:

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups