Public Shared Declaration

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'd like to declare my connection string as a public shared variable, to be
used throughout my ASP. Net projected (created using VB.NET 2003).

Below is the line of code:
Public Shared conStr As String = "Provider=Microsoft.Jet.OleDb.4.0;data
source=C:\Documents and Settings\kwlehman\My Documents\Elena1.mdb"

I am getting an error at Public Shared:
"Shared is not valid on a local variable declaration"

I know that I've used Public Shared many times, yet never doing an ASP. NET
project. I am just learning ASP. NET and am not too familiar.

Any advice would be appreciated.

Thanks,
Kevin
 
Kevin said:
I'd like to declare my connection string as a public shared variable, to
be
used throughout my ASP. Net projected (created using VB.NET 2003).

Below is the line of code:
Public Shared conStr As String = "Provider=Microsoft.Jet.OleDb.4.0;data
source=C:\Documents and Settings\kwlehman\My Documents\Elena1.mdb"

I am getting an error at Public Shared:
"Shared is not valid on a local variable declaration"

I know that I've used Public Shared many times, yet never doing an ASP.
NET
project. I am just learning ASP. NET and am not too familiar.

Any advice would be appreciated.

Thanks,
Kevin

Is the conStr var inside a Module?

Mythran
 
Kevin said:
I'd like to declare my connection string as a public shared variable, to
be
used throughout my ASP. Net projected (created using VB.NET 2003).

Below is the line of code:
Public Shared conStr As String = "Provider=Microsoft.Jet.OleDb.4.0;data
source=C:\Documents and Settings\kwlehman\My Documents\Elena1.mdb"

I am getting an error at Public Shared:
"Shared is not valid on a local variable declaration"

Where did you place this code?
 
Here is the entire code block:
..................
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim conStr As String = "Provider=Microsoft.Jet.OleDb.4.0;data
source=C:\Documents and Settings\kwlehman\My Documents\Elena1.mdb"
Dim conn As OleDbConnection = New OleDbConnection(conStr)
..................
 
Kevin said:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim conStr As String = "Provider=Microsoft.Jet.OleDb.4.0;data
source=C:\Documents and Settings\kwlehman\My Documents\Elena1.mdb"
Dim conn As OleDbConnection = New OleDbConnection(conStr)

You'll have to put the declaration of the 'conStr' "variable" outside the
'Sub' procedure. I assume that the prefix 'con' indicates a constant, thus
you can use 'Const' instead of 'Dim'.
 
Kevin said:
Here is the entire code block:
.................
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim conStr As String = "Provider=Microsoft.Jet.OleDb.4.0;data
source=C:\Documents and Settings\kwlehman\My Documents\Elena1.mdb"
Dim conn As OleDbConnection = New OleDbConnection(conStr)
.................

:

You can't put Public inside of a method. Try the following:

Public conStr As String = "..."

Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) _
Handles MyBase.Load
Dim conn As OleDbConnection = New OleDbConnection(Me.conStr)
End Sub

HTH,
Mythran
 
Herfried K. Wagner said:
You'll have to put the declaration of the 'conStr' "variable" outside the
'Sub' procedure. I assume that the prefix 'con' indicates a constant,
thus you can use 'Const' instead of 'Dim'.

--

Beat me to it :P

conStr, from what I see, means connection string (conStr for short) :)

Mythran
 
Here is the comlete code block:
.................................
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim conStr As String = "Provider=Microsoft.Jet.OleDb.4.0;data
source=C:\Documents and Settings\kwlehman\My Documents\Elena1.mdb"
Dim conn As OleDbConnection = New OleDbConnection(conStr)
...................................
 
Kevin,

When you made the connection shared (in a shared class in the way you
showed) in a module you get it without that shared class name. (I prefer a
shared class. It becomes in a module in my opinion complete unfindable after
a while). note. For Herfried, my opinion. :-)

Than you get something as this.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim conn As New OleDbConnection(MySharedClass.conStr)

(For the same you can construct that connection of course as well in that
shared class. Don't forget than to open and close it everytime)

Cor
 
Did anybody understand what I wrote, I did not. :-)

If you make the connection string shared, than you can use this code.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim conn As New OleDbConnection(MySharedClass.conStr)

This assumes that you have used a shared class with the name MySharedClass.

That I prefer above a module because than you can find more easy where it is
placed.

For the same you can construct the connection of course as well in that
shared class. Don't forget than to open and close the connection as well
everytime.

I hope this helps

Cor
 
Cor,

Cor Ligthert said:
That I prefer above a module because than you can find more easy where it
is placed.

For the same you can construct the connection of course as well in that
shared class. Don't forget than to open and close the connection as well
everytime.

Note that you actually /can/ qualify a module's member by the module name if
you want to do so!
 
Herfried,
Note that you actually /can/ qualify a module's member by the module name
if you want to do so!
Probably, what is a module else than a (to often quick and dirty written)
shared class.

:-)

Although that it is in an asking way written, an answer is not needed.
However if you want, feel free.

:-)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top