Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Confused about property

Confused about property

Scheduled Pinned Locked Moved Visual Basic
databasequestionworkspace
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ESTAN
    wrote on last edited by
    #1

    Hi, I don't know if it is a good practice what I am doing here. I have a class that is making the connections to the database. The connection string I save in the web.config. And this is the property that I used inside that class to get the connectionstring. Does it make sense?

    ''' ''' This variable holds the db connection string.
    ''' The value of the db connection string is saved in the web.config.
    ''' 
    Private Shared sConString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString
    
    Private Shared ReadOnly Property ConString() As String
    	Get
    		Return sConString
    	End Get
    End Property
    
    C 1 Reply Last reply
    0
    • E ESTAN

      Hi, I don't know if it is a good practice what I am doing here. I have a class that is making the connections to the database. The connection string I save in the web.config. And this is the property that I used inside that class to get the connectionstring. Does it make sense?

      ''' ''' This variable holds the db connection string.
      ''' The value of the db connection string is saved in the web.config.
      ''' 
      Private Shared sConString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString
      
      Private Shared ReadOnly Property ConString() As String
      	Get
      		Return sConString
      	End Get
      End Property
      
      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Making the property private is pointless as the only thing that access to the property also has access to the original string. Other than that, looks okay.

      Recent blog posts: *Method hiding Vs. overriding *Microsoft Surface *SQL Server / Visual Studio install order My Blog

      E 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Making the property private is pointless as the only thing that access to the property also has access to the original string. Other than that, looks okay.

        Recent blog posts: *Method hiding Vs. overriding *Microsoft Surface *SQL Server / Visual Studio install order My Blog

        E Offline
        E Offline
        ESTAN
        wrote on last edited by
        #3

        So, it is better that I use only Private Shared sConString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString Isn't that so that every time I ask for sConString, he will lookup the connection string in the web.config? Or I can do the following ? Private Shared sConString As String = "" Public Sub New() sConString = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString End Sub

        C J 2 Replies Last reply
        0
        • E ESTAN

          So, it is better that I use only Private Shared sConString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString Isn't that so that every time I ask for sConString, he will lookup the connection string in the web.config? Or I can do the following ? Private Shared sConString As String = "" Public Sub New() sConString = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString End Sub

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          ESTAN wrote:

          Isn't that so that every time I ask for sConString, he will lookup the connection string in the web.config?

          No. It won't. Think about what you are actually doing. You have a shared field on the class. It has no functionality, it is just a reference to an area of memory that stored the connection string.

          ESTAN wrote:

          Or I can do the following ? Private Shared sConString As String = "" Public Sub New() sConString = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString End Sub

          You can, but I don't see the point.

          Recent blog posts: *Method hiding Vs. overriding *Microsoft Surface *SQL Server / Visual Studio install order My Blog

          E 1 Reply Last reply
          0
          • E ESTAN

            So, it is better that I use only Private Shared sConString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString Isn't that so that every time I ask for sConString, he will lookup the connection string in the web.config? Or I can do the following ? Private Shared sConString As String = "" Public Sub New() sConString = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString End Sub

            J Offline
            J Offline
            Jon_Boy
            wrote on last edited by
            #5

            They both accomplish the same thing. If you have multiple constructors, then you have to reduplicate the "sConString = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString" line for each constructor. I prefer 'Private Shared sConString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString' because I am lazy. Cheers!

            Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

            1 Reply Last reply
            0
            • C Colin Angus Mackay

              ESTAN wrote:

              Isn't that so that every time I ask for sConString, he will lookup the connection string in the web.config?

              No. It won't. Think about what you are actually doing. You have a shared field on the class. It has no functionality, it is just a reference to an area of memory that stored the connection string.

              ESTAN wrote:

              Or I can do the following ? Private Shared sConString As String = "" Public Sub New() sConString = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString End Sub

              You can, but I don't see the point.

              Recent blog posts: *Method hiding Vs. overriding *Microsoft Surface *SQL Server / Visual Studio install order My Blog

              E Offline
              E Offline
              ESTAN
              wrote on last edited by
              #6

              Thanks, than I will keep only the 'reference' to this object. Private Shared sConString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ConnectionString

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups