Optional variables in a function

E

et

I have the following function that uses an optional parameter, and it sets a
default, as the program says I have to. Yet it doesn't pick up the default
value. Why is this, what am I doing wrong.

Public Function SelectQuery(strTbl as string, arr as ArrayList, Optional
SortCol as string = "Yr desc")

Calling procedure:

qry = SelectQuery(strTbl, arr, SortCol)
 
K

Ken Cox [Microsoft MVP]

You're passing SortCol in your calling procedure, so it wouldn't be
necessary (or desireable) for the optional portion to use the default value.

What happens with:

qry = SelectQuery(strTbl, arr)
 
K

Karl Seguin

Et,
Ken gave you the right answer, but consider an alternative to use optional
parameters: overloading. So you end up with two SelectQuery functions:

public funciton SelectQuery(strTable as string, arr as ArrayList) as XXXX
return SelectQuery(strTable, arr, "Yr desc")
end function

Public Function SelectQuery(strTbl as string, arr as ArrayList, SortCol as
string ) AS XXX
....
end function

notice that the first function only has 2 parameters and simply calls the
2nd function with a default parameter. Both methods will work (optional or
overloading), but I prefer overloading...there has been some low level
technical debates about which is best, but I simply prefer it for
readability and the fact that optional parameters don't work in C# and I
like to keep my code as cross-language friendly as possible.

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top