Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

the Optional parameter

Status
Not open for further replies.

Tapeworm

Programmer
Mar 6, 2001
8
0
0
DE
Why is here the Output here 4 and ot 5 ?
Is it one of the many VB Bugs or my code ?

Private Sub Command1_Click()
Dim i As Integer
i = 4
Call inc(i, ,3)
MsgBox i '---> Output 4 , why ?
End Sub

Private Sub inc(c As Integer, Optional b As Integer, _ Optional d As Integer)
If IsMissing(b) Then
c = c + 1
Else
c = c + b
End If
End Sub
 
isMissing doesn't work with the Integer argument. If you don't pass a value for b it gets value 0. And your result is c + b = 4 (4+0).

Tom
 
The argument of the IsMissing funtion MUST be a Variant Data Type. The reason is that simple data type (such as Integer) do not have provision for a "Missing" flag bit. Note that a Variant variable takes much more memory space.

Sometimes you can simulate the IsMissing function by providing a default value for the optional argument.

Private SomeFunction (intA as Integer, Optional intB as Integer = 0)

Select Case intB
Case 0

'Argument was Missing
Case Else

'Argument Present
End Select

End Function


The obvious drawback to this method is that you can't distinguish between intB as Missing and intB intentionally passed as Zero!

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
It is a better practice to always use default values for any arguements which you declare as optional. Furthermore,
I suggest you not check if the arguement is missing but instead set the default value as 1.

example:

Private sub inc(c as integer, optional b as integer = 1)
c = c + b
end sub

If you noticed in your code, you did not set the c to be "ByRef" and if so... how are you suppose to be able to change the value of i if you only pass in the i as "Byval" which is the default.

In writing functions and subs, you should always remember to state if you are passing arguements by value or reference. That way, it would be alot easier to debug.
 
Amongst all of the above, I did not note any questioning of Tapeworms overall logic/functionallity.

No reference is made to the other optional parameter "d", which is not used in sub inc. Nor does anyone want to know the WHY would someone want to either add 1 or some other passed value? Altogether, the Sub inc is just to simple to want/need to be a sub. There MUST be something else going on here?

I would like to know some more about this "simple" process.


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Dear Michael,

telling the truth, this is a top secret snippet from my code
which will soon enable myself to remote control the American master puppet ...ehhh... president I mean.



No, don't call the NSA, it's just an interpretation of a snippet out of an Addison-Wesley book. But the remarking thing about this is that they tell that optional is working with variant since version 5 but NOT that ismissing() still works only with variant.
For an Addison Wesley book that's the really strange thing about the whole story !




 
I'm sorry Micheal but those might just be experimental code which is why "d" isn't referenced and tapeworm might not have finished the whole of the coding so..... nobody here wants to question if tapeworm is an amateur or know to much of what others are doing as long as they answer the specific question tapeworm is asking

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top