UDTs Within UNIONs

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Mark Newman
    Member
    • Dec 1999
    • 445

    UDTs Within UNIONs

    It seems that placing a UDT within a UNION isn't legal, as I'm
    getting error 476, "Block/scanned statement not allowed here"
    when I compile. For example:
    Code:
    UNION Angles
       TYPE DEGREES
          lDegrees     AS LONG
          sDescription AS ASCIIZ
          lExtraStuff  AS LONG
       END TYPE
       TYPE RADIANS
          lRads        AS LONG
          sDescription AS ASCIIZ
          lExtraStuff  AS LONG
          lMoreStuff   AS LONG
       END TYPE
       TYPE GRADS
          lGrads       AS LONG
          sDescription AS ASCIIZ
          lExtraStuff  AS LONG
          lOtherStuff  AS SINGLE
       END TYPE
    END UNION
    I need (rather, the DLL needs) to have the TYPE blocks as a
    whole overlap, not the individual elements. One solution would be
    to track the current Angle mode, updating the TYPEs as need be
    and then pass a pointer to that TYPE block to a function that
    expects the UNION parameter.

    Anyone have a better way I could get around this problem?

    Thanks!

    ------------------
    Mark Newman
    Mark Newman
  • Eric Pearson
    Member
    • Oct 1987
    • 10347

    #2
    You have to define the TYPEs, then use them in the UNION.

    Code:
    TYPE DEGREES
       lDegrees     AS LONG
       sDescription AS ASCIIZ * 10 '?
       lExtraStuff  AS LONG
    END TYPE
     
    TYPE RADIANS
       lRads        AS LONG
       sDescription AS ASCIIZ * 10 '?
       lExtraStuff  AS LONG
       lMoreStuff   AS LONG
    END TYPE
     
    TYPE GRADS
       lGrads       AS LONG
       sDescription AS ASCIIZ * 10 '?
       lExtraStuff  AS LONG
       lOtherStuff  AS SINGLE
    END TYPE
     
      
    UNION ANGLES
       MyDegrees AS DEGREES
       MyRadians AS RADIANS
       MuGrads   AS GRADS
    END UNION
     
    DIM Something AS ANGLES
     
    Something.MyDegrees.lDegrees = 90
    -- Eric


    ------------------
    Perfect Sync Development Tools
    Perfect Sync Web Site
    Contact Us: mailto:[email protected][email protected]</A>



    [This message has been edited by Eric Pearson (edited March 19, 2001).]
    "Not my circus, not my monkeys."

    Comment

    • Kev Peel
      Member
      • Sep 2000
      • 3127

      #3

      Mark, from the looks of it, you are coding it wrong, try this:

      Code:
       
      $Compile Exe
       
      Type MYTYPEA
           A As Long
      End Type
      
      Type MYTYPEB
           B As Long
      End Type
      
      Type MYTYPEC
           C As Long
      End Type
      
      Union MYUNION
           A As MYTYPEA
           B As MYTYPEB
           C As MYTYPEC
      End Union
       
       
      Function PbMain()
       Local tUnion As MYUNION
       
       tUnion.a.a = 100
        
       MsgBox Str$(tUnion.a.a)      ' Returns 100
       MsgBox Str$(tUnion.b.b)      ' Returns 100
       MsgBox Str$(tUnion.c.c)      ' Returns 100
       
      End Function
      That should work OK.

      Also, your code won't compile because the ASCIIZ length was not defined.

      Regards,





      ------------------
      Kev G Peel
      KGP Software, Bridgwater, UK.
      kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

      Comment

      • Mark Newman
        Member
        • Dec 1999
        • 445

        #4
        Brilliant! Defining the TYPEs ahead of the UNION fixed the problem.
        BTW, the TYPES were just a demo of the problem as the original source
        is copyrighted, so that's why the ASCIIZs weren't properly declared.
        Gee, can't put any anything past you guys!

        Many thanks to Eric & Kev!




        ------------------
        Mark Newman
        Mark Newman

        Comment

        Working...
        X