I have the following code:-
IOrderedDictionary rowValues = new IOrderedDictionary();
rowValues = GetValues(GridView1.Rows[i]);
CheckBox cbRemove = new CheckBox();
cbRemove = (CheckBox)GridView1.Rows[i].FindControl("Remove");
My question is why do I need to create a instance\object of the IOrderedDictionary and CheckBox classes?
I changed the code to
My question is why do I need to create a instance\object of the IOrderedDictionary and CheckBox classes?
I changed the code to
IOrderedDictionary rowValues;
rowValues = GetValues(GridView1.Rows[i]);
CheckBox cbRemove;
cbRemove = (CheckBox)GridView1.Rows[i].FindControl("Remove");
And found that it still works at both compile and runtime?
Regards
Steven
And found that it still works at both compile and runtime?
Regards
Steven

VulpesRank #55 · 29.2K reputationPosted Jun 6, 2012, 2:49 PM EDT
The changes you've made are fine. The RHS of the assignment statements produces an object which implements IOrderedDictionary and a CheckBox object. The variables, rowValues and cbRemove, then have respective references to these objects which is what you want.
Guest UserRank #4,868 · 321 reputationPosted Jun 7, 2012, 12:07 AM EDT
What is RHS please?
In future would I only need to instantiate if I need access to the Members of the class ie Properties, Methods etc?
Regards
Steven
VulpesRank #55 · 29.2K reputationPosted Jun 7, 2012, 12:13 AM EDT
In general, you only need to instantiate an object if you're not otherwise getting one by invoking a method or property.
For example this is OK:
MyClass mc = GetMyClassObject();
but this is pointless:
MyClass mc = new MyClass(); // superfluous object created here
mc = GetMyClassObject();
All this does is create a superfluous object and assign it to the variable, mc. When mc is then assigned a different object, there are no longer any references to the previous object and it then becomes eligible for garbage collection without ever being used!
Guest UserRank #4,868 · 321 reputationPosted Jun 7, 2012, 12:17 AM EDT