469,636 Members | 1,527 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

"Optional" Array Parameters

24
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?:

Expand|Select|Wrap|Line Numbers
  1. Sub ResolveArrays(ByVal array1() As String, ByVal valueX As String, _
  2.   Optional ByVal array2() As String = {""}, Optional ByVal retry As Boolean = False)
  3.     ' Code to take array1 and compare each value to valueX, 
  4.     ' if no value is found, either exit or
  5.     ' (optionally) compare the second array to valueX if the retry flag is set
  6. 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)
Jul 16 '07 #1
6 5434
Killer42
8,435 Expert 8TB
...
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?
Jul 17 '07 #2
Queez
24
Sorry, I think it was me being silly.
Oct 2 '07 #3
Killer42
8,435 Expert 8TB
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?
Oct 2 '07 #4
cugone
20
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?:

Expand|Select|Wrap|Line Numbers
  1. Sub ResolveArrays(ByVal array1() As String, ByVal valueX As String, _
  2.   Optional ByVal array2() As String = {""}, Optional ByVal retry As Boolean = False)
  3.     ' Code to take array1 and compare each value to valueX, 
  4.     ' if no value is found, either exit or
  5.     ' (optionally) compare the second array to valueX if the retry flag is set
  6. 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:

Expand|Select|Wrap|Line Numbers
  1.     ''' <summary>
  2.     ''' Compares array values to a given string.
  3.     ''' </summary>
  4.     ''' <param name="array1">Required. Array to compare against.</param>
  5.     ''' <param name="valueX">Required. String value to compare.</param>
  6.     ''' <param name="retry">Required. Whether or not to try the second, optional array.</param>
  7.     ''' <param name="array2">Optional. Array to compare against if value in Array1 is not found and retry is set to true.</param>
  8.     ''' <remarks>If array2.Length is greater than 10, only the first 10 elements will be evaluated.</remarks>
  9.     Public Sub ResolveArrays(ByVal array1() As String, ByVal valueX As String, ByVal retry As Boolean, ByVal ParamArray array2() As String)
  10.         Dim maxArray2Length As Integer = 10 'Insert maximum array2 length here
  11.         For Each line As String In array1
  12.             If line = valueX Then
  13.                 'Do something
  14.             End If
  15.         Next line
  16.         If retry = True Then
  17.             For i As Integer = 0 To maxArray2Length - 1
  18.                 If array2(i) = valueX Then
  19.                     'Do something
  20.                 End If
  21.             Next i
  22.         End If
  23.     End Sub
  24.  
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.
Oct 2 '07 #5
Queez
24
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.

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.
Oct 3 '07 #6
cugone
20
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.
Oct 3 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

18 posts views Thread by Clark Nu | last post: by
2 posts views Thread by Steve | last post: by
reply views Thread by shai.halevi | last post: by
6 posts views Thread by .rhavin grobert | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.