I want to create a global function to use throughout my application. Let say it's about the connection to the database.
My code that i plan to use in my global function is:
myConnection = New SqlConnection("...........")
myConnection.Open()
So that I can call it to use in every form throughout my application. This can make me easy to edit the connection later.
Could anyone help me show how to de开发者_如何学Cfine this global function and how to call this function in the form.
Best regard,
Public NotInheritable Class Utilities
Private Sub New()
End Sub
Public Shared Function MyMethod(myParam As Object) As MyObject
'Do stuff in here
Return New MyObject()
End Function
End Class
And then to consume
Dim instance As MyObject = Utilities.MyMethod(parameterObject)
Use Module instead of class
Module ConnectionHelper
Public Function OpenConnection() As SqlConnection
Dim conn As New SqlConnection("")
conn.Open()
Return conn
End Function
End Module
Class P
Public Sub New()
Using conn = OpenConnection()
'here you can work with connection
End Using
End Sub
End Class
In class P you have showcase of prefered usage
精彩评论