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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function definitions

Status
Not open for further replies.

iainm

Technical User
Nov 28, 2001
67
0
0
AU
I'm a Java programmer, and I've got some (old) visual basic training, but as some of you have already found I'm having serious difficulties learning access basic. The question for today is thus:
In Java it is possible to declare a function more than once with different inputs, so that depending on what variables get passed to it a different version of the function runs. An example of this is as follows

Private Sub Display(intInput As Integer)
intValue = intInput * intInput
MsgBox intValue
End Sub

Private Sub Display(strInput As String)
MsgBox strInput
End Sub

then I can call

Display 4

and the compiler would recognise that I'm passing an integer, and run the first sub. The output would be a message box with 16 in it
Or I could call

Display "Hello"

and the output would be a message box with &quot;Hello&quot; in it. Is it possible to do this with VB? If not, is there a workaround that'll give the same results? I was thinking I could have one of each type of input, then each time I call it I set all passed variables to null except the one I want to work with, then have a casewhere each one <> null. This could get very messy, so I'd prefer it if I could do multiple declarations. Does anyone have any idea what I'm babbling about? If so, please help me out, or just give me the name of a good therapist.
 
Here's a simple approach I have used many times.

Private Sub Display(varInput As Variant, intAction As Integer)

dim intValue as Integer

select case intAction
case 1 ' 1 indicates an integer data value
intValue = varInput * varInput
MsgBox (intValue)
case 2 ' 2 indicates a string data value
MsgBox (varInput)
end select

End Sub


Hope you find it fits your needs.
 
Great, thanks. I was thinking about playing with variant types, but I'm not quite sure how to manipulate them. How do you specify what data type it should be interpreted as? Is it assumed to be string, or whatever datatype you copy it into, or is it stored with the data?
 
Your absolutely right! A long time ago, by trial and error, I tested how variant-declared data would react in different situations e.g. in a variety of arithmetic expressions. The results convinced me to stick to the explicit declaration policy I had always used in the past.

In my original suggestion to you, I tried to adhere to your original code layout, knowing that I could get away without having to coerce the variant text data into a string variable.

Using my earlier solution, I've now included declarations for all procedure variables.

Private Sub Display(varInput As Variant, intAction As Integer)

dim intValue as Integer
dim strValue as String

select case intAction
case 1 ' 1 indicates an integer data value
intValue = varInput * varInput
MsgBox (intValue)
case 2 ' 2 indicates a string data value
strValue = varInput
MsgBox (strValue)
end select

End Sub

Hope this explanation is better.
 
Yep VB does not support function overloadin unfortunately. I guess you wan to do it so that you don't know what the data type is that you pass to the function - that way you just want to pass the variable itself not with another one stating the data type. If so just pass it as a variant and use checks in the function itself like isnumeric(var) etc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top