4 Replies Latest reply on Apr 5, 2018 4:51 PM by Rob Edwards

    Use already declared variables in function

    Harold Black

      I have a function which I would like to reuse the variables I have already declared in the main macro. How do I go about accomplishing this? I've tried looking but got confused in the help file. I don't see the point of declaring new variables in the function for the values that are sent to the function call.

        • Re: Use already declared variables in function
          Jacob Corder

          i would avoid this at all costs.  because certain variables are Reference types and others are Value Types.

           

          any reference type that is passed as an argument will be modified globally as a reference type is actually just a memory pointer to an object in memory.

           

          if it is a double, string, integer then go ahead but if it isnt then dont do it. 

           

          the way to do it is simply

           

          (If it is an object type)

          Set SomeVariable = Something

          or Variants

          SomeVariable = Something.

          • Re: Use already declared variables in function
            Harold Black

            My understanding of calling functions is minimal. What I would like to do is pass some variables which contain numbers, a variant containing an array of numbers, and a variant containing a collection of sketch segments.

             

            In the main code:

             

            Dim TanY                            As Double

            Dim SplinePoints(15)         As Variant

            Dim swPileSeg(10)             As Variant

            Dim i                                   As Integer

            Dim spacing                       As Double

            Dim xStart                          As Double

             

            My call would be this

            GetTangent(TanY, SplinePoints,swPileSeg,i,spacing,xStart) As Boolean

             

            My function would be this

            Function GetTangent(TanY, ByRef SplinePoints As Object, ByRef swPileSeg As Object, i, spacing, xStart) As Boolean

             

             

            End Function

             

            I'm getting a compile error:

            Statement invalid outside Type block when I call GetTangent.

              • Re: Use already declared variables in function
                Josh Brady

                Harold Black wrote:

                 

                My understanding of calling functions is minimal. What I would like to do is pass some variables which contain numbers, a variant containing an array of numbers, and a variant containing a collection of sketch segments.

                 

                In the main code:

                 

                Dim TanY As Double

                Dim SplinePoints(15) As Variant

                Dim swPileSeg(10) As Variant

                Dim i As Integer

                Dim spacing As Double

                Dim xStart As Double

                 

                My call would be this

                TrueOrFalse = GetTangent(TanY, SplinePoints,swPileSeg,i,spacing,xStart) As Boolean <-Get rid of this part

                ^-This variable has to be a Boolean because below you say that the function returns a Boolean.

                My function would be this

                Function GetTangent(TanY, ByRef SplinePoints As Object, ByRef swPileSeg As Object, i, spacing, xStart) As Boolean

                 

                 

                End Function

                 

                I'm getting a compile error:

                Statement invalid outside Type block when I call GetTangent.

              • Re: Use already declared variables in function
                Rob Edwards

                Hi Harold

                Josh has pointed out the error in your code, but regarding your main question it is easier than you think.

                You don't have to pass any arguments because the global variables are in scope..