Link to home
Start Free TrialLog in
Avatar of Meps
MepsFlag for United States of America

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?
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;
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kelevra
Kelevra

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Meps

ASKER

Was just going to use this object for a few pages, so I didn't want to place in the gobal.

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.
Avatar of Kelevra
Kelevra

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:


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!
}

Open in new window

Avatar of Meps

ASKER

Thank you for the assist.  good to know I don't have to instanciate the object every time.