substitue for vb6 IsMissing()

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

for a function parameter of a non-reference datatype ... such as date or
boolean ....

how to determine if the parameter was not specified in the calling code ?

in vb6 we would have used the IsMissing() function ...
 
I think .. there is no IsMissing() in VB.NET.
Instead ... VB.NET enforces specifying of default value for optional
parameters. So you can set the optional param. default value to Nothing / -1
/ "" whatever as per the datatype and check inside the function if the value
is default / value (overrides default value) specified by the caller.
ex. -
private function ABC(Byval X as integer,Optional Byval Y as integer = -1)
.....
.....
end function

~ Kapil
 
hi kapil, and thanks for the response.

i don't think you understand my post.

i specifically asked about non-reference type variables, such as Date and
Boolean. these type of variables can not be Nothing -- since they are not
reference variables it makes no sense for them to not refer to any object --
which is what Nothing means.

if i use a default value of False for an Optional Boolean type function
parameter, and then attempt to determine if the calling code supplied a
value by checking if the parameter's value is False, this leads to an
erroneous result if the calling code did supply a value , and that value =
False.
 
if you realy need to know if the parameter was passed or not, just create
two methods...

public sub test()
....
end sub
public sub test(value as string)
....
end sub

I suppose the 2 method will do almost the same thing, then maybe do
something like this

public sub test()
realTest("", true)
end sub
public sub test(value as string)
realTest(value, false)
end sub

private sub realTest(value as string, isDefaultString as boolean)
...
end sub
 
hi dominique, and thanks for the response.

because of overloading, it appears to me that your suggestion would work.

but don't you think there's got to be a better way ?

why would the vb.net architects make it so difficult to implement this
much-used and much-needed functionality for parameters of non-reference
datatypes ?
 
Hi John,

I think because you cannot do MySub() when the procedure is

MySub(byval as date)

This gives an error in your IDE or compiler.

Cor
 
hi dominique, and thanks for the response.

because of overloading, it appears to me that your suggestion would work.

but don't you think there's got to be a better way ?

Not really...
why would the vb.net architects make it so difficult to implement this
much-used and much-needed functionality for parameters of non-reference
datatypes ?

Well, it isn't really needed in VB.NET since VB.NET forces you to give
optional parameters a default value... So, why do you care if the user
passed a value or not - your going to get one, and it should be clear to
the user what value will be passed if they don't provide a value.

Personally, I prefer to use overloads over optional parameters. The main
reason is that the value of the parameter gets compiled into the assembly
and essentially becomes part of the method contract... If you change the
default value in a future version you end up breaking old clients.

--
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
"He expanded his chest to make it totally clear that here
was the sort of man you only dared to cross if you had a
team of Sherpas with you. "
 
John A Grandy said:
hi dominique, and thanks for the response.

because of overloading, it appears to me that your suggestion would
work.

but don't you think there's got to be a better way ?

IMO: No. There is overloading and you still can declare the arg as Object to
use optional args.
why would the vb.net architects make it so difficult to implement
this much-used and much-needed functionality for parameters of
non-reference datatypes ?

In VB6, IsMissing also only worked for Variants, so I don't see a change for
the worse.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
* "John A Grandy said:
for a function parameter of a non-reference datatype ... such as date or
boolean ....

how to determine if the parameter was not specified in the calling code ?

in vb6 we would have used the IsMissing() function ...

No! 'IsMissing' worked with variants only.
 
<<<<
Well, it isn't really needed in VB.NET since VB.NET forces you to give
optional parameters a default value... So, why do you care if the user
passed a value or not - your going to get one, and it should be clear to
the user what value will be passed if they don't provide a value.
user? what user? why are you assuming i want this functionality in order
to process values of various UI controls ?

i'm talking about the following type of functionality ( code written
*assuming* VB.NET did not require default values for Optional parameters,
and that IsMissing() was available)

Private Sub PanelSetProperties(ByRef pPanel As Panel, Optional ByVal
pVisible As Boolean, Optional ByVal pEnabled As Boolean)

If Not pPanel Is Nothing Then

If Not IsMissing(pVisible) Then
pPanel.Visible = pVisible
End If

If Not IsMissing(pEnabled) Then
pPanel.Enabled = pEnabled
End If

End If

End Sub


extend this for more parameters/properties than just two, and this is a very
nice little sub to have because it allows me to make calls like

PanelSetProperties(,False) 'disable a panel without affecting (or even
knowing or caring about) its visible status

PanelSetProperties(True) 'make a panel visible without affecting (or even
knowing or caring about) its enabled status
 
there aren't any variants in vb.net

so i think that my analogy is valid

the point is the following:

in vb6 it was possible to write a function/sub so that a parameter of any
datatype could be declared so that it was possible to perform a check inside
the function as to whether or not a value had been specified in the calling
code. yes, i know we had to use variants, but the vartype function could be
applied to determine the underlining datatype. you are being unnecessarily
pedantic.

further, it was possible to specify multiple optional parameters so that in
the calling code any combination of parameters could be left out of the
arguments list, if desired.

if vb.net it is no longer possible to write such a function. i this
particular area, we have gone backwards in -- a practical sense.

*practical* is the key word here ... PRACTICAL ... ok???? a word that seems
to have escaped the vb.net architects. every day i have to write yards of
code to do what could be accomplished in much less code in vb6.
 
John,
in vb6 it was possible to write a function/sub so that a parameter of any
datatype could be declared so that it was possible to perform a check inside
the function as to whether or not a value had been specified in the calling
code.
Remember that all datatypes ultimately derive from Object, which means:
if vb.net it is no longer possible to write such a function. i this
particular area, we have gone backwards in -- a practical sense.
In VB.NET it is most certainly possible! To declare a function that accepts
"any data type" you define the parameter as Object, if you make an Object
parameter Optional, then you need to use Nothing as the default value. To
check to see if the parameter is missing you would then check

However!!! Do you write a single function that accepts a single parameter
that you then attempt to determine the parameter type of, this is what
Overloading is for, you write individual functions that accept specific
types, and find problems at compile time, instead of possible at run time.
This way if your function only accepts x, y, and z types you write three
functions that specifically accept those types, at compile time if you try
to pass w, you will get a compile error. If you want to know the parameter
was not "passed" declare an overload that does not accept a value!
*practical* is the key word here ... PRACTICAL ... ok???? a word that seems
to have escaped the vb.net architects. every day i have to write yards of
code to do what could be accomplished in much less code in vb6.
In my experience once you have the "proverbial AHA!" on OOP (overloading in
this case) then you will find that VB.NET is very practical and the VB6
method was not so practical.

Hope this helps
Jay

John A Grandy said:
there aren't any variants in vb.net

so i think that my analogy is valid

the point is the following:

in vb6 it was possible to write a function/sub so that a parameter of any
datatype could be declared so that it was possible to perform a check inside
the function as to whether or not a value had been specified in the calling
code. yes, i know we had to use variants, but the vartype function could be
applied to determine the underlining datatype. you are being unnecessarily
pedantic.

further, it was possible to specify multiple optional parameters so that in
the calling code any combination of parameters could be left out of the
arguments list, if desired.

if vb.net it is no longer possible to write such a function. i this
particular area, we have gone backwards in -- a practical sense.

*practical* is the key word here ... PRACTICAL ... ok???? a word that seems
to have escaped the vb.net architects. every day i have to write yards of
code to do what could be accomplished in much less code in vb6.


code
 
John A Grandy said:
there aren't any variants in vb.net

so i think that my analogy is valid

the point is the following:

in vb6 it was possible to write a function/sub so that a parameter of
any datatype could be declared so that it was possible to perform a
check inside the function as to whether or not a value had been
specified in the calling code. yes, i know we had to use variants,
but the vartype function could be applied to determine the
underlining datatype. you are being unnecessarily pedantic.

Right, but using "As Object" instead of "As Variant", you can do the same
now. Instead of the Variant sub type, you can now use TypeOf to check the
type, or use "Is Nothing" to check if the arg has been passed.
further, it was possible to specify multiple optional parameters so
that in the calling code any combination of parameters could be left
out of the arguments list, if desired.

Still possible:

test(1, , Me)
test(2, o2:=Me)
'...
Sub test( _
ByVal x As Integer, _
Optional ByVal o1 As Object = Nothing, _
Optional ByVal o2 As Object = Nothing)

If o1 Is Nothing Then
MsgBox("o1 is nothing")
End If

End Sub

if vb.net it is no longer possible to write such a function. i
this particular area, we have gone backwards in -- a practical
sense.

*practical* is the key word here ... PRACTICAL ... ok???? a word
that seems to have escaped the vb.net architects. every day i have
to write yards of code to do what could be accomplished in much less
code in vb6.

The only practical drawback I see is that you have to write "= Nothing" for
each optional parameter. This default value, VB6 assumed on it's own, but
that's only a little more to type in, so I don't see a big problem.
 
Back
Top