Assigning a default value to a function-variable

Discussion in 'ASP General' started by 0rsted, Jun 25, 2005.

  1. 0rsted

    0rsted Guest

    Well, it's quite simple, it's easily done in C, but in ASP (and VB in
    general) i haven't been able to see it done...

    function setvalues(var1, var2="a", var3="b")
    if var2="a" then
    response.write("Var2 wasn't defined")
    end if
    if var3="b" then
    response.write("Var3 wasn't defined")
    end if
    end function

    I hope it makes sense, and that someone has the solution to it...
    0rsted, Jun 25, 2005
    #1
    1. Advertising

  2. 0rsted wrote:
    > Well, it's quite simple, it's easily done in C, but in ASP (and VB in
    > general) i haven't been able to see it done...
    >
    > function setvalues(var1, var2="a", var3="b")
    > if var2="a" then
    > response.write("Var2 wasn't defined")
    > end if
    > if var3="b" then
    > response.write("Var3 wasn't defined")
    > end if
    > end function
    >
    > I hope it makes sense, and that someone has the solution to it...


    That's because it's not possible to assign default values for parameters in
    the method declaration statements in vb/vbscript.

    In VB, you can declare parameters using the Optional keyword, and use the
    IsMissing method to test whether or not the arguments were supplied. This is
    not the case in vbscript. Of course, given the fact that all variables are
    Variant in vbscript, it's not something that can't be worked around:

    <%
    function setvalues(var1,var2,var3)
    if len(var2)=0 then
    response.write("Var2 wasn't defined")
    end if
    if len(var3)=0 then
    response.write("Var3 wasn't defined")
    end if
    end function

    setvalue 6
    %>

    Bob Barrows

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
    Bob Barrows [MVP], Jun 25, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newbie
    Replies:
    6
    Views:
    8,487
    Bob Barrows [MVP]
    Jan 24, 2005
  2. Yipkei Kwok
    Replies:
    7
    Views:
    1,385
    Keith Thompson
    Apr 1, 2010
  3. michael75
    Replies:
    0
    Views:
    1,831
    michael75
    Aug 3, 2010
  4. 7stud --
    Replies:
    35
    Views:
    318
  5. weston
    Replies:
    1
    Views:
    247
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page