Confused about property
-
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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