Is it posible to implement polymorphic function/Sub in VBA?

jriano

Registered User.
Local time
Today, 12:24
Joined
Dec 9, 2003
Messages
45
The title says it all. Is it posible? How? I have not been able to find a straight answer and/or example. Thank you guys for any help.
 
Do you have a mathmatical or logical model?
 
yes, I want to use this function below, so that the intDataToInsert parameter can be either a string or an integer
Code:
'Polymorphic version for integer
Public Function SetSqlUpdateStatmt(RS As ADODB.Recordset, _
    strTableToUpdate As String, _
    strFieldToUpdate As String, _
    intDataToInsert As integer, _
    strFieldToBeUsedInWhereStatement) As String
    If IsNull(RS) Then
        MsgBox "The recordset was not initializes. (Function SetSqlUpdateStatmt)"
    Else
        SetSqlUpdateStatmt = "UPDATE " & strTableToUpdate & " SET " & strFieldToUpdate & _
        " = " & intDataToInsert & " WHERE " & strFieldToBeUsedInWhereStatement & " = " & _
        RS(strFieldToBeUsedInWhereStatement)
    End If
End Function
 
Could you set it up as string, that will get it in the function, then test and convert it from there if / as needed?
 
yes, I can do that, I was just curious to know it VBA can handle polymorphic functions....
 
I'm lost. I know what polymorphic means but I don't see how you are applying it as an attribute of a function unless you are wanting to return different data types. If so, return the function as a Variant.
 
i think your question boils down to can you overload function definitions in VBA and so far as i know the answer is no. but...

since you're only using that parameter to build a SQL string, what's wrong with passing the integer value as a string? as your code stands the parameter is implicitly cast to a string type before being concatenated into the SQL statement anyway. now, if you get a "real" string, you'll need to surround it with the ' delimiter in the finished SQL statement, so you'll need to be able to tell the difference between an integer passed as a string and a "real" string. you could do that by attempting to cast the string to an integer and trapping any error. if you get an error, the string must be a plain string and not an integer, whereas if the cast works you know you've got an integer on your hands. it would go something like this:

Code:
On Error Goto NonNumericString
intDummy = CInt(TheParameter)

'If we made it to here without an error being raised, the parameter is an integer-string
'Do your SQL concatenation here as appropriate

Exit Function

NonNumericString:
    'If we made it here, an error was raised and the parameter is just a plain string
    'Do your SQL concatenation here as appropriate
    Exit Function
 
Yes my question was aimed towards overloading, I did not select the best example, sorry. In this example the return data type is the same, but I am interested in being able to return different data types, you guys clarified that anyway. Thanks.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom