473,669 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optional variables in a function

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(str Tbl as string, arr as ArrayList, Optional
SortCol as string = "Yr desc")

Calling procedure:

qry = SelectQuery(str Tbl, arr, SortCol)
Nov 19 '05 #1
2 1650
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(str Tbl, arr)

"et" <ea************ *@yahoo.com> wrote in message
news:%2******** ********@tk2msf tngp13.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(str Tbl as string, arr as ArrayList, Optional
SortCol as string = "Yr desc")

Calling procedure:

qry = SelectQuery(str Tbl, arr, SortCol)


Nov 19 '05 #2
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(str Table as string, arr as ArrayList) as XXXX
return SelectQuery(str Table, arr, "Yr desc")
end function

Public Function SelectQuery(str Tbl 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...t here 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******** ********@tk2msf tngp13.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(str Tbl as string, arr as ArrayList, Optional
SortCol as string = "Yr desc")

Calling procedure:

qry = SelectQuery(str Tbl, arr, SortCol)

Nov 19 '05 #3

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

Similar topics

9
2977
by: BKDotCom | last post by:
Many built-in functions allow an optional variable to be passed for error messages. How do I implement this in my own function? function test($param1, $param2, &$errormsg) { // complains if 3rd param is not sent } function test($param1, $param2, &$errormsg='') { // complains about something }
9
4569
by: Rob Long | last post by:
Hey, I've just noticed a somewhat annoying feature with PHP5 type hinting. You cannot hint types on optional arguments like this: class MyClass { function someFunc(Type1 $arg1, Type2 $arg2 = new Type2()) { }
4
6173
by: Gerry Abbott | last post by:
Hi All, Im trying to use thie combination but have not had success. Below is the function It tried the following myriskLevel(2,2) myrisklevel(0,0,2) and the ismissing(Three) alwasy returns false.?
13
2512
by: William Ryan | last post by:
I just picked up a copy of John Robbins' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was checking them out to see what IL is created. I've done this before with Modules, and saw that <gasp> they behave just like sealed classes with only static members. Anyway, it looks like Optional Parameters are nothing but a way to tell the compiler to write some overloads for you. So, in...
16
4057
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
14
3262
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself which one to pass and which not, rather than relying on massively overlaoded methods which hopefully provide the best...
12
2661
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be avoided. I'd like to hear your various opinions on this matter.
1
1264
by: sake | last post by:
Hey, If you don't want to read all this, just look at the bottom part :-P I'm currently making a Tic Tac Toe game, just for fun, and ran across an interesting subject(for me). I have a method in class Player that can be used for two uses. It can check for a win and check for a possible win. Once it finds a possible win, it puts the X and Y coords of that possible win into a variable, located in the class. But what if I wanted to fulfill...
7
7051
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 same signature
0
8383
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8809
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8588
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1788
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.