Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to declare a variable?
VB
Imports System.Data.OleDb
Public Class Form1
    Public constring1 As String
    constring1 = "Provider=MSDAORA.1;Password=temp;User ID=temp;Data Source=orcl1;Persist Security Info=True"

End Class
Here constring1 is showing error: Declaration expected

Please help.
Posted
Updated 24-Jan-14 0:03am
v3

You don't have your string allocation inside any method though. You want to do this:
VB
Public constring1 As String = "Provider=MSDAORA.1;Password=temp;User ID=temp;Data Source=orcl1;Persist Security Info=True"
 
Share this answer
 
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:
VB
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.
 
Share this answer
 
You might want to pickup reading from the basics - http://msdn.microsoft.com/en-us/library/aa290387%28v=vs.71%29.aspx[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900