Help with Function Parameters (Array)

firas489

Member
Joined
Apr 25, 2008
Messages
14
Programming Experience
5-10
Hello all,

Im trying to build a function in VB.Net that one of its parameters is optional and to hold an array...
Ex:

Public Function test(Optional ByVal arVar As System.Array)

if i do this i'll get the following error:
Optional parameters must specify a default value.

Thats fine, but if i try adding a value to it, either
Optional ByVal arVar As System.Array="val1"
or
Optional ByVal arVar As System.Array={"val1","val2"}

I get this error:
Value of type 'String' cannot be converted to 'System.Array'.

I understand the error, but how to add a value to an array in a function parameters?


BTW, i tried using this too:
Public Function test(Optional ByVal arVar() As String = "val1")

And got the same error...

I hope i made sense in the thread

Thanks in advance,


Best Regards
 
Instead of using the Optional Keyword, you'll need to overload your function.

IE:
VB.NET:
Public Overloads Function test()
End Function

Public Overloads Function test(ByVal arVar() As string)
End Function
 
Thank you JuggaloBrotha for your quick reply,

but unfortunatly, when i use the Overloads, it will not make the variable in the parameter Optional.
The main thing is that i want the function to accept array type variable in its parameters while in the same time this array type variable is optional!.


Thank you,

Best Regards

Firas Assaad
 
That's exactly what Overloading does.

I made a new windows forms project and simply added this code, it works exactly as you want:
VB.NET:
    Private Overloads Sub Test()
        MessageBox.Show("This is called with NO array")
    End Sub

    Private Overloads Sub Test(ByVal AnArray() As String)
        MessageBox.Show("This is called WITH an array")
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim TheArray() As String = {"1", "2"}
        Call Test()
        Call Test(TheArray)
    End Sub
 
Aha,
Now i see what you mean...


I just thought about a quick method to follow to take advantage of the Overloads without the need to duplicate the code in two functions...

1. Have two functions that uses the Overload,
2. creat a subroutine that has all the real code that the function is suppose to run
3. the two functions will only be used to assign public variables with the parameters
4. and then they will execute the subroutine that uses the public variables that are assigned by the functions in the begining...


Does that sound logically correct?
or do you recommend a different approach?


Thank you for all your help

Best Regards

Firas Assaad
 
It depends on the situation here I guess. Usually I have a sub that takes all the parameters and actually does the work, then I create all the overloads subs that take various parameters (or none at all) and all of these call the one overload that actually does the work. If it's a function, the overloaded ones simply pass the value blindly through it from the one that does the actual work.

For example, there was a need to analyze a string looking for another string and I needed more control than what IndexOf provided at the time (this is an old code example by now):
VB.NET:
#Region " ContainsText "
    <System.Diagnostics.DebuggerStepThrough()> Friend Function ContainsText(ByVal Source As String, ByVal SearchText As String) As Boolean
        Return ContainsText(Source, SearchText, True)
    End Function

    <System.Diagnostics.DebuggerStepThrough()> Friend Function ContainsText(ByVal Source As String, ByVal SearchText As String, ByVal CaseSensitive As Boolean) As Boolean
        For Counter As Integer = 0 To Source.Length - SearchText.Length
            If CaseSensitive = True Then
                If Source.Substring(Counter, SearchText.Length) = SearchText Then Return True
            Else
                If Source.Substring(Counter, SearchText.Length).ToLower = SearchText.ToLower Then Return True
            End If
        Next Counter
        Return False
    End Function
#End Region

There are two functions here, one does the actual work and the other one simply excepts a param or two and passes it on to the one that does the work.
 
Thank you very much JuggaloBrotha,

Thats exactly what i was looking for,
and i just finished implementing it and testing it.

Results are greate


Again, thank you very much


Best Regards

Firas Assaad
 
Back
Top