VB2010: Calling another sub procedure
Hi guys!
The code below actually works but I have a question about if there's another possible way to do this.
Code:
Imports System.Data.SqlClient
Imports Microsoft.Office.Interop
Public Class clsPrint
Dim oXLApp As Excel.Application
Dim oXLBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
Public Sub print()
oXLApp = CreateObject("Excel.Application")
oXLApp.Visible = True
oXLBook = oXLApp.Workbooks.Add
oSheet = oXLBook.ActiveSheet
End Sub
Public Sub pManagement()
'Some codes here
End Sub
Public Sub pUser()
'Some codes here
End Sub
End Class
The above code is just another normal class. Somehow, my colleague's code works for some reason:
Code:
Private Sub butPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butPrint.Click
Dim printMe As New clsPrint
printMe.print()
printMe.pManagement()
End Sub
My question is that is it possible to create a function that goes through the "Public Sub print()" and add some parameters there to call the other sub procedures like the "pManagement" or the "pUser" so that the code in the button shortens one line like this:
Code:
Private Sub butPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butPrint.Click
Dim printMe As New clsPrint
printMe.print(printme.pManagement)
End Sub
I tried looking at the parameter's list but I can't seem to find an idea what kind of parameter I am looking for.
Re: VB2010: Calling another sub procedure
Hi,
All you need to do here is to expand your Class to include a Parameter in your print method signature and then call the method passing an argument defining whether to call an additional internal method or not. i.e:-
Class expansion of the print method:-
Code:
Public Class clsPrint
Dim oXLApp As Excel.Application
Dim oXLBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
Public Sub print(Optional ByVal call_pManagement As Boolean = False)
oXLApp = CreateObject("Excel.Application")
oXLApp.Visible = True
oXLBook = oXLApp.Workbooks.Add
oSheet = oXLBook.ActiveSheet
If call_pManagement Then
pManagement()
End If
End Sub
Public Sub pManagement()
'Some codes here
End Sub
Public Sub pUser()
'Some codes here
End Sub
End Class
Call the print method as follows:-
Code:
Dim printMe As New clsPrint
printMe.print(True)
Hope that helps.
Cheers,
Ian
Re: VB2010: Calling another sub procedure
Thanks for the reply.
I wouldn't use the boolean in this procedure because other than the "pManagement" and the "pUser", I have other different sub procedure in the class.
I could use the "SELECT CASE" for this:
Code:
Public Class clsPrint
Dim oXLApp As Excel.Application
Dim oXLBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
Public Sub print(ByVal pOption As String)
oXLApp = CreateObject("Excel.Application")
oXLApp.Visible = True
oXLBook = oXLApp.Workbooks.Add
oSheet = oXLBook.ActiveSheet
Select Case pOption
Case "pManagement"
pManagement()
Case "pUser"
pUser()
Case Else
MsgBox("Error", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Error")
End Select
End Sub
Public Sub pManagement()
'Some codes here
End Sub
Public Sub pUser()
'Some codes here
End Sub
End Class
Thanks for the idea.