Meps
asked on
Proper way to call and use C# variables?
Having a bad day today and need some expert advice.
Am I even doing this right? It seems kinda reduntant.
What I intend on doing is using the object throughout the pages, so I put it in a session.
But each time I want to access the object I got to create another one and inject it with the session object?
Am I even doing this right? It seems kinda reduntant.
What I intend on doing is using the object throughout the pages, so I put it in a session.
But each time I want to access the object I got to create another one and inject it with the session object?
protected void Page_Load(object sender, EventArgs e)
{
if (Session["AuditAnswer"] == null)
{
AnswerMainDTO AuditAnswer = new AnswerMainDTO();
Session["AuditAnswer"] = AuditAnswer;
}
}
protected void SelectProduct()
{
AnswerMainDTO AuditAnswer = new AnswerMainDTO();
AuditAnswer = (AnswerMainDTO)Session["AuditAnswer"];
AuditAnswer.Product = 1;
Session["AuditAnswer"] = AuditAnswer;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well, you see the object you instanciate (by the new word) is visible only in your current page.
You can pass it through to other classes files or function by placing it into argument.
like:
You can pass it through to other classes files or function by placing it into argument.
like:
MyObject obj = new MyObject();
obj.this = "that";
//call a function using the object without having to instanciate it again!
MyFunction (obj)
here if you have a function:
public void MyFunction (MyObject obj)
{
//do something!
}
ASKER
Thank you for the assist. good to know I don't have to instanciate the object every time.
ASKER
I know the starting page, and the ending page that will use this object so creating and destroying the object there.
Just seemed kinda odd to keep on creating the object just to use it.