Public Shared Declaration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin

    #1

    Public Shared Declaration

    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=Micro soft.Jet.OleDb. 4.0;data
    source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.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
  • Mythran

    #2
    Re: Public Shared Declaration


    "Kevin" <Kevin@discussi ons.microsoft.c om> wrote in message
    news:050A0977-B019-4E4C-A449-328BF9125D45@mi crosoft.com...[color=blue]
    > 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=Micro soft.Jet.OleDb. 4.0;data
    > source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.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[/color]

    Is the conStr var inside a Module?

    Mythran

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Public Shared Declaration

      "Kevin" <Kevin@discussi ons.microsoft.c om> schrieb:[color=blue]
      > 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=Micro soft.Jet.OleDb. 4.0;data
      > source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.mdb"
      >
      > I am getting an error at Public Shared:
      > "Shared is not valid on a local variable declaration"[/color]

      Where did you place this code?

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://classicvb.org/petition/>

      Comment

      • Kevin

        #4
        Re: Public Shared Declaration

        Here is the entire code block:
        ............... ...
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
        System.EventArg s) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim conStr As String = "Provider=Micro soft.Jet.OleDb. 4.0;data
        source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.mdb"
        Dim conn As OleDbConnection = New OleDbConnection (conStr)
        ............... ...

        "Mythran" wrote:
        [color=blue]
        >
        > "Kevin" <Kevin@discussi ons.microsoft.c om> wrote in message
        > news:050A0977-B019-4E4C-A449-328BF9125D45@mi crosoft.com...[color=green]
        > > 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=Micro soft.Jet.OleDb. 4.0;data
        > > source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.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[/color]
        >
        > Is the conStr var inside a Module?
        >
        > Mythran
        >
        >[/color]

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Public Shared Declaration

          "Kevin" <Kevin@discussi ons.microsoft.c om> schrieb:[color=blue]
          > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
          > System.EventArg s) Handles MyBase.Load
          > 'Put user code to initialize the page here
          > Dim conStr As String = "Provider=Micro soft.Jet.OleDb. 4.0;data
          > source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.mdb"
          > Dim conn As OleDbConnection = New OleDbConnection (conStr)[/color]

          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'.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • Mythran

            #6
            Re: Public Shared Declaration


            "Kevin" <Kevin@discussi ons.microsoft.c om> wrote in message
            news:99804F9A-B170-454D-BE10-ACDD2CC9E207@mi crosoft.com...[color=blue]
            > Here is the entire code block:
            > ............... ..
            > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
            > System.EventArg s) Handles MyBase.Load
            > 'Put user code to initialize the page here
            > Dim conStr As String = "Provider=Micro soft.Jet.OleDb. 4.0;data
            > source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.mdb"
            > Dim conn As OleDbConnection = New OleDbConnection (conStr)
            > ............... ..
            >
            > "Mythran" wrote:
            >[/color]

            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

            Comment

            • Mythran

              #7
              Re: Public Shared Declaration


              "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
              news:O$ozUYndFH A.3932@TK2MSFTN GP12.phx.gbl...[color=blue]
              > "Kevin" <Kevin@discussi ons.microsoft.c om> schrieb:[color=green]
              >> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
              >> System.EventArg s) Handles MyBase.Load
              >> 'Put user code to initialize the page here
              >> Dim conStr As String = "Provider=Micro soft.Jet.OleDb. 4.0;data
              >> source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.mdb"
              >> Dim conn As OleDbConnection = New OleDbConnection (conStr)[/color]
              >
              > 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'.
              >
              > --[/color]

              Beat me to it :P

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

              Mythran


              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: Public Shared Declaration

                "Mythran" <kip_potter@hot mail.comREMOVET RAIL> schrieb:[color=blue]
                > conStr, from what I see, means connection string (conStr for short) :)[/color]

                Ooops :-).

                --
                M S Herfried K. Wagner
                M V P <URL:http://dotnet.mvps.org/>
                V B <URL:http://classicvb.org/petition/>

                Comment

                • Kevin

                  #9
                  Re: Public Shared Declaration

                  Here is the comlete code block:
                  ............... ............... ...
                  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
                  System.EventArg s) Handles MyBase.Load
                  'Put user code to initialize the page here
                  Dim conStr As String = "Provider=Micro soft.Jet.OleDb. 4.0;data
                  source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.mdb"
                  Dim conn As OleDbConnection = New OleDbConnection (conStr)
                  ............... ............... .....
                  "Herfried K. Wagner [MVP]" wrote:
                  [color=blue]
                  > "Kevin" <Kevin@discussi ons.microsoft.c om> schrieb:[color=green]
                  > > 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=Micro soft.Jet.OleDb. 4.0;data
                  > > source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.mdb"
                  > >
                  > > I am getting an error at Public Shared:
                  > > "Shared is not valid on a local variable declaration"[/color]
                  >
                  > Where did you place this code?
                  >
                  > --
                  > M S Herfried K. Wagner
                  > M V P <URL:http://dotnet.mvps.org/>
                  > V B <URL:http://classicvb.org/petition/>
                  >
                  >[/color]

                  Comment

                  • Herfried K. Wagner [MVP]

                    #10
                    Re: Public Shared Declaration

                    "Kevin" <Kevin@discussi ons.microsoft.c om> schrieb:[color=blue]
                    > 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=Micro soft.Jet.OleDb. 4.0;data
                    > source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.mdb"
                    >
                    > I am getting an error at Public Shared:
                    > "Shared is not valid on a local variable declaration"[/color]

                    Where did you place this code?

                    --
                    M S Herfried K. Wagner
                    M V P <URL:http://dotnet.mvps.org/>
                    V B <URL:http://classicvb.org/petition/>

                    Comment

                    • Kevin

                      #11
                      Re: Public Shared Declaration

                      Here is the entire code block:
                      ............... ...
                      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
                      System.EventArg s) Handles MyBase.Load
                      'Put user code to initialize the page here
                      Dim conStr As String = "Provider=Micro soft.Jet.OleDb. 4.0;data
                      source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.mdb"
                      Dim conn As OleDbConnection = New OleDbConnection (conStr)
                      ............... ...

                      "Mythran" wrote:
                      [color=blue]
                      >
                      > "Kevin" <Kevin@discussi ons.microsoft.c om> wrote in message
                      > news:050A0977-B019-4E4C-A449-328BF9125D45@mi crosoft.com...[color=green]
                      > > 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=Micro soft.Jet.OleDb. 4.0;data
                      > > source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.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[/color]
                      >
                      > Is the conStr var inside a Module?
                      >
                      > Mythran
                      >
                      >[/color]

                      Comment

                      • Cor Ligthert

                        #12
                        Re: Public Shared Declaration

                        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.
                        [color=blue]
                        > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
                        > System.EventArg s) Handles MyBase.Load[/color]
                        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



                        Comment

                        • Herfried K. Wagner [MVP]

                          #13
                          Re: Public Shared Declaration

                          "Kevin" <Kevin@discussi ons.microsoft.c om> schrieb:[color=blue]
                          > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
                          > System.EventArg s) Handles MyBase.Load
                          > 'Put user code to initialize the page here
                          > Dim conStr As String = "Provider=Micro soft.Jet.OleDb. 4.0;data
                          > source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.mdb"
                          > Dim conn As OleDbConnection = New OleDbConnection (conStr)[/color]

                          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'.

                          --
                          M S Herfried K. Wagner
                          M V P <URL:http://dotnet.mvps.org/>
                          V B <URL:http://classicvb.org/petition/>

                          Comment

                          • Mythran

                            #14
                            Re: Public Shared Declaration


                            "Kevin" <Kevin@discussi ons.microsoft.c om> wrote in message
                            news:99804F9A-B170-454D-BE10-ACDD2CC9E207@mi crosoft.com...[color=blue]
                            > Here is the entire code block:
                            > ............... ..
                            > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
                            > System.EventArg s) Handles MyBase.Load
                            > 'Put user code to initialize the page here
                            > Dim conStr As String = "Provider=Micro soft.Jet.OleDb. 4.0;data
                            > source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.mdb"
                            > Dim conn As OleDbConnection = New OleDbConnection (conStr)
                            > ............... ..
                            >
                            > "Mythran" wrote:
                            >[/color]

                            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

                            Comment

                            • Mythran

                              #15
                              Re: Public Shared Declaration


                              "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                              news:O$ozUYndFH A.3932@TK2MSFTN GP12.phx.gbl...[color=blue]
                              > "Kevin" <Kevin@discussi ons.microsoft.c om> schrieb:[color=green]
                              >> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
                              >> System.EventArg s) Handles MyBase.Load
                              >> 'Put user code to initialize the page here
                              >> Dim conStr As String = "Provider=Micro soft.Jet.OleDb. 4.0;data
                              >> source=C:\Docum ents and Settings\kwlehm an\My Documents\Elena 1.mdb"
                              >> Dim conn As OleDbConnection = New OleDbConnection (conStr)[/color]
                              >
                              > 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'.
                              >
                              > --[/color]

                              Beat me to it :P

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

                              Mythran


                              Comment

                              Working...