Properties Vs Constructor Parameters- A Closer Analysis

We all know that we can set the properties of the ADO.NET objects by 2 ways.. By passing parameters to the constructors. Consider the following code snippet that creates a new SqlConnecti

ITPro Today

May 20, 2004

2 Min Read
ITPro Today logo in a gray background | ITPro Today

We all know that we can set the properties of the ADO.NET objects by 2 ways.. By passing parameters to the constructors. Consider the following code snippet that creates a newSqlConnection object  and sets the connection string by directly specifying the value of the property.

 

Dim objSqlCon as new System.Data.SqlClient.SqlConnection()


objSqlCon.ConnectionString = "Data source=localhost;Database=Northwind;User ID=sa;Pwd="

 

Above task can be performed by passing the connection string as a parameter to the constructor, see the code below..

 

Dim objSqlCon as new System.Data.SqlClient.SqlConnection()


objSqlCon.ConnectionString = "Data source=localhost;Database=Northwind;User ID=sa;Pwd="
 

Practically I feel there is no significant difference in the performance of both the approaches and the choice depends on the perception of every individual.

 

By setting the properties directly by specifying values makes code readable and debugging simple when compared with passing parameters to the constructors.

 

In some cases passing parameters to a constructor may provide an advantage to our application. In the case of DataView object, setting the values of the RowFilter and RowStateFilter properties directly results in creating an Index at least twice while creating the object and when the properties are set.

 

In this scenario pass the values for the properties, as parameters to the constructors is the profitable approach.

 

Happy Learning !!! 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like