Announcement

Collapse
No announcement yet.

Function To Detect Datatype And Process Accordingly

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

  • Function To Detect Datatype And Process Accordingly

    '
    Code:
    'http://www.powerbasic.com/support/pbforums/showthread.php?t=24103
    ' FILE: PROCESSS_ANY_ARRAY.BAS
    ' AUTHOR: Michael Mattias Racine WI
    ' 7/18/02  WORKS GOOD!!
    ' 8/15/03 Tested that it gets UBOUND and LBOUND of passed array correctly.. it does.
    ' Compiler: Pb/Win 7.0
    #Compile Exe
    '
    '
    '===[Windows API Header Files]=========================================
    '  If you don't need all of the functionality supported by these APIs
    '  (and who does?), you can selectively turn off various modules by putting
    '  the following constants in your program BEFORE you #include "win32api.inc":
    '
    '  %NOGDI = 1     ' no GDI (Graphics Device Interface) functions
    '  %NOMMIDS = 1   ' no Multimedia ID definitions
    '
    %NOMMIDS  = 1
    #Include "WIN32API.INC"
    ' ====================================================
    ' FUNCTION TO DETECT DATATYPE AND PROCESS ACCORDINGLY
    ' ====================================================
    Function ProcessAnyArray (AD() As Word ) As Long
     '
        Local DT, nElements, I As Long
        Local s, OutString, Caption As String
    '
        DT= ArrayAttr( AD(), 1&)         ' Ignores type in function header; reads parameter
        nElements = ArrayAttr(AD(),4&)   ' *AS IT DARN WELL SHOULD!!*
        If DT = %VarClass_Lng Then
            Caption = "LONG array Detected"
            ReDim MyLong(nElements -1) As Long At VarPtr(AD(0))
            ' Build the output string
            For I = LBound(myLong,1) To UBound(MyLong, 1)
                OutString = OutString & Str$(MyLong(I)) & ","
            Next
        ElseIf DT = %VarClass_Sng Then
            Caption = "SINGLE array detected"
            ReDim MySingle(nelements -1) As Single At VarPtr(AD(0))
            For I = LBound(mySingle,1) To UBound(MySingle, 1)
                OutString = OutString & Str$(MySingle(I)) & ", "
            Next
        ElseIf DT = %VarClass_Str Then '<<< tried %VarClass_Fix but no good
            Caption = "String array detected"
            ReDim MyString(nelements -1) As String At VarPtr(AD(0))
            For I = LBound(myString,1) To UBound(MyString, 1)
                OutString = OutString & MyString(I) & ", "
            Next
     '
        Else
            Caption   = "Other array detected"
            OutString = "Unsupported datatype=" & Str$(DT)
        End If
        Local LB As Long, UB As Long
        LB = LBound(AD,1): UB = UBound(AD,1)
        s$ = Using$("Passed Array   LB=#, UB=#,", LB, UB)  & $CrLf & _
             OutString 
        MsgBox s, _
               %MB_ICONEXCLAMATION, _
               "ArrayAttr DEMO: " & Caption
    '
    End Function
    '
    Function PBMain () As Long
    '
        Local Stat As Long, I As Long
    '
        ReDim X(10) As Long
        For I = 0 To 5
            X(I) = I * 100
        Next
        ReDim Y(90) As Single
        For I = 0 To 10
            Y(I) = -.1 * I
        Next                
    '
        ReDim Strng(10) As String * 10
        For I = 0 To 10
            Strng(I) = "String"& Str$(i)
        Next                
        'Now try a string array
        Stat = ProcessAnyArray (ByVal VarPtr(Strng(0))) '<<< no good, not with StrPtr either
     '   
        ' process an array of LONGS:
        Stat = ProcessAnyArray (ByVal VarPtr (X()))
        ' now process an array of SINGLEs with the same function:
        Stat = ProcessAnyArray (ByVal VarPtr (Y()))
        ' now give the function something for which it is not prepared:
        Dim Z (4 To 999) As Cur
        Stat = ProcessAnyArray (ByVal VarPtr (Z()))
    '
    '    
    End Function
    ' ** END OF FILE **
    '
    Last edited by Gösta H. Lovgren-2; 22 Nov 2008, 09:25 PM.
    It's a pretty day. I hope you enjoy it.

    Gösta

    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

  • #2
    It detects a string srray but returns wild LB & UB. Just ran it (checking for the - previously noted in the other thread - attrib of 16) and got:
    ArrayAttr DEMO: String array detected
    Passed Array LB=840,984,430 UB=1,951,604,768
    You may note a subtle error there.

    ===========================================
    "It has become appallingly obvious
    our technology has exceeded our humanity."
    Albert Einstein (1879-1955)
    ===========================================



    Now it's my turn to say "Huh?"
    It's a pretty day. I hope you enjoy it.

    Gösta

    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

    Comment


    • #3
      >ReDim Strng(10) As String * 10

      A fixed-string array is not a VARCLASS_STR, it is a VARCLASS_FIX.

      While it should not be affecting LBOUND and UBOUND, this will definitely affect the data, as the element size of a STRING * 10 array is ten (10) and the element size of a STRING array is four (4).

      Access out of bounds a few times lots of values in your program turn funky.

      As I said... yiou can get around the compiler's type checking, but that means... the types are not checked and you are responsible for a lot more things yourself.

      Oh, wait a minute here....
      > Stat = ProcessAnyArray (ByVal VarPtr(Strng(0)))

      This should be passing VARPTR (Strng()), not VARPTR(Strng(some element number)) . Your 'ProcessAnyArray' function expects the address of the array descriptor, not the address of array data.

      Frankly I think you were pretty UN-lucky not to have gotten a protection fault. *UN*-lucky because when you get a protection fault you KNOW IMMEDIATELY you did something naughty and you don't waste any time checking output which simply must be garbage.

      MCM
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Okay I got it now (at least as far as this example goes). The answer to my original question (Can I pass an array portion of a UDT?) is still no (so far as I can see). It appears the function readily accepts dynamic string arrays, but not fixed length (at least not by anything I tried).

        Thanks anyway, M.

        '
        Code:
        'http://www.powerbasic.com/support/pbforums/showthread.php?t=24103
        ' FILE: PROCESSS_ANY_ARRAY.BAS
        ' AUTHOR: Michael Mattias Racine WI
        ' 7/18/02  WORKS GOOD!!
        ' 8/15/03 Tested that it gets UBOUND and LBOUND of passed array correctly.. it does.
        ' Compiler: Pb/Win 7.0
        #Compile Exe
        '
        '
        '===[Windows API Header Files]=========================================
        '  If you don't need all of the functionality supported by these APIs
        '  (and who does?), you can selectively turn off various modules by putting
        '  the following constants in your program BEFORE you #include "win32api.inc":
        '
        '  %NOGDI = 1     ' no GDI (Graphics Device Interface) functions
        '  %NOMMIDS = 1   ' no Multimedia ID definitions
        '
        %NOMMIDS  = 1
        #Include "WIN32API.INC"
        '
        Type Testing
          Test1 As String * 10
          test2 As Long
          test3(1 To 10) As String * 25
          test4 As Single
        End Type
        ' ====================================================
        ' FUNCTION TO DETECT DATATYPE AND PROCESS ACCORDINGLY
        ' ====================================================
        Function ProcessAnyArray (AD() As Word ) As Long
         '
            Local DT, nElements, I As Long
            Local s, OutString, Caption As String
        '
            DT= ArrayAttr( AD(), 1&)         ' Ignores type in function header; reads parameter
            nElements = ArrayAttr(AD(),4&)   ' *AS IT DARN WELL SHOULD!!*
            If DT = %VarClass_Lng Then
                Caption = "LONG array Detected"
                ReDim MyLong(nElements -1) As Long At VarPtr(AD(0))
                ' Build the output string
                For I = LBound(myLong,1) To UBound(MyLong, 1)
                    OutString = OutString & Str$(MyLong(I)) & ","
                Next
            ElseIf DT = %VarClass_Sng Then
                Caption = "SINGLE array detected"
                ReDim MySingle(nelements -1) As Single At VarPtr(AD(0))
                For I = LBound(mySingle,1) To UBound(MySingle, 1)
                    OutString = OutString & Str$(MySingle(I)) & ", "
                Next
            ElseIf DT = %VarClass_Fix Then '22 returned as unsupported earlier
                Caption = "Fixed Length String array detected"
                ReDim MyString(nelements -1) As String At VarPtr(AD(0))
                For I = LBound(myString,1) To UBound(MyString, 1)
                    OutString = OutString & MyString(I) & $CrLf 
                Next
            ElseIf DT = %VarClass_Str Then '23 returned as unsupported earlier
                Caption = "Dynamic String array detected"
                ReDim MyString(nelements -1) As String At VarPtr(AD(0))
                For I = LBound(myString,1) To UBound(MyString, 1)
                    OutString = OutString & MyString(I) & $CrLf 
                Next
         '
            Else
                Caption   = "Other array detected"
                OutString = "Unsupported datatype=" & Str$(DT)
            End If
            Local LB As Long, UB As Long
            LB = LBound(AD,1): UB = UBound(AD,1)
            s$ = Using$("Passed Array   LB=#, UB=#,", LB, UB)  & $CrLf & _
                 OutString 
            MsgBox s, _
                   %MB_ICONEXCLAMATION, _
                   "ArrayAttr DEMO: " & Caption  
            ClipBoard Set Text "ArrayAttr DEMO: " & Caption & $CrLf & s$ To lb      
        '
        End Function
        '
        Function PBMain () As Long
        '
            Local Stat As Long, I As Long
        '
            ReDim X(10) As Long
            For I = 0 To 5
                X(I) = I * 100
            Next
            ReDim Y(90) As Single
            For I = 0 To 10
                Y(I) = -.1 * I
            Next                
        '
        '      Already know these work
        ' '   
        '    ' process an array of LONGS:
        '    Stat = ProcessAnyArray (ByVal VarPtr (X()))
        '    ' now process an array of SINGLEs with the same function:
        '    Stat = ProcessAnyArray (ByVal VarPtr (Y()))
        '    ' now give the function something for which it is not prepared:
        '    Dim Z (4 To 999) As Cur
        '    Stat = ProcessAnyArray (ByVal VarPtr (Z()))
        '
            'Now try a dynamic string array 
            ReDim Strng(10) As String '* 10
            For I = 0 To 10
                Strng(I) = "String"& Str$(i)
            Next                
            Stat = ProcessAnyArray (ByVal VarPtr (Strng())) '<<< this works
            'Now try a fixed string array 
            ReDim Strng_Fixed(10) As String * 10
            For I = 0 To 10
                Strng_Fixed(I) = "String"& Str$(i)
            Next                
            Stat = ProcessAnyArray (ByVal VarPtr (Strng_Fixed())) '<<< GPF's
        'Now try arrat part of UDT 
        ' Testing   
        '  Test1 As String * 10
        '  test2 As Long
        '  test3(1 To 10) As String * 25
        '  test4 As Single
           Local UDT_Passing_Test As Testing    
              UDT_Passing_Test.Test1 = " Test1"
              UDT_Passing_Test.Test2 = 123
             For i = 1 To 10
              UDT_Passing_Test.Test3(i) = Using$("### Test3 ", Rnd(1, 99))
             Next i                                                      
              UDT_Passing_Test.Test4 = 789
        '    Stat = ProcessAnyArray (ByVal VarPtr (Passing_Test.Test3())) '<<< Error 526 - Period Not Allowed
        End Function
        ' ** END OF FILE **
        '
        It's a pretty day. I hope you enjoy it.

        Gösta

        JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
        LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

        Comment


        • #5
          "Arrays" within types do not have an array descriptor.. that is, they are not really arrays. (I call them tables).

          However, you should be able to create an array on top of the table ...
          Code:
          Type Testing
            Test1 As String * 10
            test2 As Long
            test3(1 To 10) As String * 25
            test4 As Single
          End Type
          
              LOCAL  T  AS Testing 
              Fill T.Test3() here   
              REDIM  Z(1 to 10)  AS STRING * 25 AT VARPTR (T.Test3(1)) 
          FIX_112508:
           '    CALL ProcessAnyArray (VARPTR(Z())  <<< error, deleted 
               CALL ProcessAnyArray (BYVAL VARPTR(Z())    ' correct
          .. and the "anyarray" function should work just fine.
          Last edited by Michael Mattias; 25 Nov 2008, 08:59 AM. Reason: Added required BYVAL at point of call
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Doesn't work here, M. Same 526 error. Your machine must be different than mine. (I'm presuming you tested it before posting. You did test before posting, didn't you? It isn't like you had to create the test code or anything. As we've all been reminded (remanded?), many times, "Post a compileable example.")
            Code:
               Local UDT_Passing_Test As Testing    
                  UDT_Passing_Test.Test1 = " Test1"
                  UDT_Passing_Test.Test2 = 123
                 For i = 1 To 10
                  UDT_Passing_Test.Test3(i) = Using$("### Test3 ", Rnd(1, 99))
                 Next i                                                      
                  UDT_Passing_Test.Test4 = 789
                ReDim  Z(1 To 10)  As String * 25 At VarPtr (T.Test3(1)) '<<< Error 526 - Period Not Allowed 
                Call ProcessAnyArray (VarPtr(Z())
            ==================================================
            "Blessed is the man,
            who having nothing to say,
            abstains from giving wordy evidence of the fact."
            George Eliot (1819-1880)
            ==================================================
            Last edited by Gösta H. Lovgren-2; 24 Nov 2008, 09:47 PM.
            It's a pretty day. I hope you enjoy it.

            Gösta

            JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
            LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

            Comment


            • #7
              Hi Gösta,

              There's a typo in your test code which causes the Error 526.

              I've put together a compilable test below to try to understand the issues here but it has me wondering since both versions of the call to ProcessAnyArray seem to work..

              Maybe just dumb luck? Will there be scope issues or something along the way?
              Code:
              #Dim All 
               
              Type Testing
                Test1 As String * 10
                test2 As Long
                test3(1 To 10) As String * 25
                test4 As Single
              End Type
               
              Function PbMain()
               Dim i&, pZ???
               Local UDT_Passing_Test As Testing
               
                   UDT_Passing_Test.Test1    = " Test1"
                   UDT_Passing_Test.Test2    = 123
                 For i = 1 To 10
                   UDT_Passing_Test.Test3(i) = Using$("### Test3 ", Rnd(1, 99))
                 Next i
                   UDT_Passing_Test.Test4    = 789
               
                ReDim Z(1 To 10) As String * 25 At Varptr(UDT_Passing_Test.Test3(1)) '<<<Error 526 - "T.Test3(1)" typo 
               
               'Call ProcessAnyArray (VarPtr(Z(1)))  ' NB "VarPtr(Z())" didn't work
               Call ProcessAnyArray (VarPtr(UDT_Passing_Test.Test3(1)))
               
              End Function
              '------------------/PBMain
               
              Function ProcessAnyArray (pZ As Dword) As Long 
               Dim i&, Msg$
               ReDim  Z(1 To 10)  As String * 25  At pZ
               
                For i = 1 To 10
                  Msg = Msg + Z(i) + $CR
                Next
                MsgBox Msg
               
              End Function 
              '------------------/ProcessAnyArray
              Last edited by Dave Biggs; 25 Nov 2008, 09:18 AM. Reason: Missed Type definition in original cut n paste
              Rgds, Dave

              Comment


              • #8
                When you pass the array descriptor address "VARPTR (Array())" you MUST use an inline "BYVAL" at the point of call.

                My error in example fixed....

                And no I don't test everything I post because I know it will work....assuming I don't do something silly like leave out the required BYVAL at the point of call.
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  Originally posted by Dave Biggs View Post
                  Hi Gösta,

                  There's a typo in your test code which causes the Error 526.
                  Nice catch, Dave. How in the heck that happened is beyond me. One ALWAYS tries to be careful when dealing with You_Know_Who. Too much excitement I guess.

                  I've put together a compilable test below to try to understand the issues here but it has me wondering since both versions of the call to ProcessAnyArray seem to work..
                  Works a charm (after adding the Type Testing at the top {grin}) and is just what I was looking for. Now I think I can replace the wordy sorting calls in the Profile with this much cleaner code.

                  Note the typo error you found didn't solve the problem though your code did quite nicely. Correctiung the typo just exposed another error.

                  Code:
                   
                        ReDim Z(1 To 10) As String * 25 At VarPtr(UDT_Passing_Test.Test3(1)) '<<<Error 526 - "T.Test3(1)" typo
                      Call ProcessAnyArray (VarPtr(Z()) '<<< Error 480 - Parameter mismatch
                  ==========================================
                  I have taken this step because
                  I want the discipline,
                  the fire and the authority of the Church.
                  I am hopelessly unworthy of it,
                  but I hope to become worthy.
                  Edith Sitwell
                  ==========================================

                  (Note M& I posted together and I didn't see his ByVal reference)
                  Last edited by Gösta H. Lovgren-2; 25 Nov 2008, 09:36 AM. Reason: CMYA
                  It's a pretty day. I hope you enjoy it.

                  Gösta

                  JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                  LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                  Comment


                  • #10
                    >Correctiung the typo just exposed another error.

                    OK, you need not rub it in. I screwed up the example by forgetting the BYVAL.

                    Stuff happens.
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                    • #11
                      OK what I get so far..
                      • You need to pass a Pointer to an Array Descriptor 'ByVal'.
                      • You can't pass a pointer ByVal to an array descriptor if the array is a member of a UDT (actually won't compile - gives Error 477)
                      • However you CAN pass a pointer (ByRef) to the first element in the array even if the array is a member of a UDT
                      • None of us is immune to typos
                      Rgds, Dave

                      Comment


                      • #12
                        You can't pass a pointer ByVal to an array descriptor if the array is a member of a UDT (actually won't compile - gives Error 477)
                        That's why you create a REAL array on top of the table using REDIM "AT" ..that creates a 'passable' array descriptor.....

                        ...and the use of the "BYVAL" keyword at the point of call tells the compiler, "forget what I typed about the parameter to that function... just pass this. I appreciate your concern but trust me, I know what I'm doing here."
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          That's why you create a REAL array on top of the table using REDIM "AT" ..that creates a 'passable' array descriptor.....
                          Is there an advantage in passing an array descriptor instead of just passing a pointer to the first array element?
                          Rgds, Dave

                          Comment


                          • #14
                            In some applications, ProcessAnyArray can make for MUCH cleaner (and less tedious) coding. Compare the two Macros below.

                            The far longer "Profile_Sort_Arrays" macro takes no (reasonably) measurable time to run while "Profile_Sort_Arrays_Cleaner_Code" takes up to a half second. That might matter to some.

                            Code:
                            '                
                            Function Profile_ProcessAnyArray_Procedures (pZ As Dword) As Long 
                             ReDim  Z(1 To %Profile_Array_Elements)  As String * %Profile_Procedure_Name_Length At pZ     
                               Array Sort z()
                            End Function 
                            '                
                            Function Profile_ProcessAnyArray_Variable (pZ As Dword) As Long 
                             ReDim  Z(1 To %Profile_Array_Elements)  As String * %Profile_Variable_Name_Length At pZ     
                               Array Sort z()
                            End Function 
                             
                            '
                            Macro Profile_Sort_Arrays_Cleaner_Code '?
                              tim1 = Timer - tim
                              tim = Timer
                            '
                              ReDim srt1(1 To %Profile_Array_Elements) As String * %Profile_Procedure_Name_Length  At VarPtr(pfi.Bytes(1)) 
                                     Profile_ProcessAnyArray_Procedures(VarPtr(pfi.Procedures(1)))
                                     Profile_ProcessAnyArray_Procedures(VarPtr(pfi.Macros(1)))
                            '
                              ReDim srt1(1 To %Profile_Array_Elements) As String * %Profile_Variable_Name_Length  At VarPtr(pfi.Bytes(1)) 
                                     Profile_ProcessAnyArray_Variable(VarPtr(pfi.Bytes(1)))
                                     Profile_ProcessAnyArray_Variable(VarPtr(pfi.Integers(1)))
                                     Profile_ProcessAnyArray_Variable(VarPtr(pfi.Longs(1)))
                                     Profile_ProcessAnyArray_Variable(VarPtr(pfi.Dwords(1)))
                                     Profile_ProcessAnyArray_Variable(VarPtr(pfi.Singles(1)))
                                     Profile_ProcessAnyArray_Variable(VarPtr(pfi.Doubles(1)))
                                     Profile_ProcessAnyArray_Variable(VarPtr(pfi.Quads(1)))
                                     Profile_ProcessAnyArray_Variable(VarPtr(pfi.Strings(1)))
                                     Profile_ProcessAnyArray_Variable(VarPtr(pfi.Pointers(1)))
                            End Macro
                            '
                            Macro Profile_Sort_Arrays
                              askey = Asc("~") 'use for sorts and formatting output
                            '
                              tim = Timer
                              ReDim srt(1 To %Profile_Array_Elements) As String 
                            ' 
                             If Prcd Then
                               For ctr = 1 To %Profile_Array_Elements 'put in to sort
                                  srt(ctr) = UCase$(pfi.Procedures(ctr))
                               Next ctr                    
                               Array Sort srt()
                               For ctr = 1 To %Profile_Array_Elements  'sorted, so put back
                                  If Asc(srt(ctr)) < askey Then 
                                     Profile_Duplicate_Check
                                     pfi.Procedures(ctr) = dup$ & MCase$(srt(ctr))
                                  End If   
                               Next ctr                 
                             End If             
                            ' 
                             If Mcro Then
                               For ctr = 1 To %Profile_Array_Elements 'put in to sort
                                  srt(ctr) = UCase$(pfi.Macros(ctr))
                               Next ctr                 
                               Array Sort srt()
                               For ctr = 1 To %Profile_Array_Elements  'sorted, so put back
                                  If Asc(srt(ctr)) < askey Then 
                                     Profile_Duplicate_Check
                                     pfi.Macros(ctr) = dup$ & MCase$(srt(ctr))
                                  End If   
                               Next ctr                 
                             End If             
                            '              
                             ReDim srt(1 To %Profile_Array_Elements) As String' * 50 'rest of stuff
                            '              
                             If Prs Then
                               For ctr = 1 To %Profile_Array_Elements 'put in to sort
                                  srt(ctr) = UCase$(pfi.Pointers(ctr))
                               Next ctr                 
                               Array Sort srt()
                               For ctr = 1 To %Profile_Array_Elements  'sorted, so put back
                                  If Asc(srt(ctr)) < askey Then 
                                     Profile_Duplicate_Check
                                     pfi.Pointers(ctr) = MCase$(srt(ctr))
                                  End If   
                               Next ctr                 
                             End If             
                            '              
                              ReDim srt(1 To %Profile_Array_Elements) As String' * 50 'rest of stuff
                            '
                             If Srng Then
                               For ctr = 1 To %Profile_Array_Elements 'put in to sort
                                  srt(ctr) = UCase$(pfi.Strings(ctr))
                               Next ctr                 
                               Array Sort srt()
                               For ctr = 1 To %Profile_Array_Elements  'sorted, so put back
                                  If Asc(srt(ctr)) < askey Then 
                                     Profile_Duplicate_Check
                                     pfi.Strings(ctr) = MCase$(srt(ctr))
                                  End If   
                               Next ctr                 
                             End If             
                            '              
                             If qad Then
                               For ctr = 1 To %Profile_Array_Elements 'put in to sort
                                  srt(ctr) = UCase$(pfi.Quads(ctr))
                               Next ctr                 
                               Array Sort srt()
                               For ctr = 1 To %Profile_Array_Elements  'sorted, so put back
                                  If Asc(srt(ctr)) < askey Then 
                                     Profile_Duplicate_Check
                                     pfi.Quads(ctr) = MCase$(srt(ctr))
                                  End If   
                               Next ctr                 
                             End If             
                            '              
                             If Dbl Then
                               For ctr = 1 To %Profile_Array_Elements 'put in to sort
                                  srt(ctr) = UCase$(pfi.Doubles(ctr))
                               Next ctr                 
                               Array Sort srt()
                               For ctr = 1 To %Profile_Array_Elements  'sorted, so put back
                                  If Asc(srt(ctr)) < askey Then 
                                     Profile_Duplicate_Check
                                     pfi.Doubles(ctr) = MCase$(srt(ctr))
                                  End If   
                               Next ctr                 
                             End If             
                            '              
                             If Sng Then
                               For ctr = 1 To %Profile_Array_Elements 'put in to sort
                                  srt(ctr) = UCase$(pfi.Singles(ctr))
                               Next ctr                 
                               Array Sort srt()
                               For ctr = 1 To %Profile_Array_Elements  'sorted, so put back
                                  If Asc(srt(ctr)) < askey Then 
                                     Profile_Duplicate_Check
                                     pfi.Singles(ctr) = MCase$(srt(ctr))
                                  End If   
                               Next ctr                 
                             End If             
                            '              
                             If Dwrd Then
                               For ctr = 1 To %Profile_Array_Elements 'put in to sort
                                  srt(ctr) = UCase$(pfi.Dwords(ctr))
                               Next ctr                 
                               Array Sort srt()
                               For ctr = 1 To %Profile_Array_Elements  'sorted, so put back
                                  If Asc(srt(ctr)) < askey Then 
                                     Profile_Duplicate_Check
                                     pfi.Dwords(ctr) = MCase$(srt(ctr))
                                  End If   
                               Next ctr                 
                             End If             
                            '              
                             If Lng Then
                               For ctr = 1 To %Profile_Array_Elements 'put in to sort
                                  srt(ctr) = UCase$(pfi.Longs(ctr))
                               Next ctr                 
                               Array Sort srt()
                               For ctr = 1 To %Profile_Array_Elements  'sorted, so put back
                                  If Asc(srt(ctr)) < askey Then 
                                     Profile_Duplicate_Check
                                     pfi.Longs(ctr) = MCase$(srt(ctr))
                                  End If   
                               Next ctr                 
                             End If             
                            '              
                             If Intg Then
                               For ctr = 1 To %Profile_Array_Elements 'put in to sort
                                  srt(ctr) = UCase$(pfi.Integers(ctr))
                               Next ctr                 
                               Array Sort srt()
                               For ctr = 1 To %Profile_Array_Elements  'sorted, so put back
                                  If Asc(srt(ctr)) < askey Then 
                                     Profile_Duplicate_Check
                                     pfi.Integers(ctr) = MCase$(srt(ctr))
                                  End If   
                               Next ctr                 
                             End If             
                            '              
                             If byt Then
                               For ctr = 1 To %Profile_Array_Elements 'put in to sort
                                  srt(ctr) = UCase$(pfi.Bytes(ctr))
                               Next ctr                 
                               Array Sort srt()
                               For ctr = 1 To %Profile_Array_Elements  'sorted, so put back
                                  If Asc(srt(ctr)) < askey Then 
                                     Profile_Duplicate_Check
                                     pfi.Bytes(ctr) = MCase$(srt(ctr))
                                  End If   
                               Next ctr                 
                             End If                          
                            ' 
                            End Macro
                            '
                            ===============================================
                            "I kissed my first girl
                            and smoked my first cigarette on the same day.
                            I haven't had time for tobacco since."

                            Arturo Toscanini
                            ===============================================
                            Last edited by Gösta H. Lovgren-2; 25 Nov 2008, 10:40 PM.
                            It's a pretty day. I hope you enjoy it.

                            Gösta

                            JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                            LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                            Comment


                            • #15
                              Could the time difference come from the fact that the one macro does a lot of IF tests and only does the variable type needed, whereas the other does no testing for types and therefore processes every type of variable?

                              Or could it be the number of procedure calls?

                              Just wondering...

                              -JohnM

                              Comment


                              • #16
                                Is there an advantage in passing an array descriptor instead of just passing a pointer to the first array element?
                                Sure: you can write one procedure to service any array type without the compiler's parameter-type checking - generally a good thing - getting in the way.

                                In a more generic sense, when you pass the descriptor you can REDIM the array in the called procedure (but I'm not sure that can be done with 'one procedure to handle any datatype').

                                Certainly I could find it convenient to, say, write one procedure to look thru arrays of fixed strings of any size for something, instead of putting the same code in one procedure for each size of fixed string to be supported.

                                So what do you need to do in your application?

                                MCM
                                Michael Mattias
                                Tal Systems (retired)
                                Port Washington WI USA
                                [email protected]
                                http://www.talsystems.com

                                Comment


                                • #17
                                  Originally posted by John Montenigro View Post
                                  Just wondering...

                                  -JohnM
                                  Want to be a little careful here, John. Wondering is a gateway to curiosity. And we all know what happened to the cat. {grin}

                                  ======================================================
                                  "Fill what's empty,
                                  empty what's full,
                                  and scratch where it itches."
                                  the Duchess of Windsor,
                                  when asked what is the secret of a long and happy life
                                  ======================================================
                                  It's a pretty day. I hope you enjoy it.

                                  Gösta

                                  JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                                  LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                                  Comment


                                  • #18
                                    ARRAYATTR seems to be a good thing for Arrays, (and along the sides I have been researching a bit of "Detecting the type after declared and not knowing the type")

                                    Aka: Pass an array, and have the function determine what the datatype was? (maybe even add some error handling if the type does not match the functions type (but falls within the definition) and does not cause a crash in "MOST" cases)

                                    Is there an API for the same train of thought on non-arrays or UDTs???

                                    I have been trying to come up with an idea myself but closest I find is whatever function I am in, the results are as I declared them, and not what were passed to them.
                                    Engineer's Motto: If it aint broke take it apart and fix it

                                    "If at 1st you don't succeed... call it version 1.0"

                                    "Half of Programming is coding"....."The other 90% is DEBUGGING"

                                    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                                    Comment


                                    • #19
                                      Originally posted by John Montenigro View Post
                                      Could the time difference come from the fact that the one macro does a lot of IF tests and only does the variable type needed, whereas the other does no testing for types and therefore processes every type of variable?

                                      Or could it be the number of procedure calls?
                                      I don't know why myself, John. The answer to that is far above my paygrade. I ust know it is. I ran it dozen(s) of times using both Macros (note the Tim1 - Tim at the top of one Macro) and it consistently ran 0 or one and .2 to .6 for the other).

                                      Just for fun, I checked the number lines in each Macro. "Cleaner_Code" uses about a dozen lines while the original needs over 150. Maybe/certainly could be done with less but a significant difference regardless. Clearly worth a half second of increased execution time.

                                      ====================================================
                                      People are what you make them.
                                      A scornful look turns into a complete fool
                                      a man of average intelligence.
                                      A contemptuous indifference turns into an enemy
                                      a woman who, well treated, might have been an angel.
                                      André Maurois
                                      ====================================================
                                      It's a pretty day. I hope you enjoy it.

                                      Gösta

                                      JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                                      LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                                      Comment


                                      • #20
                                        >Is there an API for the same train of thought on non-arrays or UDTs???

                                        UDTs and scalar data types are compile-time only entities; they do not exist at runtime. At runtime all that exists is an address.

                                        If you want to detect a variable's type at runtime, use the VARIANT data type with the VARIANTVT() function.

                                        MCM
                                        Michael Mattias
                                        Tal Systems (retired)
                                        Port Washington WI USA
                                        [email protected]
                                        http://www.talsystems.com

                                        Comment

                                        Working...
                                        X