"Optional" Array Parameters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Queez
    New Member
    • Jul 2007
    • 24

    "Optional" Array Parameters

    I've had a good look around and no-one seems to have mentioned this, which leads me to believe that I may be missing something simple.

    Basically, is there a way I can do the following, and if so, how?:

    Code:
    Sub ResolveArrays(ByVal array1() As String, ByVal valueX As String, _
      Optional ByVal array2() As String = {""}, Optional ByVal retry As Boolean = False)
        [I]' Code to take array1 and compare each value to valueX, [/I]
       [I] ' if no value is found, either exit or[/I]
       [I] ' (optionally) compare the second array to valueX if the retry flag is set[/I]
    Exit Sub
    At the moment, VS2005 is throwing an error which is not allowing me to add the optional array2 to the parameters.

    Of course, there are other ways I can do this, but it's bugging me now - are optional arrays not allowed?

    (For an example of where the above could be used: comparing a specific value to a list of words then, if not found, comparing it to a list of common mistakes)
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by Queez
    ...
    At the moment, VS2005 is throwing an error which is not allowing me to add the optional array2 to the parameters.
    What's the error?

    Comment

    • Queez
      New Member
      • Jul 2007
      • 24

      #3
      Sorry, I think it was me being silly.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by Queez
        Sorry, I think it was me being silly.
        Not to mention very s...l...o...w.. . :) Two and a half months to respond?

        Anyway, so you've got it sorted, huh?

        Comment

        • cugone
          New Member
          • Sep 2007
          • 20

          #5
          Originally posted by Queez
          I've had a good look around and no-one seems to have mentioned this, which leads me to believe that I may be missing something simple.

          Basically, is there a way I can do the following, and if so, how?:

          Code:
          Sub ResolveArrays(ByVal array1() As String, ByVal valueX As String, _
            Optional ByVal array2() As String = {""}, Optional ByVal retry As Boolean = False)
              [I]' Code to take array1 and compare each value to valueX, [/I]
             [I] ' if no value is found, either exit or[/I]
             [I] ' (optionally) compare the second array to valueX if the retry flag is set[/I]
          Exit Sub
          At the moment, VS2005 is throwing an error which is not allowing me to add the optional array2 to the parameters.

          Of course, there are other ways I can do this, but it's bugging me now - are optional arrays not allowed?

          (For an example of where the above could be used: comparing a specific value to a list of words then, if not found, comparing it to a list of common mistakes)
          Here's what you need:

          Code:
              ''' <summary>
              ''' Compares array values to a given string.
              ''' </summary>
              ''' <param name="array1">Required. Array to compare against.</param>
              ''' <param name="valueX">Required. String value to compare.</param>
              ''' <param name="retry">Required. Whether or not to try the second, optional array.</param>
              ''' <param name="array2">Optional. Array to compare against if value in Array1 is not found and retry is set to true.</param>
              ''' <remarks>If array2.Length is greater than 10, only the first 10 elements will be evaluated.</remarks>
              Public Sub ResolveArrays(ByVal array1() As String, ByVal valueX As String, ByVal retry As Boolean, ByVal ParamArray array2() As String)
                  Dim maxArray2Length As Integer = 10 'Insert maximum array2 length here
                  For Each line As String In array1
                      If line = valueX Then
                          'Do something
                      End If
                  Next line
                  If retry = True Then
                      For i As Integer = 0 To maxArray2Length - 1
                          If array2(i) = valueX Then
                              'Do something
                          End If
                      Next i
                  End If
              End Sub
          The summary, param, and remarks section is for the Intellisense.

          All Optional parameters must 1) be listed last 2) contain a default value 3) must be before all required parameters 4) not be used when ParamArray is used.

          ParamArray is an optional array of elements of the specified type.
          Paramarray: 1) Must be declared ByVal 2) Must only be declared once 3) Must be the LAST parameter. 4)

          Because ParamArray can be infinitely large, it's a good idea to limit how many elements it can have. I just inserted 10 as an arbitrary number, you can change it if need be.

          Comment

          • Queez
            New Member
            • Jul 2007
            • 24

            #6
            Originally posted by Killer42
            Not to mention very s...l...o...w.. . :) Two and a half months to respond?

            Anyway, so you've got it sorted, huh?
            Hehe, sorry for my slow response. I actually never figured it out and had to use an alternative method. The alternative method involved re-thinking what I was doing, but it was actually much neater in the end.

            Originally posted by cugone
            All Optional parameters must 1) be listed last 2) contain a default value 3) must be before all required parameters 4) not be used when ParamArray is used.

            ParamArray is an optional array of elements of the specified type.
            Paramarray: 1) Must be declared ByVal 2) Must only be declared once 3) Must be the LAST parameter. 4).
            Thanks Cugone, I wasn't aware of the ParamArray special command.

            Comment

            • cugone
              New Member
              • Sep 2007
              • 20

              #7
              Originally posted by Queez
              Hehe, sorry for my slow response. I actually never figured it out and had to use an alternative method. The alternative method involved re-thinking what I was doing, but it was actually much neater in the end.

              Thanks Cugone, I wasn't aware of the ParamArray special command.
              You're welcome, though I need to mention I made a little typo in the description. Optional parameters must be listed /after/ all required parameters, not before. And still, can not be used if you use ParamArray and vice versa.

              Comment

              Working...