Calling Sub Procedure with a variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ILCSP@NETZERO.NET

    #1

    Calling Sub Procedure with a variable

    Hi, I'm using VB.Net (2003) and I have a question. Does anyone knows
    how to call a procedure using a variable? The variable will be equal to
    the name of the procedure.

    for example, if I have the following variable:

    Public PrintTheSub as string

    and the following subs:


    Private Sub PrintOrdersRepo rts ()
    ' the code for this sub goes here
    End Sub

    Private Sub PrintCustomerRe ports()
    ' the code for this sub goes here
    End Sub

    Private Sub PrintMonthlyRep ort()
    ' the code for this sub goes here
    End Sub


    and I have a button with the public variable in it:

    Private Sub btnPrint_Click( ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles btnPrint.Click
    ' here will go the variable that will call one of the 3 subs.
    PrintTheSub
    End Sub

    And during the process but before getting to the print button, the
    PrintTheSub variable = "PrintCustomerR eports"

    How can I make the print button to accept the variable value and when
    clicked it could call the PrintCustomerRe ports sub?


    Any help would be appreciated.

  • Al Reid

    #2
    Re: Calling Sub Procedure with a variable


    <ILCSP@NETZERO. NETwrote in message
    news:1152825059 .565444.53240@7 5g2000cwc.googl egroups.com...
    Hi, I'm using VB.Net (2003) and I have a question. Does anyone knows
    how to call a procedure using a variable? The variable will be equal to
    the name of the procedure.
    >
    for example, if I have the following variable:
    >
    Public PrintTheSub as string
    >
    and the following subs:
    >
    >
    Private Sub PrintOrdersRepo rts ()
    ' the code for this sub goes here
    End Sub
    >
    Private Sub PrintCustomerRe ports()
    ' the code for this sub goes here
    End Sub
    >
    Private Sub PrintMonthlyRep ort()
    ' the code for this sub goes here
    End Sub
    >
    >
    and I have a button with the public variable in it:
    >
    Private Sub btnPrint_Click( ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles btnPrint.Click
    ' here will go the variable that will call one of the 3 subs.
    PrintTheSub
    End Sub
    >
    And during the process but before getting to the print button, the
    PrintTheSub variable = "PrintCustomerR eports"
    >
    How can I make the print button to accept the variable value and when
    clicked it could call the PrintCustomerRe ports sub?
    >
    >
    Any help would be appreciated.
    >
    How about an If...Then or a Select Case?

    Private Sub btnPrint_Click( ByVal sender As System.Object, _
    ByVal e As System.EventArg s) Handles
    btnPrint.Click
    Select Case PrintTheSub
    Case "PrintCustomerR eports"
    PrintCustomerRe ports()

    Case "PrintOrdersRep orts"
    PrintOrdersRepo rts()

    Case "PrintMonthlyRe port"
    PrintMonthlyRep ort()
    End Select

    End Sub


    I would probably make an Enum for the reports and use that instead of a
    string.

    --
    Al Reid


    Comment

    • Mattias Sjögren

      #3
      Re: Calling Sub Procedure with a variable


      You can use the CallByName function for this. But personally, I'd use
      a delegate to refer to the method to be called instead of the
      procedure name in a string.


      Mattias

      --
      Mattias Sjögren [C# MVP] mattias @ mvps.org
      http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      Please reply only to the newsgroup.

      Comment

      • ILCSP@NETZERO.NET

        #4
        Re: Calling Sub Procedure with a variable

        Hi Al, thanks for replying. I did think about a case statement, but
        since the final project will have over 200 reports, the case statement
        will not be as good as if I used the variable name.

        I was hoping that application.run (PrintTheSub) was gonna work, but it
        did not.


        Al Reid wrote:
        <ILCSP@NETZERO. NETwrote in message
        news:1152825059 .565444.53240@7 5g2000cwc.googl egroups.com...
        Hi, I'm using VB.Net (2003) and I have a question. Does anyone knows
        how to call a procedure using a variable? The variable will be equal to
        the name of the procedure.

        for example, if I have the following variable:

        Public PrintTheSub as string

        and the following subs:


        Private Sub PrintOrdersRepo rts ()
        ' the code for this sub goes here
        End Sub

        Private Sub PrintCustomerRe ports()
        ' the code for this sub goes here
        End Sub

        Private Sub PrintMonthlyRep ort()
        ' the code for this sub goes here
        End Sub


        and I have a button with the public variable in it:

        Private Sub btnPrint_Click( ByVal sender As System.Object, ByVal e As
        System.EventArg s) Handles btnPrint.Click
        ' here will go the variable that will call one of the 3 subs.
        PrintTheSub
        End Sub

        And during the process but before getting to the print button, the
        PrintTheSub variable = "PrintCustomerR eports"

        How can I make the print button to accept the variable value and when
        clicked it could call the PrintCustomerRe ports sub?


        Any help would be appreciated.
        >
        How about an If...Then or a Select Case?
        >
        Private Sub btnPrint_Click( ByVal sender As System.Object, _
        ByVal e As System.EventArg s) Handles
        btnPrint.Click
        Select Case PrintTheSub
        Case "PrintCustomerR eports"
        PrintCustomerRe ports()
        >
        Case "PrintOrdersRep orts"
        PrintOrdersRepo rts()
        >
        Case "PrintMonthlyRe port"
        PrintMonthlyRep ort()
        End Select
        >
        End Sub
        >
        >
        I would probably make an Enum for the reports and use that instead of a
        string.
        >
        --
        Al Reid

        Comment

        • tomb

          #5
          Re: Calling Sub Procedure with a variable

          You could do this:
          In the declarations:

          Delegate Sub PrintOReport()
          Delegate Sub PrintCReport()
          Delegate Sub PrintMReport()
          Private OP as PrintOReport
          Private CP as PrintCReport
          Private MP as PrintMReport
          Private sPrint As [Delegate]

          In some proc, pass the three addresses of the real subs, like this:
          public sub setDelegates(by ref so as PrintOReport, byref sc as
          PrintCReport, byref sm as PrintMReport)
          op=so
          cp=sc
          mp=sm
          end sub

          Then in your code logic, set sPrint equal to one of the delegate
          variables. When the button is clicked, the correct proc will be called.

          T


          ILCSP@NETZERO.N ET wrote:
          >Hi, I'm using VB.Net (2003) and I have a question. Does anyone knows
          >how to call a procedure using a variable? The variable will be equal to
          >the name of the procedure.
          >
          for example, if I have the following variable:
          >
          >Public PrintTheSub as string
          >
          and the following subs:
          >
          >
          >Private Sub PrintOrdersRepo rts ()
          ' the code for this sub goes here
          >End Sub
          >
          >Private Sub PrintCustomerRe ports()
          ' the code for this sub goes here
          >End Sub
          >
          >Private Sub PrintMonthlyRep ort()
          ' the code for this sub goes here
          >End Sub
          >
          >
          >and I have a button with the public variable in it:
          >
          Private Sub btnPrint_Click( ByVal sender As System.Object, ByVal e As
          >System.EventAr gs) Handles btnPrint.Click
          ' here will go the variable that will call one of the 3 subs.
          PrintTheSub
          >End Sub
          >
          >And during the process but before getting to the print button, the
          >PrintTheSub variable = "PrintCustomerR eports"
          >
          >How can I make the print button to accept the variable value and when
          >clicked it could call the PrintCustomerRe ports sub?
          >
          >
          >Any help would be appreciated.
          >
          >
          >

          Comment

          • Branco Medeiros

            #6
            Re: Calling Sub Procedure with a variable


            ILCSP@NETZERO.N ET wrote:
            Hi, I'm using VB.Net (2003) and I have a question. Does anyone knows
            how to call a procedure using a variable? The variable will be equal to
            the name of the procedure.
            <snip>
            Public PrintTheSub as string
            <snip>
            Private Sub PrintOrdersRepo rts ()
            ' the code for this sub goes here
            End Sub
            >
            Private Sub PrintCustomerRe ports()
            ' the code for this sub goes here
            End Sub
            >
            Private Sub PrintMonthlyRep ort()
            ' the code for this sub goes here
            End Sub
            <snip>
            Private Sub btnPrint_Click( ByVal sender As System.Object, ByVal e As
            System.EventArg s) Handles btnPrint.Click
            ' here will go the variable that will call one of the 3 subs.
            PrintTheSub
            End Sub
            <snip>
            How can I make the print button to accept the variable value and when
            clicked it could call the PrintCustomerRe ports sub?
            I guess that you're best bet, as suggested, is using a delegate instead
            of the method name:

            Delegate Sub PrintReportDele gate()
            Dim PrintTheSub As PrintReportDele gate

            Private Sub btnPrint_Click( ByVal sender As System.Object, _
            ByVal e As System.EventArg s) Handles btnPrint.Click

            If Not PrintTheSub Is Nothing Then PrintTheSub.Inv oke()

            End Sub

            And then, instead of assigning a sub name to PrintTheSub, you'd have:

            PrintTheSub = AddressOf PrintOrdersRepo rts

            or

            PrintTheSub = AddressOf PrintCustomerRe ports

            and so on.

            HTH.

            Regards,

            Branco.

            Comment

            • Al Reid

              #7
              Re: Calling Sub Procedure with a variable

              Agreed. Had you stated the number of reports, I would not have made that
              suggestion. It seems that CallByName or reflection would be more
              appropriate.

              --
              Al Reid

              <ILCSP@NETZERO. NETwrote in message
              news:1152826166 .024345.75300@3 5g2000cwc.googl egroups.com...
              Hi Al, thanks for replying. I did think about a case statement, but
              since the final project will have over 200 reports, the case statement
              will not be as good as if I used the variable name.
              >
              I was hoping that application.run (PrintTheSub) was gonna work, but it
              did not.
              >
              >

              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: Calling Sub Procedure with a variable

                <ILCSP@NETZERO. NETschrieb:
                Hi, I'm using VB.Net (2003) and I have a question. Does anyone knows
                how to call a procedure using a variable? The variable will be equal to
                the name of the procedure.
                Calling a method by its name
                <URL:http://dotnet.mvps.org/dotnet/faqs/?id=callbyname& lang=en>

                --
                M S Herfried K. Wagner
                M V P <URL:http://dotnet.mvps.org/>
                V B <URL:http://classicvb.org/petition/>

                Comment

                Working...