我想全局声明变量,以便在整个程序中使用,就像connectionString一样。我试过以下方法,但运气不好。我刚开始编程,我在谷歌上搜索过,但运气不好,或者至少我能看到什么。
Public Class Service1
Public StrTempDir As String = Nothing
Public conn As SqlConnection = Nothing
Sub Getdata()
Public conn As New SqlConnection(ConfigurationSettings.AppSettings(("ConnectionStringdev")))
Public IntNumberOfPhotos As Integer = Int32.Parse(ConfigurationSettings.AppSettings("numberofPhotos"))
Public StrTempDir As String = ConfigurationSettings.AppSettings("tempDir")
End Sub
Sub GetImage()
'<-- use connection string here?
conn.open
End Sub
End Class
您可能希望在类中使用共享变量。
创建新类Globals
Public Class Globals
Public Shared conn = New SqlConnection(ConfigurationSettings.AppSettings(("ConnectionStringdev")))
End Class
你可以在代码的任何地方使用这个conn
,而不必创建一个类的实例:
Sub GetImage()
Globals.conn.open
End Sub