Home All Groups Group Topic Archive Search About

Calling Sub Procedure with a variable

Author
13 Jul 2006 9:10 PM
ILCSP
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 PrintOrdersReports ()
   ' the code for this sub goes here
End Sub

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

Private Sub PrintMonthlyReport()
   ' 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.EventArgs) 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 = "PrintCustomerReports"

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


Any help would be appreciated.

Author
13 Jul 2006 9:20 PM
Al Reid
<IL***@NETZERO.NET> wrote in message
Show quoteHide quote
news:1152825059.565444.53240@75g2000cwc.googlegroups.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 PrintOrdersReports ()
>   ' the code for this sub goes here
> End Sub
>
> Private Sub PrintCustomerReports()
>   ' the code for this sub goes here
> End Sub
>
> Private Sub PrintMonthlyReport()
>   ' 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.EventArgs) 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 = "PrintCustomerReports"
>
> How can I make the print button to accept the variable value and when
> clicked it could call the PrintCustomerReports 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.EventArgs) Handles
btnPrint.Click
   Select Case PrintTheSub
      Case "PrintCustomerReports"
            PrintCustomerReports()

      Case "PrintOrdersReports"
            PrintOrdersReports()

      Case "PrintMonthlyReport"
            PrintMonthlyReport()
   End Select

End Sub


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

--
Al Reid
Author
13 Jul 2006 9:29 PM
ILCSP
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:
Show quoteHide quote
> <IL***@NETZERO.NET> wrote in message
> news:1152825059.565444.53240@75g2000cwc.googlegroups.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 PrintOrdersReports ()
> >   ' the code for this sub goes here
> > End Sub
> >
> > Private Sub PrintCustomerReports()
> >   ' the code for this sub goes here
> > End Sub
> >
> > Private Sub PrintMonthlyReport()
> >   ' 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.EventArgs) 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 = "PrintCustomerReports"
> >
> > How can I make the print button to accept the variable value and when
> > clicked it could call the PrintCustomerReports 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.EventArgs) Handles
> btnPrint.Click
>    Select Case PrintTheSub
>       Case "PrintCustomerReports"
>             PrintCustomerReports()
>
>       Case "PrintOrdersReports"
>             PrintOrdersReports()
>
>       Case "PrintMonthlyReport"
>             PrintMonthlyReport()
>    End Select
>
>  End Sub
>
>
> I would probably make an Enum for the reports and use that instead of a
> string.
>
> --
> Al Reid
Author
13 Jul 2006 10:46 PM
Al Reid
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

<IL***@NETZERO.NET> wrote in message
Show quoteHide quote
news:1152826166.024345.75300@35g2000cwc.googlegroups.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.
>
>
Author
13 Jul 2006 9:21 PM
Mattias Sjögren
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.
Author
13 Jul 2006 9:35 PM
tomb
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(byref 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


IL***@NETZERO.NET wrote:

Show quoteHide quote
>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 PrintOrdersReports ()
>   ' the code for this sub goes here
>End Sub
>
>Private Sub PrintCustomerReports()
>   ' the code for this sub goes here
>End Sub
>
>Private Sub PrintMonthlyReport()
>   ' 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.EventArgs) 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 = "PrintCustomerReports"
>
>How can I make the print button to accept the variable value and when
>clicked it could call the PrintCustomerReports sub?
>
>
>Any help would be appreciated.
>

>
Author
13 Jul 2006 10:22 PM
Branco Medeiros
IL***@NETZERO.NET 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 PrintOrdersReports ()
>    ' the code for this sub goes here
> End Sub
>
> Private Sub PrintCustomerReports()
>    ' the code for this sub goes here
> End Sub
>
> Private Sub PrintMonthlyReport()
>    ' the code for this sub goes here
> End Sub
<snip>
>  Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) 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 PrintCustomerReports sub?

I guess that you're best bet, as suggested, is using a delegate instead
of the method name:

  Delegate Sub PrintReportDelegate()
  Dim PrintTheSub As PrintReportDelegate

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

    If Not PrintTheSub Is Nothing Then PrintTheSub.Invoke()

End Sub

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

  PrintTheSub  = AddressOf  PrintOrdersReports

or

  PrintTheSub  = AddressOf  PrintCustomerReports

and so on.

HTH.

Regards,

Branco.
Author
13 Jul 2006 10:59 PM
Herfried K. Wagner [MVP]
<IL***@NETZERO.NET> schrieb:
> 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/>