Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. variable scope

variable scope

Scheduled Pinned Locked Moved ASP.NET
csharphelptutorialasp-netcom
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AesopTurtle
    wrote on last edited by
    #1

    Hi, I have been facing the variable scope problem since I started coding in ASP.NET (C#). A variable I declare globally seems to be reset whenever it gets out of a function block. For example, I have a label control named "lblURL" and a button named "cmdShow" on a web form with the following code: public class MyClass: System.Web.UI.Page { private string _url = ""; private void Page_Load(object sender, System.EventArgs e) { this.SetUrl(); } private void SetUrl(){ _url = "http://www.myWebSite.com"; } private void cmdShow_Click(object sender, System.EventArgs e) { this.lblURL.Text = _url; } } When clicking on a button, I expect the label to display "http://www.myWebSite.com". But it turns out to be blank. I have tried debugging and found that the _url variable has been set properly in the SetUrl function. But the value is gone after I click the button (_url = "" in cmdShow_Click() function). Personally, I think it's the IsPostBack issue since I've never had this kind of problem with Windows Form programming. But I don't know how to solve it. :(( Someone please tell me how it works and how to solve it.

    KiT Never wait for a chance to come, Believe in your own potential and go get it!

    A G 2 Replies Last reply
    0
    • A AesopTurtle

      Hi, I have been facing the variable scope problem since I started coding in ASP.NET (C#). A variable I declare globally seems to be reset whenever it gets out of a function block. For example, I have a label control named "lblURL" and a button named "cmdShow" on a web form with the following code: public class MyClass: System.Web.UI.Page { private string _url = ""; private void Page_Load(object sender, System.EventArgs e) { this.SetUrl(); } private void SetUrl(){ _url = "http://www.myWebSite.com"; } private void cmdShow_Click(object sender, System.EventArgs e) { this.lblURL.Text = _url; } } When clicking on a button, I expect the label to display "http://www.myWebSite.com". But it turns out to be blank. I have tried debugging and found that the _url variable has been set properly in the SetUrl function. But the value is gone after I click the button (_url = "" in cmdShow_Click() function). Personally, I think it's the IsPostBack issue since I've never had this kind of problem with Windows Form programming. But I don't know how to solve it. :(( Someone please tell me how it works and how to solve it.

      KiT Never wait for a chance to come, Believe in your own potential and go get it!

      A Offline
      A Offline
      Arun Immanuel
      wrote on last edited by
      #2

      According to the code, it should work as you expected... This is because when you press the button, both the page load event and the button click event will be fired. The problem may be due to label box itself. Try to set the label box property at design time and see if it is displayed in the label box when you first run the application. Or try to replace _url with Session["_url"].

      Regards, Arun Kumar.A

      A 1 Reply Last reply
      0
      • A AesopTurtle

        Hi, I have been facing the variable scope problem since I started coding in ASP.NET (C#). A variable I declare globally seems to be reset whenever it gets out of a function block. For example, I have a label control named "lblURL" and a button named "cmdShow" on a web form with the following code: public class MyClass: System.Web.UI.Page { private string _url = ""; private void Page_Load(object sender, System.EventArgs e) { this.SetUrl(); } private void SetUrl(){ _url = "http://www.myWebSite.com"; } private void cmdShow_Click(object sender, System.EventArgs e) { this.lblURL.Text = _url; } } When clicking on a button, I expect the label to display "http://www.myWebSite.com". But it turns out to be blank. I have tried debugging and found that the _url variable has been set properly in the SetUrl function. But the value is gone after I click the button (_url = "" in cmdShow_Click() function). Personally, I think it's the IsPostBack issue since I've never had this kind of problem with Windows Form programming. But I don't know how to solve it. :(( Someone please tell me how it works and how to solve it.

        KiT Never wait for a chance to come, Believe in your own potential and go get it!

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        KiTsuNeKo wrote:

        A variable I declare globally seems to be reset whenever it gets out of a function block.

        There are no global variables in .NET. You have declared a member variable in your MyClass class. Furthermore, you are expecting your variable to keep it's value between postbacks, but a new page object is created for each request, so your variable doesn't even exist between requests. However, for every request the Page_Load method should be executed, so your variable should be recreated and repopulated for every request. Set a breakpoint in Page_Load and verify that it's called twice. It should be called when the page is first created, then when you click the button it should be called again when the page is recreated for the postback.

        KiTsuNeKo wrote:

        Personally, I think it's the IsPostBack issue since I've never had this kind of problem with Windows Form programming.

        Web programming is fundamentally different from windows form programming. A windows application is a state holding stand-alone client program, while a web application is a stateless client-server application.

        --- single minded; short sighted; long gone;

        A 1 Reply Last reply
        0
        • A Arun Immanuel

          According to the code, it should work as you expected... This is because when you press the button, both the page load event and the button click event will be fired. The problem may be due to label box itself. Try to set the label box property at design time and see if it is displayed in the label box when you first run the application. Or try to replace _url with Session["_url"].

          Regards, Arun Kumar.A

          A Offline
          A Offline
          AesopTurtle
          wrote on last edited by
          #4

          Using the Session variable is what I did. However, I wonder if there is any other way to work with it. Thank you very much.

          KiT Never wait for a chance to come, Believe in your own potential and go get it!

          A 1 Reply Last reply
          0
          • G Guffa

            KiTsuNeKo wrote:

            A variable I declare globally seems to be reset whenever it gets out of a function block.

            There are no global variables in .NET. You have declared a member variable in your MyClass class. Furthermore, you are expecting your variable to keep it's value between postbacks, but a new page object is created for each request, so your variable doesn't even exist between requests. However, for every request the Page_Load method should be executed, so your variable should be recreated and repopulated for every request. Set a breakpoint in Page_Load and verify that it's called twice. It should be called when the page is first created, then when you click the button it should be called again when the page is recreated for the postback.

            KiTsuNeKo wrote:

            Personally, I think it's the IsPostBack issue since I've never had this kind of problem with Windows Form programming.

            Web programming is fundamentally different from windows form programming. A windows application is a state holding stand-alone client program, while a web application is a stateless client-server application.

            --- single minded; short sighted; long gone;

            A Offline
            A Offline
            AesopTurtle
            wrote on last edited by
            #5

            Thank you very much. Currently, I am using a Session variable to hold the value. Is there any other way to work with this issue?

            KiT Never wait for a chance to come, Believe in your own potential and go get it!

            1 Reply Last reply
            0
            • A AesopTurtle

              Using the Session variable is what I did. However, I wonder if there is any other way to work with it. Thank you very much.

              KiT Never wait for a chance to come, Believe in your own potential and go get it!

              A Offline
              A Offline
              Arun Immanuel
              wrote on last edited by
              #6

              What about ViewState?

              Regards, Arun Kumar.A

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups