[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Proper way to call and use C# variables?

Posted on 2009-07-08
4
Medium Priority
?
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
Kelevra earned 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

656 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