Announcement

Collapse
No announcement yet.

Pass an array to a method

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

  • Pass an array to a method

    Has anyone been successful in passing an array to a method in a class? I am receiving the error “Error 422, scalar variable expected”. I also tried passing a pointer to the array, but this resulted in a crash. Any ideas would be appreciated. If I find a solution (which is probably something simple), I will post it.
    RTM

  • #2
    Here is a simple example of the problem:

    Code:
    #COMPILE EXE
    #DIM ALL
    
    CLASS cTest
        INTERFACE iTest
            INHERIT IUNKNOWN
            
            METHOD readArray(BYVAL p AS STRING POINTER)
                MSGBOX @p[0]
            END METHOD
            
        END INTERFACE
    
    END CLASS
    
    FUNCTION PBMAIN () AS LONG
        DIM i AS DWORD
        DIM p AS STRING POINTER
        DIM a(2) AS STRING
        
        a(0) = "Test 1"
        a(1) = "Test 2"
        a(2) = "Test 3"
        
        p = VARPTR(a(0))
        
        DIM c AS iTest
        c = CLASS "cTest"
        
        c.readArray(p)
        
    END FUNCTION
    RTM

    Comment


    • #3
      You can pass the array as an Variant

      LET vrntvar = array()
      Last edited by Michael Mayerhoffer; 30 Aug 2008, 11:13 PM.
      A dozen what.

      Comment


      • #4
        Thanks Michael. I figured that variants would be the solution.

        Code:
        #COMPILE EXE
        #DIM ALL
        
        CLASS cTest
            INTERFACE iTest
                INHERIT IUNKNOWN
        
                METHOD readArray(v AS VARIANT)
                    DIM a(2) AS STRING
                    LET a() = v
                    MSGBOX a(0)
                END METHOD
        
            END INTERFACE
        END CLASS
        
        FUNCTION PBMAIN () AS LONG
            DIM i AS DWORD
            DIM a(2) AS STRING
            DIM v AS VARIANT
        
            a(0) = "Test 1"
            a(1) = "Test 2"
            a(2) = "Test 3"
            
            LET v = a()
        
            DIM c AS iTest
            c = CLASS "cTest"
        
            c.readArray(v)
        END FUNCTION
        RTM

        Comment


        • #5
          REDIM AT is your friend.

          Code:
          #COMPILE EXE
          #DIM ALL
          
          CLASS cTest
              INTERFACE iTest
                  INHERIT IUNKNOWN
                  
                  METHOD readArray(BYVAL p AS DWORD, BYVAL n AS DWORD)
                      REDIM x(n-1) AS STRING AT p
                      MSGBOX x(0)
                  END METHOD
                  
              END INTERFACE
          
          END CLASS
          
          FUNCTION PBMAIN () AS LONG
              DIM i AS DWORD
              DIM p AS STRING POINTER
              DIM a(2) AS STRING
              
              a(0) = "Test 1"
              a(1) = "Test 2"
              a(2) = "Test 3"
              
              p = VARPTR(a(0))
              
              DIM c AS iTest
              c = CLASS "cTest"
              
              c.readArray(p, 3)
              
          END FUNCTION
          Forum: http://www.jose.it-berater.org/smfforum/index.php

          Comment


          • #6
            Excellent solution. Thanks Jose.
            RTM

            Comment

            Working...
            X