0

I want to declare variables globaly to use in my entire program like a connectionString. I have tried the following but no luck. In new to programming and I've googled but no luck, or at least what I could see.

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
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
user3588641
  • 21
  • 1
  • 5

1 Answers1

1

you probably want to use a Shared variable in a class.

Create a new class called Globals

Public Class Globals
    Public Shared conn = New SqlConnection(ConfigurationSettings.AppSettings(("ConnectionStringdev")))
End Class

you can then use this conn anywhere in you code without having to create an instance of the class:

Sub GetImage()
   Globals.conn.open
End Sub
Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143