3

Is it possible to use global variables in C#? I'm coming from mainly a PHP background so variables are either accessible everywhere or just a global definition away.

My main issue is I have a User class that I built myself to wrap around the current users table on my company's database. I am defining it in the MasterPage but can't seem to access it from the actual pages (I don't know if there's a better word to describe them but they are the pages that inherit the styles and format from the MasterPage)

Any general tips or implementation practices for me?

EDIT: here's some code snippets of what I'm trying to do:

Site.master.cs

public partial class SiteMaster : System.Web.UI.MasterPage
{
    public User user = new User();
}

logout.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="logout.aspx.cs" Inherits="logout" %>
<%@ MasterType  virtualPath="~/Site.master"%>

logout.aspx.cs

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        User user = Master.user;
    }
}
Andrew G. Johnson
  • 26,603
  • 30
  • 91
  • 135
  • 1
    It's possible per different recommnedations (and warnings) below. If you do choose to use globals I suggest doing so just to get moving forward but refactoring them out as soon as possible. – Paul Sasik Mar 11 '10 at 16:21
  • Don't think your problem is global variables or not. Rather how to get to data in your masterpage from your page instance. – Peter Lillevold Mar 11 '10 at 16:32

7 Answers7

4

No, it is impossible. It is possible to create singletons or public static classes, but this is bad practice.

C# was designed for object oriented programming. If you haven't written programs using object oriented paradigm before it can be a bit hard to switch to it in the beginning. OOP (http://en.wikipedia.org/wiki/Object-oriented_programming) is built on three main concepts: inheritance, polymorphism and encapsulation.

You can defined classed apart of the pages/masterpages, it is good practice to define them in the App_Code folder.

Andrew Bezzub
  • 15,744
  • 7
  • 51
  • 73
  • 1
    Just to add to @Andrew Bezzub's answer - public static classes are usually used to declare global consts, not to keep global state. If you need global state, use a singleton. – Franci Penov Mar 11 '10 at 16:21
2

Have a public static class and declare public static member variables.

That's what I do when I need some globals, though I try to avoid using them when I can.

bobber205
  • 12,948
  • 27
  • 74
  • 100
2

If Page is inheriting from MasterPage, then make User property protected in MasterPage and it will be visible to Page.

Kimi
  • 13,621
  • 9
  • 55
  • 84
2

The Master page class can be accessible to the pages that use it by setting the MasterPageClass in your .aspx page like so:

<%@ MasterType TypeName="MyTypeName" VirtualPath="~/MasterPageName.master" %>

It sounds to me like you may just need to put your code in a slightly different place. A typical User class would be accessible to your project through a stand-alone class, and not bundled into a master page or a master type.

I might suggest that you add your User class into a new classfile in the /AppCode directory of your project instead, (User.cs). That would let you have access to it from your pages without having to muck with the MasterType.

Tj Kellie
  • 6,336
  • 2
  • 31
  • 40
  • Yes I have User.cs in /App_Code (vs suggested I put it there so I thought WHY THE HELL NOT) -- but I need to define it somewhere (I thought in the MasterPage) as `User current_user = new User();` and then try to get at `current_user` from all the actual pages – Andrew G. Johnson Mar 11 '10 at 17:12
1

See my answer to this question. Non-static class-level variables do no persist once the response is sent to the browser. This is because each Page object is going to be a new instance of the class, not the same one from your last request.

Use the "Session" property instead as I show in the link.

Community
  • 1
  • 1
Mike Marshall
  • 7,788
  • 4
  • 39
  • 63
1

Is your problem, from your page, get to data stored in the masterpage (assuming we're talking about the ASP.Net MasterPage mechanism here)?

If so, you should look at strongly-typed access to masterpages. Basically, what you do is create a public property in your MasterPage class. Then, in your Page, declare the MasterPageFile and MasterType, like this:

public partial class MasterPage
{
    public User CurrentUser{...}
}

In your page aspx, declare to use the masterpage and which master type to use.

<%@ Page  masterPageFile="~/MasterPage.master"%>
<%@ MasterType  virtualPath="~/MasterPage.master"%>

You will then be able to access the property from within your page class like this:

var user = Master.CurrentUser;

Then, for the question on where to initialize the CurrentUser object, look at the list of page lifecycle events. As you can see, MasterPage.Init fires before Page.Init and MasterPage.Load fires before Page.Load. You can use either MP.Init or MP.Load to make sure the data is ready for when the page events fire, though Init is preferred.

Peter Lillevold
  • 33,668
  • 7
  • 97
  • 131
  • Peter I added a bunch of code snippets to my question, any ideas? I think you are closest to knowing what I am TRYING to do – Andrew G. Johnson Mar 11 '10 at 17:19
  • You're on the right track. But `CodeFile="logout.aspx.cs" Inherits="logout"` doesn't look right for a page called `_Default`. Cut-n-paste error? – Peter Lillevold Mar 11 '10 at 17:25
0

There are at least a couple different ways to achieve what you want:

  1. Use the Application object - It can be used to store things globally and is part of ASP.Net.

  2. Use static classes - This is another option for creating a singleton.

JB King
  • 11,860
  • 4
  • 38
  • 49