Solved

Proper way to call and use C# variables?

Posted on 2009-07-08
4
249 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:Meps
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
Kelevra earned 100 total points
ID: 24806532
Hi,

If there is a global variable you want to use in the entire site you could use the global.asax file!

Otherwise to answer your question you can store objects in session there are no problems in doing so!
protected void Page_Load(object sender, EventArgs e)

    {

        if (Session["AuditAnswer"] == null)

        {

            AnswerMainDTO AuditAnswer = new AnswerMainDTO();
 
 

            Session["AuditAnswer"] = AuditAnswer;

        }

 

    }

    protected void SelectProduct()

    {

        AnswerMainDTO AuditAnswer = (maybe do a cast here) Session["AuditAnswer"];
 

        AuditAnswer.Product = 1;
 

        Session["AuditAnswer"] = AuditAnswer;

    }

Open in new window

0
 
LVL 3

Author Comment

by:Meps
ID: 24806571
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.
0
 
LVL 3

Expert Comment

by:Kelevra
ID: 24806708
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

0
 
LVL 3

Author Closing Comment

by:Meps
ID: 31601245
Thank you for the assist.  good to know I don't have to instanciate the object every time.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS ReportViewer report timeout 7 104
Why Does This Page Not Show Up? 4 24
Excel file not created as expected 7 50
Code works but it's slow 24 44
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now