Announcement

Collapse
No announcement yet.

reset an array in a structure

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • reset an array in a structure

    Hello!

    I would like to know if I'm at fault here or PBDLLv6 doesn't support this feature.

    type MYTYPE
    wigits(32) as long
    end type

    dim temp as MYTYPE

    reset temp.wigits() 'This line give me a sytax error

    Cheers!

  • #2
    Hello,

    This works and will do the same thing. It shouldn’t take to much longer than having PB do it. If you needed something faster you could use pointers and overwrite a string of NULL’s to the location of the array.

    I have no idea why you can’t simple say: RESET temp.wigits()

    Code:
    TYPE MYTYPE
        wigits(32) AS LONG
    END TYPE
    FUNCTION PBMAIN
        LOCAL temp AS MYTYPE
        REGISTER count AS LONG
        FOR count = 0 TO 32
            RESET temp.wigits(count)
        NEXT count
    END FUNCTION
    Colin Schmidt

    ------------------
    Colin Schmidt & James Duffy, Praxis Enterprises, Canada

    Comment


    • #3
      An array within a UDT is not the same as a conventional array, so you cannot manipulate them in the same way.

      An array in a UDT should be thought of as a 'table' - essentially it just defines a block of memory within a UDT to simplify access to smaller sections of that UDT member.

      If you are regularly clearing a UDT array in your code, then you may be better off defining a UNION for that section - of which the UNION contains the array and a fixed-length string of the same length. That way it is a simple task of setting the string data to erase the numeric array.
      Code:
      UNION iArrayType
        Wigits(32) AS LONG
        sWigits AS STRING * 128
      END UNION
       
      TYPE MyTypeType
        a AS LONG
        b AS iArrayType
        c AS LONG
      END TYPE
       
      DIM MyType as MyTypeType
      ' Now clear the array:
      MyType.b.sWigits = STRING$(0, 128)
      It's amazing how often a UNION can make life easier - they are often overlooked by programmers and offer all sorts of ways to "recast" data in different ways!


      ------------------
      Lance
      PowerBASIC Support
      mailto:[email protected][email protected]</A>
      Lance
      mailto:[email protected]

      Comment


      • #4
        The Union is a much better idea… Like you said, I completely overlooked them. I’ll definitely add them to my arsenal They’ll be very usefull in databases.

        Thanks,
        Colin Schmidt

        ------------------
        Colin Schmidt & James Duffy, Praxis Enterprises, Canada

        Comment


        • #5
          Thank you for your answers, they both seem to work very well!

          Cheers!

          ------------------

          Comment


          • #6
            Colin,

            I could not get your code with RESET to work. In fact, contrary to the PB/DLL
            users' manual (p. 313), I couldn't get RESET to work on a user-defined type
            at all. I had to use temp.wigits(count) = 0 for your code to work.

            I also implemented Lance's code, but found that his STRING$ parameters were
            reversed. The implementation should be as follows:

            Code:
               #COMPILE EXE
               UNION iArrayType
                  Wigits(32) AS LONG
                  sWigits AS STRING * 128
               END UNION
            
               TYPE MyType
                  a AS LONG
                  b AS iArrayType
                  c AS LONG
               END TYPE
            
               FUNCTION PBMAIN
                  LOCAL temp AS MYTYPE
                  MSGBOX STR$(temp.b.Wigits(5))
                  temp.b.wigits(5) = 25
                  MSGBOX STR$(temp.b.wigits(5))
                  'now reset wigits() array
                  temp.b.sWigits = STRING$(128, 0)
                  MSGBOX STR$(temp.b.wigits(5))
               END FUNCTION
            ------------------

            Comment


            • #7
              charles --
              1) if to take mark's initial question, best solution is lance' method, posted (p.7) in http://www.powerbasic.com/support/pb...hread.php?t=43
              <font face="courier new, courier" size="3"><pre>
              #compile exe
              type mytype
              wigits(32) as long
              end type
              function pbmain
              local temp as mytype
              temp.wigits(5) = 25 ' test
              lset temp = string$(len(temp),0) ' nothing more !!!
              msgbox str$(temp.wigits(5)) ' test
              end function
              [/CODE]
              2) why 128 (instead of 132) ?
              wdigits(32) = wdigits (0 : 32)


              [this message has been edited by semen matusovski (edited march 18, 2000).]

              Comment


              • #8

                Semen.. you have sharp eyes.. 33 x 4 = 132

                I prefer this
                Code:
                    #Compile Exe   
                    Type MYTYPE      
                        wigits(32) As Long   
                    End Type   
                    Function PbMain      
                        Local temp      As MYTYPE
                        Local tempEmpty As MYTYPE
                
                        temp.wigits(5) = 25 ' Test
                        temp = tempEmpty ' NOTHING MORE !!!
                        MsgBox Str$(temp.wigits(5)) ' Test
                
                    End Function


                ------------------
                Jim..
                [email protected]
                Jim..

                Comment


                • #9
                  Well done, Semen.
                  I appreciate your post... I learned something.

                  And Jim, I like your code too. And although you're creating two objects,
                  I don't think it uses any more space than Semen's, since his uses string
                  space instead.

                  ------------------

                  Comment


                  • #10
                    Charles – your right! I didn’t check the value after the reset, I just assumed that it did what it said it would…

                    Code:
                    #COMPILE EXE
                    TYPE MYTYPE
                        wigits AS LONG
                    END TYPE
                    FUNCTION PBMAIN
                        LOCAL temp AS MYTYPE
                        LOCAL test AS LONG
                        LOCAL mystr AS STRING
                        temp.wigits = 1
                        test = 1
                        mystr = "Hello"
                        RESET temp.wigits
                        RESET temp
                        RESET test
                        RESET mystr
                        MSGBOX STR$(temp.wigits)
                        MSGBOX STR$(test)
                        MSGBOX mystr
                    END FUNCTION
                    Both the temp and test in this example are not reset…. The mystr does though…

                    I read all six sentences in the help file on RESET and could not make sense of this. I’m assuming the help file is just wrong and that PB assumes that you will reset any numeric value by using the ever complicated “= 0” statement instead.

                    Lance, Dave?

                    Colin Schmidt

                    ------------------
                    Colin Schmidt & James Duffy, Praxis Enterprises, Canada

                    Comment


                    • #11
                      Has noone read the recent Errata posts in the FAQ forum? There is errata available on the RESET statement as described in the doc's.

                      BTW, I wrote the code in my earlier message off-the-cuff and did not test it - I guess it's a case of errors and omissions excepted! Sorry folks! Lucky we have people here that can spot even the smallest of errors...!

                      (I inadvertantly reversed the parameters to STRING$() and dropped the UNION's string member by one) :sigh:


                      ------------------
                      Lance
                      PowerBASIC Support
                      mailto:[email protected][email protected]</A>
                      Lance
                      mailto:[email protected]

                      Comment


                      • #12
                        Lance --

                        > Has noone read the recent Errata posts in the FAQ forum?

                        Semen did... He even posted a link to the appropriate page about RESET in his message (above).

                        -- Eric

                        ------------------
                        Perfect Sync: Perfect Sync Development Tools
                        Email: mailto:[email protected][email protected]</A>



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

                        Comment

                        Working...
                        X