Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 475
  • Last Modified:

global variable vs passing variable

I have a page that is pretty complex and I have ended up having to pass a number of viariables through quite a few functions etc.

Some of those don't use the variable but a function call inside of it requires the variable to be passed in.

I'm just wondering what is the best way to go about this?

Should I be passing it right through, using a global variable (which I'm reading isn't a good idea) or are there other options?

Presumably there is a best practice for this?

Thanks in advance.
0
scm0sml
Asked:
scm0sml
  • 3
  • 3
  • 2
  • +1
2 Solutions
 
Julian HansenCommented:
Why not encapsulate the information you want to pass in a class.

This way you are only passing one variable around by reference and can access whatever data you need when you need it without having to worry about managing parameter lists.

Global - not a good idea.
0
 
AndyAinscowCommented:
The 'problem' with a global variable is that it can be accessed/modified everywhere - convenient but not safe.  When you pass as a parameter it is much clearer to track down where the value could be being modified when you study the code.  (Object Orientated Programming).

As a rule of thumb - avoid global variables like the plague.
0
 
RouchieCommented:
By global variable, do you really mean global as in 'accessible across your entire application'?

Are you aware that you can create variables that are accessible across the entire page, but not across the whole application?  Therefore they don't require Session or Cookies which use up unncessary resources.

    private int _userID {
        get {
            return ViewState("userID");
        }
        set {
            ViewState("userID") = value;
        }
    }

    void Page_Load(object sender, System.EventArgs e) {
        this._userID = 123;
    }

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
scm0smlAuthor Commented:
sorry when i said global i meant global to the page....

does that change things?
0
 
RouchieCommented:
If you're passing variables down through functions just so you don't lose them, then you need to use page variables instead like I showed.  There are times when there is no alternative if you want clean code.  I use page variables quite a bit myself.

If you can tell us more about what the page does, maybe the experts here can suggest alternative approaches?
0
 
scm0smlAuthor Commented:
The page is taking payments and uses 3d secure as well so there is quite going on.

There card and customer objects as well as other individual variables being passed down through functions.....
0
 
RouchieCommented:
Yes page variables would do here, one for each item.  Or, as julianH suggests, a class would also do.  You would set the values on the first post back, then simply read them within any function that requires it.
0
 
Julian HansenCommented:
A class would work if you are passing the data across class boundaries i.e. out of scope of the page. Otherwise setting page variables as recommended by Rouchie would probably work better - it would also mean you don't have to pass anything between function calls - each function can refer to the page data as needed when it is called.

Private int _userID {
        get {
            return ViewState("userID");
        }
        set {
            ViewState("userID") = value;
        }
    }

    void Page_Load(object sender, System.EventArgs e) {
        this._userID = 123;
        callSomeFunction()
    }
    void callsomefunction()
    {
        // page data is available here without it having to be passed to this function
        // if another function is called and is in the page class then no parameters need to 
        // be passed.
    }

Open in new window

0
 
scm0smlAuthor Commented:
right ok thanks both. Set you off on the wrong foot with my use of global!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now