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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maxwell_Smart@ThePentagon.com

    #1

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

    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_Str ing 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.

  • Tom Shelton

    #2
    Re: Is there a Function and Function Argument generic self-reference?


    Maxwell_Smart@T hePentagon.com wrote:[color=blue]
    > 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.
    >[/color]

    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.Sto p ()"
    End Function
    End Class

    Private Class MyImplementatio n
    Inherits Common


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

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

    End Class

    Sub Main()
    Dim imp As New MyImplementatio n

    Console.WriteLi ne(imp.Go())
    Console.WriteLi ne(imp.Stop())
    End Sub

    End Module

    --
    Tom Shelton [MVP]

    Comment

    • chacham

      #3
      Re: Is there a Function and Function Argument generic self-reference?

      Tom Shelton wrote:[color=blue]
      > Maxwell_Smart@T hePentagon.com wrote:[color=green]
      > > 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.
      > >[/color]
      >
      > 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.Sto p ()"
      > End Function
      > End Class
      >
      > Private Class MyImplementatio n
      > Inherits Common
      >
      >
      > Public Overrides Function Go() As String
      > Return "MyImplementati on.Go ()" & " - " & MyBase.Go()
      > End Function
      >
      > Public Overrides Function [Stop]() As String
      > Return "MyImplementati on.Stop ()" & " - " & MyBase.Stop()
      > End Function
      >
      > End Class
      >
      > Sub Main()
      > Dim imp As New MyImplementatio n
      >
      > Console.WriteLi ne(imp.Go())
      > Console.WriteLi ne(imp.Stop())
      > End Sub
      >
      > End Module
      >
      > --
      > Tom Shelton [MVP][/color]

      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_Procedur e_Common(ByVal Command As DB2Command)
      Command.Command Type = CommandType.Sto redProcedure
      Command.Paramet ers.Add("OUT_PA RM_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.CreateComman d
      Get_Data1.Param eters.Add(GET_D ATA1_SPECIFIC_P ARM_1)
      Get_Data1.Param eters.Add(GET_D ATA1_SPECIFIC_P ARM_2)
      Get_Data1.Param eters.Add(GET_D ATA1_SPECIFIC_P ARM_3)
      Get_Data1.Param eters.Add(GET_D ATA1_SPECIFIC_P ARM_4)
      Get_Data1.Param eters.Add(GET_D ATA1_SPECIFIC_P ARM_5)

      Stored_Procedur e_Common(Get_Da ta1)
      End Function

      Public Function Get_Data2() As DB2Command
      Get_Data2 = DB.CreateComman d
      Get_Data2.Param eters.Add(GET_D ATA1_SPECIFIC_P ARM_1)
      Get_Data2.Param eters.Add(GET_D ATA2_SPECIFIC_P ARM_2)
      Get_Data2.Param eters.Add(GET_D ATA2_SPECIFIC_P ARM_3)
      Stored_Procedur e_Common(Get_Da ta2)
      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.CreateComman d
      Me..Parameters. Add(GET_DATA2_S PECIFIC_PARM_1)
      Me..Parameters. Add(GET_DATA2_S PECIFIC_PARM_2)
      Me..Parameters. Add(GET_DATA2_S PECIFIC_PARM_3)
      Stored_Procedur e_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.

      Comment

      Working...