473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

"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 5791
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

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

Similar topics

3
by: Paramesh | last post by:
Hello friends, My friend asked me this question: This question regards proprietary software (of which I am one of the developers), so I cannot post actual code for this question. I will try...
5
by: (Pete Cresswell) | last post by:
I've dabbled in "Optional" over the last few days and think I'm coming down against using it. Seems to me like it makes the code harder to read and more complicated. Instead of using Optional,...
18
by: Clark Nu | last post by:
It seems that when I define a fuction,I can set a default value to some of the peremeters.When I call the fuction without some of them,the fuction will use the default value automaticlly then...
2
by: Oenone | last post by:
In our applications, we use the special value of DateTime.MinValue to represent "null dates" throughout all our code. We recently ran into an issue where we wanted an optional date parameter for a...
2
by: Steve | last post by:
Kind of a strange question... I have a VB.NET 2.0 solution containing a main project (my EXE) and a number of other projects (class DLLs) that are "plug-ins" to the main app. These plugins get...
1
by: Sinex | last post by:
Hi, I have a webmethod. It takes an int parameter. I want this to get serialized as an XMLAttribute...so I used the XMLAttributeAttribute decoration and it works fine. Now, I want to specify...
3
by: Rincevent | last post by:
Hi, I'm trying to process a large number of files, each with a very small size (about 500 chars maximum). I need to retrieve multiple information from each file, and some of these are optional. So...
0
by: shai.halevi | last post by:
I'm looking for a simple template with the following property: I want to have in the resulting page something like this (say): <div> This is some text that never changes: ]] This is some more...
6
by: .rhavin grobert | last post by:
hello;-) i frequently need the following construction: ReturnParam § Function() § { /...do something.../ someType var § = something; /...do something.../ return something;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.