We help IT Professionals succeed at work.

global variable vs passing variable

scm0sml
scm0sml asked
on
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.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT

Commented:
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

Author

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

does that change things?
CERTIFIED EXPERT

Commented:
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?

Author

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.....
CERTIFIED EXPERT
Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
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

Author

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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.