473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Jul 13 '06 #1
7 2634

<IL***@NETZERO.NETwrote in message
news:11*********************@75g2000cwc.googlegrou ps.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
Jul 13 '06 #2

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.
Jul 13 '06 #3
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:
<IL***@NETZERO.NETwrote in message
news:11*********************@75g2000cwc.googlegrou ps.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
Jul 13 '06 #4
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:
>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.
Jul 13 '06 #5

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.

Jul 13 '06 #6
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.NETwrote in message
news:11*********************@35g2000cwc.googlegrou ps.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.


Jul 13 '06 #7
<IL***@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/>
Jul 13 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andrew Wilkinson | last post by:
Hi, First off I know that in almost all cases this would be a terrible thing to do, but this is an unusual case where this makes sense. Basically I have a procedure where you pass a string...
4
by: Mario Pranjic | last post by:
Hi! I have a scalar function that returns integer: xview (int) Now, I'm trying to build a procedure that has the following select inside: select atr1, xview(atr2) from tablename
5
by: Frank Apap | last post by:
I am trying to call a stored procedure that has an INOUT parameter from the CLP to do some testing on a UDB 8 system. Since the value is needed as input I cannot use ? for the parameter, and...
30
by: Tim Marshall | last post by:
Here's the scenario, A2003, Jet back end, illustrated with some cut down code at the end of the post: A proc dims a snapshot recordset (dim rst as Dao.recordset) and opens it. There are several...
14
by: Microsoft | last post by:
I know how to call a finction and pass a parameter to it, but I don;t understand how to take the resulting output of the function and use it from the subroutine that called the function ...
4
by: ST | last post by:
From Form1 I call a public sub in a module. The sub writes on Form1. Problem: the call creates a new Form1 and writes on it, instead of writing on the one already shown Form1. I have tried to...
1
by: UDBDBA | last post by:
Hi: How can one call SYSPROC.DB2LOAD procedure within a SQL stored procedure. I get the following error: DB21034E The command was processed as an SQL statement because it was not a valid...
7
by: zheetee | last post by:
<script type="text/vbscript"> sub setTextBoxValue(a1,a2) thedelete.deleteTextBox.value = a1 end sub </script> <td> <a href="#"...
6
Soniad
by: Soniad | last post by:
Hello, I am excecuting a stored procedure in my ASP page , it has one out parameter (@confirm) . after executing the procedure i want to retreive this out parameter and assign it to variable...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.