New to C# and trying to use a global variable

Posted by Andrew G. Johnson on Stack Overflow See other posts from Stack Overflow or by Andrew G. Johnson
Published on 2010-03-11T16:15:26Z Indexed on 2010/03/11 17:29 UTC
Read the original article Hit count: 269

Filed under:
|

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;
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET