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
VulpesPosted Jun 6, 2012, 6:49 PM
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 UserPosted Jun 7, 2012, 4:17 AM
VulpesPosted Jun 7, 2012, 4:13 AM
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 UserPosted Jun 7, 2012, 4:07 AM
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