Problem with Optional Arguments

is this sorted? - the code for your optional argument is incorrect

I just needed to remove the parenthesis from the function code when calling the function.
Everything works fine now thanks Dave.
 
All data types can be Optional but only Variants can be correctly tested using the IsMissing() function. The IsMissing() function tests the Variant’s ‘Missing’ value and only Variants have a ‘Missing’ value.

When an argument is not supplied…
Except for Variants:
When declared as Optional they are initialized as per normal, numbers to 0, strings to zero length string and so forth.

Optional Variants are different in that they are not initialized, to Empty, as per normal but instead have their Missing value set to True. For an Optional Variant, Empty and Null are both False, only its Missing value is set to True.

The concept of an argument being un-typed is bogus. If the type of the argument is not declared it defaults to type Variant, as would normally be the case. If it is Optional it is then treated as a Variant as per above.

What I would recommend to anyone, including myself as I get a bit fuzzy on the matter after a while, is to open a new module and start testing by writing code.

Chris.
 
And I did point that out per the article. I guess I am of the mindset that Variants are to be used sparingly (I typically only use them for arrays for the split function or the ItemsSelected of a listbox, etc).

IsMissing might be the easy way out but having a strongly typed optional parameter guarantees you don't have to worry about testing the validity of the input as compared to a variant. So, if I am going to be assigning a Date but someone can send any kind of text string to your code because variant can accept anything, then you have to test to make sure it is a valid date or suffer an error when attempting to use it. Or, passing a form to a parameter when you expect a number.

So, for my money, strongly typed parameters help with validation because it won't accept bad input and you don't have to be the one to notify the user about their crappy input. And it is simple to figure out if the parameter has the default and if it does you can either choose to bypass it or even use it if the situation warrants.
 
Well Bob and Chris have just improved my knowledge base. Thanks guys.
 
Dave:

That is a completely false statement. I just thought you ought to know that optional arguments can and SHOULD be strong typed. There is no programming language out there that I know of where that statement would be true. It isn't for VB6, VBA, VB.NET, C# etc.

bob

you are quite right.

I put this in without checking. What is true is that ismissing is only valid with a variant type optional argument - any typed optional arguments are never missing - or more correctly, it is not possible to tell whether a typed optional argument was passed or not.

http://msdn.microsoft.com/en-us/library/aa164532(office.10).aspx

that's what I got confused with.
 
it is not possible to tell whether a typed optional argument was passed or not.
which isn't necessarily a problem depending on what you're doing. In all of my code I have used a lot of optional arguments and have NEVER used IsMissing. So, I can state that may be overrated as a desired item.
 
which isn't necessarily a problem depending on what you're doing. In all of my code I have used a lot of optional arguments and have NEVER used IsMissing. So, I can state that may be overrated as a desired item.

bob,

I have used ismissing. If I have a function, and want to change its use slightly for a new purpose, I sometimes add an optional argument. Then I don't have to change any existing calls to the function, as long as the function works correctly, with or without the optional argument.

bit like using openargs in a form, I suppose.
 

Users who are viewing this thread

Back
Top Bottom