Function Parameters

  • Thread starter Thread starter hharry
  • Start date Start date
H

hharry

Hello All,

Is it possible to force a function argument to fall with a range of
values ?

For example:

I have a function which searches a database table and accepts a query
type argument which I would like to be either 1 or 0.

Public Function IsFound(iQueryType As Integer, sName As String)

Can I do this and if so what would be the syntax..


Thanks In Advance
 
hharry,

If the arguments can only be 1 or 0 then I would use a boolean.
Public Function IsFound(iQueryType As Boolean, sName As String)
 
hharry said:
Is it possible to force a function argument to fall with a range of
values ?

That's what coding is for ... ;-)
I have a function which searches a database table and accepts a
query type argument which I would like to be either 1 or 0.

In this particular case, you could /suggest/ values by using an Enum,
as in

Public Enum QueryType
[Type0] = 0
[Type1] = 1
End Enum

Public Function IsFound( _
ByVal eaType as QueryType _
, ByVal sName As String _
) as ??? ' Option Strict On /please/

But that *doesn't* guarantee that you're /always/ going to get one
of those values - since Enums are boiled down to Integers at compile
time, you could /still/ get some awkward .. Soul who manages to foist
the value 37 on your poor, defenceless Function. So you code
"defensively" :

Public Function IsFound( _
ByVal eaType as QueryType _
, ByVal sName As String _
) as ??? ' Option Strict On /please/

If Not [Enum].IsDefined(GetType(QueryType), eaType) Then
Throw New ArgumentException( ...
End If

' Do the real work here

End Function

HTH,
Phill W.
 
As Pipo mentioned, if the parameter is going to have only 2 values, you are
better off making the parameter a boolean type. In general, if you need fixed
values, you should use enums.

Enum QueryType
Type1=1
Type2
Type3
Type4
End Enum

Public Function IsFound(iQueryType As QuertyType, sName as String)

hope that helps..
Imran.
 
Imran,
As Pipo mentioned, if the parameter is going to have only 2 values, you
are better off making the parameter a boolean type.
However! I would recommend an Enum with 2 values instead of Boolean, if the
values of the Enum are more descriptive then simply True & False.

For example

Public Enum Direction
Forward = 0
Backward = 1
End Enum

Public Sub Move(ByVal dir As Direction)
...
End Sub


Move(Direction.Forward)

verses:

Move(True)

I hope you will agree that "Move(Direction.Forward)" is more obvious what is
going to happen then "Move(True)"...

Although I will admit I probably use a boolean more then I use 2 value
Enums... :-)

Just a thought
Jay
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top