• Capturing Arguments passed to a Procedure

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Capturing Arguments passed to a Procedure

    Author
    Topic
    #457915

    I am looking for a general snippet that can be used to capture in an array, the arguments which were passed to the current procedure or function. I visualize something akin to the following, but do not know of any way to determine properties of a procedure such as which arguments were passed to it.
    Any insight will be greatly appreciated.
    [codebox] For ctr = 1 To PassedArguments.Count
    myArray(ctr) = PassedArguments(ctr)
    Next
    [/codebox]

    Viewing 2 reply threads
    Author
    Replies
    • #1149612

      I don’t think you can do that unless you use ParamArray in the declaration of the procedure or function.

      • #1149621

        Thank you Hans
        I have come to the same conclusion. Passing the array as an argument achieves the desired results with no added agony.

        • #1149843

          Thank you Hans
          I have come to the same conclusion. Passing the array as an argument achieves the desired results with no added agony.

          If you were not using ParamArray, then you could simply declare the array and populate it as required given that you know at design time what the inputs are. I’m not really sure what purpose it would serve though?

    • #1149876

      What I sometimes do is use a custom type. This makes adding an argument simple:

      [codebox]Option Explicit

      Public Type People
      FirstName As String
      LastName As String
      Street As String
      End Type

      Sub Example()
      Dim tPerson As People
      With tPerson
      .FirstName = “Jan”
      .LastName = “Pieterse”
      .Street = “Churchlane”
      End With
      ShowPerson tPerson
      End Sub

      Sub ShowPerson(tPerson As People)
      With tPerson
      MsgBox .FirstName & ” ” & .LastName & vbNewLine & .Street
      End With
      End Sub
      [/codebox]

      • #1149882

        What I sometimes do is use a custom type.

        Me too.
        In particular, within a VBA project, or better yet, within a client’s project (which may consist of several VBA Application projects), if I can create a self-descriptive TYPE, I can do all sorts of useful debugging things.
        A self-descriptive TYPE being one where the first item is, say, a LONG that describes the number of dimensions of a parameter array, the next Val(lng) items are the size of each dimension, and then next item is the array.
        Or sometimes, paired items that tell me the type as well as the dimension.
        And so on.

        MARC library records were a popular example of this technique years ago.

        I wish that VBA made available to us poor mortals the descriptors of data, so we could interrogate them instead of relying on trial and On Error.

    • #1150029

      I am looking for a general snippet that can be used to capture in an array, the arguments which were passed to the current procedure or function.

      I’m not sure what you’re wanting to do here, but I’ve often used the Join and Split functions to “carry” multiple values in a single (global?) variable, which can be easily transformed to an array. See VB String Array Functions – Split, Join, Filter (VB6) for example.

      Alan

      • #1150279

        I’m not sure what you’re wanting to do here, but I’ve often used the Join and Split functions to “carry” multiple values in a single (global?) variable, which can be easily transformed to an array. See VB String Array Functions – Split, Join, Filter (VB6) for example.

        Alan

        Thanks for the input Alan
        As soon as I hit the send button on my original post I realized that if I wanted to create an array of arguments in the called procedure, the simplest way would be to create the array in the calling procedure and pass the array.

    Viewing 2 reply threads
    Reply To: Capturing Arguments passed to a Procedure

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: