By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,967 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,967 IT Pros & Developers. It's quick & easy.

Optional variables in a function

P: n/a
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)
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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)

"et" <ea*************@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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)


Nov 19 '05 #2

P: n/a
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
--
MY ASP.Net tutorials
http://www.openmymind.net/
"et" <ea*************@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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)

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.