• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • 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
 
AndyAinscowFreelance programmer / ConsultantCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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