Is there a Function and Function Argument generic self-reference?

M

Maxwell_Smart

Is there a way for a function to refer to itself generically? I'd like
to use such a thing (should it exist) for convenience and consistency,
not functionality.

For example:

Function Common(Some_String as String) As String
...
End Function

Function Go() As String
Dim Some_String = New String
Common(Go)
....
End Function

Function Stop() As String
Dim Some_String = New String
Common(Stop)
....
End Function

In this example, both Go() and Stop() want to call Common() to take
care of some common code. Being Go() an Stop() are instantiated
objects, they pass themselves as the argument. The question is, is
there a way for the code to be the same, something like Common(Me),
where Me is a reference to the function itself. Dimming a new object
(of common name) defeats the purpose, however.

-----

Is there a way of refering to the functions arguments? For example,
this would be what i would want to do:

Sub Reset(Obj As Object, First_Name As String, Last_Name as String,
Address As String)

Dim Counter As Integer

For Counter = 1 to Reset.Arguments.Count -1

Set Obj.Parameters(Reset.Arguments(Counter).Name).Value =
Reset.Arguments(Counter).Name

Next Counter

End Function


B.
 
T

Tom Shelton

Is there a way for a function to refer to itself generically? I'd like
to use such a thing (should it exist) for convenience and consistency,
not functionality.

Well, if I understan what you're asking, you could achieve something
like this with inheritance. You would create a base class containing
the common code and then inherit from it. Then, its a matter of
calling your base class implementaion... Something like:

Option Strict On
Option Explicit On

Module Module1

Private Class Common
Public Overridable Function Go() As String
Return "MyBase.Go ()"
End Function

Public Overridable Function [Stop]() As String
Return "MyBase.Stop ()"
End Function
End Class

Private Class MyImplementation
Inherits Common


Public Overrides Function Go() As String
Return "MyImplementation.Go ()" & " - " & MyBase.Go()
End Function

Public Overrides Function [Stop]() As String
Return "MyImplementation.Stop ()" & " - " & MyBase.Stop()
End Function

End Class

Sub Main()
Dim imp As New MyImplementation

Console.WriteLine(imp.Go())
Console.WriteLine(imp.Stop())
End Sub

End Module
 
C

chacham

Tom said:
Is there a way for a function to refer to itself generically? I'd like
to use such a thing (should it exist) for convenience and consistency,
not functionality.

Well, if I understan what you're asking, you could achieve something
like this with inheritance. You would create a base class containing
the common code and then inherit from it. Then, its a matter of
calling your base class implementaion... Something like:

Option Strict On
Option Explicit On

Module Module1

Private Class Common
Public Overridable Function Go() As String
Return "MyBase.Go ()"
End Function

Public Overridable Function [Stop]() As String
Return "MyBase.Stop ()"
End Function
End Class

Private Class MyImplementation
Inherits Common


Public Overrides Function Go() As String
Return "MyImplementation.Go ()" & " - " & MyBase.Go()
End Function

Public Overrides Function [Stop]() As String
Return "MyImplementation.Stop ()" & " - " & MyBase.Stop()
End Function

End Class

Sub Main()
Dim imp As New MyImplementation

Console.WriteLine(imp.Go())
Console.WriteLine(imp.Stop())
End Sub

End Module

Firstly, thanx for responding. I appreciate it.
Secondly, sorry for using "stop" as a function name. Silly me using a
keyword. :)
Thirdly, I have no real working knowledge with inheritance, and do not
understand what the override did exactly.
Finally, why are you returning them in quotes? I chose string as an
object, not because i wanted a string.

Here's what i am trying to do. I have a number of database procedures
(DB2) that will return CURSORs to my DB2Commands for the DataAdaptors
to use. There are quite a few of them, and i need to specify the
parameters for each. But, some code is the same. So, for right now, the
basic is something like:

Sub Stored_Procedure_Common(ByVal Command As DB2Command)
Command.CommandType = CommandType.StoredProcedure
Command.Parameters.Add("OUT_PARM_ERROR_CODE")
End Sub

That would be the common code to be added to all DB2Commands for stored
procedures after the "real" function adds the specific parameters. For
example:

Public Function Get_Data1() As DB2Command
Get_Data1 = DB.CreateCommand
Get_Data1.Parameters.Add(GET_DATA1_SPECIFIC_PARM_1)
Get_Data1.Parameters.Add(GET_DATA1_SPECIFIC_PARM_2)
Get_Data1.Parameters.Add(GET_DATA1_SPECIFIC_PARM_3)
Get_Data1.Parameters.Add(GET_DATA1_SPECIFIC_PARM_4)
Get_Data1.Parameters.Add(GET_DATA1_SPECIFIC_PARM_5)

Stored_Procedure_Common(Get_Data1)
End Function

Public Function Get_Data2() As DB2Command
Get_Data2 = DB.CreateCommand
Get_Data2.Parameters.Add(GET_DATA1_SPECIFIC_PARM_1)
Get_Data2.Parameters.Add(GET_DATA2_SPECIFIC_PARM_2)
Get_Data2.Parameters.Add(GET_DATA2_SPECIFIC_PARM_3)
Stored_Procedure_Common(Get_Data2)
End Function

The first statement is common to all functions, but is only one
statement. The second (and third and forth, for all parameters the
stored PROCEDURE takes) statment is specific to each function. The
final statement call a common function to execute all that is common to
all of them.

I deally, the code would read

Public Function Get_Data2() As DB2Command
Me. = DB.CreateCommand
Me..Parameters.Add(GET_DATA2_SPECIFIC_PARM_1)
Me..Parameters.Add(GET_DATA2_SPECIFIC_PARM_2)
Me..Parameters.Add(GET_DATA2_SPECIFIC_PARM_3)
Stored_Procedure_Common(Me)
End Function

Or, if some form of inheritance could do it.

I am not sure how many we need to do, but it's probably between two and
three hundred, so common code would help inn writing, manintenance, and
understanding.

B.
 

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

Top