Sir, I need your help in the following topic:-
I here writing two
methods of calling a class. Please give a explanation that which is
better ,faster performable and more standard
Also tell me which method reduce size of the program?
1.Method 1:
namespace Employer
{
public partial class frmRejectionIn : Form
{
public frmRejectionIn()
{
InitializeComponent();
}
EmployeeSP SPEmployee=new EmployeeSP();// call class for entire form
}
2. Method 2:
private void btnSave_Click(object sender, EventArgs e)
{
EmployeeSP SPEmployee=new EmployeeSP(); // call class within the function
}
private void btnDelete_Click(object sender, EventArgs e)
{
EmployeeSP SPEmployee=new EmployeeSP(); // again called the class within the function
}
Loading

RobbinsonPosted Oct 21, 2013, 2:27 AM
The answer to your query would be different in context. Let me explain you advantage of both the cases in different context.
1.Method 1:
suppose you need reference to an object at muliple places in the class then method 1 would preferable. Method 1 needs slight modification. it would be better to have read only property that returns an instance of a class. please see below code to get more idea.This approach would helpful where you require an instance of class at many places. This approach would help to avoid object creation overhead which results in improved performance depends on creations logic.
private EmployeeSP _employeeSP=null;
public EmployeeSP EmployeeSP
{
if(_employeeSP==null)
{
_employeeSP=new EmployeeSP();
}
return _employeeSP;
}
1.Method 2:
Local objects are preferable when object is not useful at many places.so, that it would be created and garbaged once it goes out of scope, there by it freed up memory.
I hope, this explaination would help you get answer for your question. Feel free to connect in case of query.
Don't forget to mark as answer if this post helps you.
RobbinsonPosted Oct 21, 2013, 2:53 AM
This would not affect memory because if you use both the approach in above defined appropriate context, memory consumption would be optimum. You can write code to use optimum memory by just understanding the object life time of your application. In short no object would have reference if it is no longer useful.
Hope, this will help you out.
Don't forget to mark as answer if this post helps you.
Bineesh ViswanathPosted Oct 21, 2013, 2:36 AM