CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2009
    Posts
    2

    Unhappy hw to define global variable in c#

    like i want 2 define a string variable wich hs value(text) as label1.text (for eg word=label1.text ) so i want 2 declare word as global variable which i able 2 use in any ASPX page..... plz help me.....

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: hw to define global variable in c#

    You can use Session State.
    try to pick a book or read some tutorials about C# and asp.net.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  3. #3
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: hw to define global variable in c#

    create a public class, name it Globals, and use it....like so....

    Code:
    public class Globals
    {
         public static string SomeText = "some text";
    }
    
    
    protected void Page_Load()
    {
         label1.Text = Globals.SomeText;
    }

  4. #4
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: hw to define global variable in c#

    Quote Originally Posted by eclipsed4utoo View Post
    create a public class, name it Globals, and use it....like so....

    Code:
    public class Globals
    {
         public static string SomeText = "some text";
    }
    
    
    protected void Page_Load()
    {
         label1.Text = Globals.SomeText;
    }
    Note that using this technique, the variable is shared between all sessions. this can lead to concurrency issues. Different users will be reading/writing to the same variable.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  5. #5
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: hw to define global variable in c#

    if it's just static text(which it sounds like what he wants), it should be fine. Maybe create a property and only do the "get"?

    Code:
    public class Globals
    {
        private static string m_someText = "some text";
    
        public static string SomeText
        {
            get { return Globals.m_someText; }
        }
    }

  6. #6
    Join Date
    Feb 2009
    Posts
    2

    Re: hw to define global variable in c#

    Quote Originally Posted by hspc View Post
    Note that using this technique, the variable is shared between all sessions. this can lead to concurrency issues. Different users will be reading/writing to the same variable.
    hi...
    i understand create a global class bt problem is where to create that class

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: hw to define global variable in c#

    Quote Originally Posted by madhuripokharkar View Post
    hi...
    i understand create a global class bt problem is where to create that class
    If it really should be global do i in the App_Code file.
    If you need it only global in one specific session I would create it at the first time it is used and store it in the Session state using
    Code:
    string sometext;
    Session.Add("MyVariable", sometext);
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured