You can declare variables at the class scope, but you cannot actually
do anything at that level. So you do what Pete said, and make the assignment in the declaration.
BUT... it is generally a bad practice to declare public variables at the class scope. If this is a constant -- it is available for use but will never be changed -- add the
Const
keyword, like so:
Public Const constring1 As String = "Provider=MSDAORA.1;Password=temp;User ID=temp;Data Source=orcl1;Persist Security Info=True"
Constants are kept in a separate section of memory where they are not subjected to things like garbage collection. This will also protect the value from being tampered with, either deliberately or accidentally.
Const
also adds an implicit
Shared
to the declaration, meaning you would access it by using the class name --
Form1.constring1
-- rather than by using an instance.
Since you are declaring this on a form, which presumably will be
not be used as a class, why make it
Public
at all? A good rule of thumb in Object Oriented Programming is to expose
only those elements that need to be exposed. If the string will be used only internally, declare it to be
Private
, or
Protected
if you will be using your form as a base class for other forms.