473,566 Members | 2,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function with optional parameter

JT
how can i declare a function that will accept an optional parameter?

something like:

function newFunc(strValu e1, strValue2) --where strValue2 is optional.

thanks much.
Jul 19 '05 #1
7 36565
JT wrote:
how can i declare a function that will accept an optional parameter?

something like:

function newFunc(strValu e1, strValue2) --where strValue2 is optional.

thanks much.


http://www.4guysfromrolla.com/webtech/071801-1.shtml

HTH,
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 19 '05 #2
What language? VBScript or JScript? Optional arguments are not supported
in VB Script, unfortunately. Here's a work around that someone has come up
with. http://www.4guysfromrolla.com/webtech/071801-1.shtml

I'm ~pretty~ sure that jscript does not support optional arguments either,
but hopefully someone can verify that, as I'm a jscript novice at best.

Ray at work

"JT" <je******@sppin c.net> wrote in message
news:OL******** ******@TK2MSFTN GP11.phx.gbl...
how can i declare a function that will accept an optional parameter?

something like:

function newFunc(strValu e1, strValue2) --where strValue2 is optional.

thanks much.

Jul 19 '05 #3
Optional parameters aren't supported in VBScript. What we do in our code is
this:

Call MySub("sherlock |holmes|pipe|ha t|coat")

Sub MySub(parms)
const cFirstname = 0
const cLastname = 1
const cAccessory = 2
const cClothing1 = 3
const cClothing2 = 4
parms = split(parms, "|")
redim preserve parms(cClothing 2)
'--- set up defaults (if any)
if parms(cFirstnam e) = "" then parms(cFirstnam e) = "John"
if parms(cLastname ) = "" then parms(cLastname ) = "Watson"
'--- other processing with the values
End Sub

If you're looking strictly at true/false values you can do it this way:

Call MySub(12)

Sub MySub(parms)
const tfparm1 = 2
const tfparm2 = 4
const tfparm3 = 8
if (parms and tfParm1) = tfParm1 then
'--- some processing here
end if
if (parms and tfParm2) = tfParm2 then
'--- some processing here
end if
if (parms and tfParm3) = tfParm3 then
'--- some processing here
end if
End Sub

"JT" <je******@sppin c.net> wrote in message
news:OL******** ******@TK2MSFTN GP11.phx.gbl...
how can i declare a function that will accept an optional parameter?

something like:

function newFunc(strValu e1, strValue2) --where strValue2 is optional.

thanks much.

Jul 19 '05 #4


Ray, from one novice to another :)

I think you can use optional arguments with Jscript. but you can't
skip a parameter... eg if you leave off arg3 below you also have to
leave off 4 & 5.

if there is a way of just skipping arg3 below then I dont know it and
maybe a guru will help :)

Also I've learned you can pass an object to a function (myfunc2 below)
which might help

HTH
Al.

function Myfunc (arg1, arg2, arg3, arg4, arg5) {

// the below will asign the value after the || if the arg1
// is "undefined/null"
var a1 = arg1 || ""; // undefined...set to ""
var a2 = arg2 || 0; // undefined...set to 0
var a3 = arg3 || 0; // undefined...set to 0
var a4 = arg4 || ""; // undefined...set to ""
var a5 = arg5 || 0; // undefined...set to 0

// or you can do this:

if (typeof arg1 == "undefined" ) {
arg1 = "";
}

//etc
}
// calling a func to pass an object.
// note the curly brackets INSIDE the rounded ones.
var RetVal = myFunc2 ( {x :10, y:20, z : 30, text:"myText: "} );

// function "requires" x & y properties... z & text is optional
function myFunc2 (obj) {
var calc = obj.x * obj.y;

if (typeof obj.z == "number") {
calc *= obj.z;
}
if (typeof obj.text == "string") {
return obj.text + calc
}
return calc;
}

On Wed, 14 Jan 2004 15:35:38 -0500, "Ray at <%=sLocation% >"
<myfirstname at lane34 dot com> wrote:
What language? VBScript or JScript? Optional arguments are not supported
in VB Script, unfortunately. Here's a work around that someone has come up
with. http://www.4guysfromrolla.com/webtech/071801-1.shtml

I'm ~pretty~ sure that jscript does not support optional arguments either,
but hopefully someone can verify that, as I'm a jscript novice at best.

Ray at work

"JT" <je******@sppin c.net> wrote in message
news:OL******* *******@TK2MSFT NGP11.phx.gbl.. .
how can i declare a function that will accept an optional parameter?

something like:

function newFunc(strValu e1, strValue2) --where strValue2 is optional.

thanks much.


Jul 19 '05 #5
"Harag" <ha***@REMOVETH ESECAPITALSsoft home.net> wrote in message
news:hj******** *************** *********@4ax.c om...


Ray, from one novice to another :)

I think you can use optional arguments with Jscript. but you can't
skip a parameter... eg if you leave off arg3 below you also have to
leave off 4 & 5.

if there is a way of just skipping arg3 below then I dont know it and
maybe a guru will help :)


<script language="JavaS cript" runat="SERVER">
function argTest(first,s econd,third){
Response.Write( "<br>First: " + (first?first:"N o parameter specified"));
Response.Write( "<br>Second : " + (second?second: "No parameter
specified"));
Response.Write( "<br>Third: " + (third?third:"N o parameter specified"));
}
</script>

<script language="VBScr ipt" runat="SERVER">
argTest "Apple",,"Canta loupe"
</script>

-Chris Hohmann
Jul 19 '05 #6
On Wed, 14 Jan 2004 15:29:40 -0800, "Chris Hohmann"
<no****@thankyo u.com> wrote:

[snipped]

[skipping an argument]
<script language="VBScr ipt" runat="SERVER">
argTest "Apple",,"Canta loupe"
</script>

-Chris Hohmann

ahh thanks Chris, didn't know you could just put double commas in.

I normally use either double quote (empty string) or put the word
null.

What I was tring to say is I dont know of a way where you can put the
parameters in whatever order you feel like it. I only know that you
have to specify them in order from first to last. Unless you pass it
as an object like the second function in the prev post.

By skipping I was thinking along the lines of parameters in a MSSQL
Stored Procedure.

EXEC usp_SampleProce dure @EmpID = 9, @EmpName = 'Jim'

Al.
Jul 19 '05 #7
"Harag" <ha***@REMOVETH ESECAPITALSsoft home.net> wrote in message
news:ab******** *************** *********@4ax.c om...
On Wed, 14 Jan 2004 15:29:40 -0800, "Chris Hohmann"
<no****@thankyo u.com> wrote:

[snipped]

[skipping an argument]
<script language="VBScr ipt" runat="SERVER">
argTest "Apple",,"Canta loupe"
</script>

-Chris Hohmann

ahh thanks Chris, didn't know you could just put double commas in.

I normally use either double quote (empty string) or put the word
null.

What I was tring to say is I dont know of a way where you can put the
parameters in whatever order you feel like it. I only know that you
have to specify them in order from first to last. Unless you pass it
as an object like the second function in the prev post.

By skipping I was thinking along the lines of parameters in a MSSQL
Stored Procedure.

EXEC usp_SampleProce dure @EmpID = 9, @EmpName = 'Jim'

Al.


I think the term is "named parameters". As far as I know neither JScript
nor VBScript supports named parameters.

HTH
-Chris Hohmann
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3827
by: Adam Dyga | last post by:
Hi, How to create function with optional parameter passed by reference? I've tried sth like this: function (&$param=NULL) // or: function (&$param=new array()) { //... }
1
2460
by: Ole S. Pedersen | last post by:
I have two tables: eg. a person-table (no nulls allowed), with an id and so on, and a person_course table (an intermediate table in a many-to many relationship between person table and courses tables), with two fields person_id and course_id. But I want to make ONE multipurpose stored procedure, which has ONLY optional parameters on all...
7
14178
by: (-: Dan :-) | last post by:
hi everybody suppose I have a function in a DLL function F1(var1, var2, var3) where var2 and var3 are optional parameter with default value (for exsample "-1") I create a new instance of my class and then I call F1.
21
5737
by: Marc DVer | last post by:
I am trying to create a query that can be loaded as a querydef object but not having to assign values to the parameters if I don't want to. Normally when using a parameter query in VBA my code would go something like this: dim qry as dao.querydef set qry = currentdb.querydefs("myquery") qry.parameters("Par1") = "blah"
10
11926
by: John Morgan | last post by:
Does anyone know what parameter should be used instead of Date = 0 for the optional parameter in the following function? Public Function dhAge(ByVal dtmBD As Date, Optional ByVal dtmDate As Date = 0) As Short
1
1789
by: Ed Chiu | last post by:
Hi, Is there a way to assign a DBNull value to an optional parameter of a function? I tried: Public Function AddUpdateCases( _ ByVal CrisCaseID As Integer, _ Optional ByVal APN As DateTime = System.DBNull.value)
1
4182
by: KayC | last post by:
Hi I am using Access2002 I have a form with 5 text boxes which are parameters to a function 3 parameters are optional in the function How do I pass the empty textbox values to a optional parameter? Completely stumped on how to do this Any ideas? Regards KC
2
1314
by: Curious | last post by:
I have two methods that are almost identical, except for the "HtmlLogger varLogger" parameter passed to #2. In #1, it uses a default HtmlLogger "logger" while in #2, it uses "varLogger" parameter. Other than that, the two methods are identical. I would like to combine the two methods into a single method, but I wish to know: - How to...
7
7046
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1 As Integer = 0, _ Optional ByVal Arg2 As Integer = 0, _ Optional ByVal Arg3 As Integer = 0) ' Call my SQL proc with the...
0
7673
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...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
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. ...
1
7645
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
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...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
926
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...

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.