473,597 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Optional" Array Parameters

24 New Member
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 5807
Killer42
8,435 Recognized Expert Expert
...
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 New Member
Sorry, I think it was me being silly.
Oct 2 '07 #3
Killer42
8,435 Recognized Expert Expert
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 New Member
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 New Member
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 New Member
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
1578
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 to use illustrative examples. I have two libraries for distribution; one is an essential library and the other is an add-on (providing specialized extensions and
5
9495
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, I'm making the parm a Variant and passing Null when it's not used.... then checking for "If Len(theParmValue & "") > 0... to see if it is present. That way I can eliminate checks for IsMissing(theParmValue)...
18
2243
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 continue to work.But I'v fogot how to use,even I'v fogot whether I can use it in C# or not. Help me.
2
11904
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 procedure. We weren't able to declare it with DateTime.MinValue as its default value, as MinValue is a read-only property rather than a constant. To work around, we had to use a "magic date" that we checked for later on. I was never very happy...
2
2013
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 installed depending on each user's requirements. I'd like to implement a function in one plugin that only executes if another plugin is present. Typically, for one plugin to access a second plugin, I need to set a reference at design-time to the...
1
3010
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 that this attribute is optional (in the schema it should show up as Use = "Optional"). How do I do this. And how will my method function if the client doesnt pass the parameter?
3
3182
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 to "simplify" the process, I undefed $/ (variable contains the full file). The current regular expression that I know works (with all mandatory matches) is the following: /\{2:+(\d{6})\d{4}.\}.*^:25:(.+?$).*^:13D:(\d{6})/sm as can be seen,...
0
1119
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 text that never changes </div> The crux is that if the calling code never initializes the variable,
6
2394
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
7977
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8276
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8261
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
3893
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3936
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2409
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1496
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.