|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Calling Sub Procedure with a variablehow 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. <IL***@NETZERO.NET> wrote in message
Show quoteHide quote news:1152825059.565444.53240@75g2000cwc.googlegroups.com... How about an If...Then or a Select Case?> 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. > 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 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 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. -- Show quoteHide quoteAl Reid <IL***@NETZERO.NET> wrote in message 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. > > 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. 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. > > > IL***@NETZERO.NET wrote:
> Hi, I'm using VB.Net (2003) and I have a question. Does anyone knows <snip>> how to call a procedure using a variable? The variable will be equal to > the name of the procedure. > Public PrintTheSub as string <snip>> Private Sub PrintOrdersReports () <snip>> ' 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 > Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As <snip>> System.EventArgs) Handles btnPrint.Click > ' here will go the variable that will call one of the 3 subs. > PrintTheSub > End Sub > How can I make the print button to accept the variable value and when I guess that you're best bet, as suggested, is using a delegate instead> clicked it could call the PrintCustomerReports sub? 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. <IL***@NETZERO.NET> schrieb:
> Hi, I'm using VB.Net (2003) and I have a question. Does anyone knows Calling a method by its name> how to call a procedure using a variable? The variable will be equal to > the name of the procedure. <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/>
ISO-8859-1 encoding of an xml string
Deleting a Photo stored in a Picturebox is Recursive Procedures/Function what i need to solve my problem? Lauching a form from another form Lookup table in a Label consume web service via httpwebrequest Limit to range of colors displayed by Label control? How to remove an empty array ? Batch file question Transitioning to VB.NET |
|||||||||||||||||||||||