I am using a procedure which involves parameter passing and the parameter being passed is a variable. Because I have explicitly declared the data type of another parameter, I need to do the same for this one. What data type do I declare the parameter as if it is a variable? Thanks
2 Answers
An example of what you are doing and what Types you are dealing with would have been nice. You can implement Overloading
to provide for different parameter Types:
Friend Function FooBar(n As Integer) As Integer
Friend Function FooBar(n As Int64) As Integer
Friend Function FooBar(n As Short) As Integer
The compiler will pick the function which matches the data type being passed. Internally, they might do whatever based on the Type passed, then call another procedure to perform any stuff common to them all.
There is probably a finite number of types you need it to work with. For instance Font
, Point
and Rectangle
probably make no sense. Even Date
is dubious because you cannot do stuff to a date in the same way as with an Int or Long. String
is also not likely needed because you can always pass it as FooBar(CInt(someString))
provided it does contain a valid integer or whatever.
You can also use a generic to tell the function what you are passing:
Private Function FooBar(Of T)(parm As T) As Integer
' called as:
ziggy = FooBar(Of Int32)(n)
zoey = FooBar(Of String)(str)
This might even be Private Function FooBar(Of T)(parm As T) As T
if the function return varies depending on the parameter Type
passed. There are many uses for this (one of which is to avoid passing a param as Object
), but as a general purpose way of passing any type you want it is not a good idea: internally you will likely have to have a big If/Else to handle the different types their own way.
Turning off Option Strict
is never advisable since all sorts of unwanted type conversions can take place.

- 38,411
- 12
- 59
- 178
In VB.NET, you can use Object as the type but with Option Strict Off. You can pass any kind of parameter in that case. for more information, refer : https://stackoverflow.com/a/2890023/3660930