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
  1. Home
  2. General Programming
  3. C#
  4. Pass variable between methods

Pass variable between methods

Scheduled Pinned Locked Moved C#
csharpquestionasp-netvisual-studio
10 Posts 5 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.
  • B Offline
    B Offline
    Boro_Bob
    wrote on last edited by
    #1

    Hi I am sure this is a stupid question, but I can't work it out and its driving me mad. I have a asp.net C# web page. (Visual Studio 2005) In the class declarations at the top I have declared a Public String strTeacherName; In a button click method I give the string a value protected void btnBook_Click(object sender, EventArgs e) { strTeachername = "xxx"; } In a second button click methods I try to use the variable, but it is empty protected void btnConfirm_Click(object sender, EventArgs e) { lblMessage.Text = "You booked your lesson with " + strTeacherName; } I am assuming that the second postback is clearing the value of strTeachername. What is the best way of passing the variable from one method to the other. At the moment I am using a Session variable, but that can't be the best way. Any advice greatly appreciated...

    Words fade as the meanings change, but somehow, it don't bother me.

    Q B C E 4 Replies Last reply
    0
    • B Boro_Bob

      Hi I am sure this is a stupid question, but I can't work it out and its driving me mad. I have a asp.net C# web page. (Visual Studio 2005) In the class declarations at the top I have declared a Public String strTeacherName; In a button click method I give the string a value protected void btnBook_Click(object sender, EventArgs e) { strTeachername = "xxx"; } In a second button click methods I try to use the variable, but it is empty protected void btnConfirm_Click(object sender, EventArgs e) { lblMessage.Text = "You booked your lesson with " + strTeacherName; } I am assuming that the second postback is clearing the value of strTeachername. What is the best way of passing the variable from one method to the other. At the moment I am using a Session variable, but that can't be the best way. Any advice greatly appreciated...

      Words fade as the meanings change, but somehow, it don't bother me.

      Q Offline
      Q Offline
      quiteSmart
      wrote on last edited by
      #2

      i can not see any error in the way u are using the variable. you should look on another place where you are using this variable and making it empty. Try the find utility (Ctrl + F) and type the variable name it will lead u at all the places where the variable is found. if you can provide all the code, maybe i can help more Jamil abou khalil

      C 1 Reply Last reply
      0
      • B Boro_Bob

        Hi I am sure this is a stupid question, but I can't work it out and its driving me mad. I have a asp.net C# web page. (Visual Studio 2005) In the class declarations at the top I have declared a Public String strTeacherName; In a button click method I give the string a value protected void btnBook_Click(object sender, EventArgs e) { strTeachername = "xxx"; } In a second button click methods I try to use the variable, but it is empty protected void btnConfirm_Click(object sender, EventArgs e) { lblMessage.Text = "You booked your lesson with " + strTeacherName; } I am assuming that the second postback is clearing the value of strTeachername. What is the best way of passing the variable from one method to the other. At the moment I am using a Session variable, but that can't be the best way. Any advice greatly appreciated...

        Words fade as the meanings change, but somehow, it don't bother me.

        B Offline
        B Offline
        baerten
        wrote on last edited by
        #3

        This question is better in the ASP.NET Message-Board I'm not so good in ASP.NET but i think you must it store in the session variable. Because the page is loaded all the time aat new, so all informations are cleared (re-settet) The Session-Object is all the time avaible. There you stock normally the choises of the Visitor or his entered infos ... So, why not the Teachers Name :) You can also pass the Name by sending it to your requested page via URL, for example But that isn't a good idea

        1 Reply Last reply
        0
        • B Boro_Bob

          Hi I am sure this is a stupid question, but I can't work it out and its driving me mad. I have a asp.net C# web page. (Visual Studio 2005) In the class declarations at the top I have declared a Public String strTeacherName; In a button click method I give the string a value protected void btnBook_Click(object sender, EventArgs e) { strTeachername = "xxx"; } In a second button click methods I try to use the variable, but it is empty protected void btnConfirm_Click(object sender, EventArgs e) { lblMessage.Text = "You booked your lesson with " + strTeacherName; } I am assuming that the second postback is clearing the value of strTeachername. What is the best way of passing the variable from one method to the other. At the moment I am using a Session variable, but that can't be the best way. Any advice greatly appreciated...

          Words fade as the meanings change, but somehow, it don't bother me.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Your class is regenerated every time you postback. Think of each postback as a new instance of the same class. You have several options, including session state ( lives on the server ) and viewstate ( gets encoded in the page, so goes down to the client ). Typically, I would do what you're wanting to do with a property, like this: private const string teacherName = "TeacherName"; private string strTeacherName { get { string s = ViewState[teacherName]; return string.IsNullOrEmpty(s) ? "" : s; } set { ViewState[teacherName] = value; } } I put the key for the viewstate into a constant string so that there is never a mix up between them, and use IsNullOrEmpty to not have to deal with nulls in the rest of my code ( it will be null to start with, obviously. )

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

          B 1 Reply Last reply
          0
          • Q quiteSmart

            i can not see any error in the way u are using the variable. you should look on another place where you are using this variable and making it empty. Try the find utility (Ctrl + F) and type the variable name it will lead u at all the places where the variable is found. if you can provide all the code, maybe i can help more Jamil abou khalil

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            That's because it's an ASP.NET question.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            1 Reply Last reply
            0
            • C Christian Graus

              Your class is regenerated every time you postback. Think of each postback as a new instance of the same class. You have several options, including session state ( lives on the server ) and viewstate ( gets encoded in the page, so goes down to the client ). Typically, I would do what you're wanting to do with a property, like this: private const string teacherName = "TeacherName"; private string strTeacherName { get { string s = ViewState[teacherName]; return string.IsNullOrEmpty(s) ? "" : s; } set { ViewState[teacherName] = value; } } I put the key for the viewstate into a constant string so that there is never a mix up between them, and use IsNullOrEmpty to not have to deal with nulls in the rest of my code ( it will be null to start with, obviously. )

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              B Offline
              B Offline
              Boro_Bob
              wrote on last edited by
              #6

              Thanks everyone. I thought the Postback was the problem. I will try your method Christian, thanks.

              Words fade as the meanings change, but somehow, it don't bother me.

              C 1 Reply Last reply
              0
              • B Boro_Bob

                Thanks everyone. I thought the Postback was the problem. I will try your method Christian, thanks.

                Words fade as the meanings change, but somehow, it don't bother me.

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                No worries. You should probably do some reading as well, to get a better idea of the nature of ASP.NET and how your classes are used to generate HTML.

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                B 1 Reply Last reply
                0
                • C Christian Graus

                  No worries. You should probably do some reading as well, to get a better idea of the nature of ASP.NET and how your classes are used to generate HTML.

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                  B Offline
                  B Offline
                  Boro_Bob
                  wrote on last edited by
                  #8

                  Yes. I have got a C# book which is good for describing classes and object orientation, and asp.net books which are good for other aspects, but none of them seemed to be able answer this one for me. I think they are too basic. If you have any good suggestions for a suitable book... I have an online voucher for www.ComputerManuals.co.uk to use up. :)

                  Words fade as the meanings change, but somehow, it don't bother me.

                  1 Reply Last reply
                  0
                  • B Boro_Bob

                    Hi I am sure this is a stupid question, but I can't work it out and its driving me mad. I have a asp.net C# web page. (Visual Studio 2005) In the class declarations at the top I have declared a Public String strTeacherName; In a button click method I give the string a value protected void btnBook_Click(object sender, EventArgs e) { strTeachername = "xxx"; } In a second button click methods I try to use the variable, but it is empty protected void btnConfirm_Click(object sender, EventArgs e) { lblMessage.Text = "You booked your lesson with " + strTeacherName; } I am assuming that the second postback is clearing the value of strTeachername. What is the best way of passing the variable from one method to the other. At the moment I am using a Session variable, but that can't be the best way. Any advice greatly appreciated...

                    Words fade as the meanings change, but somehow, it don't bother me.

                    E Offline
                    E Offline
                    eggsovereasy
                    wrote on last edited by
                    #9

                    Making it static will also cause the value to persist between postbacks. public static string strTeacherName;

                    B 1 Reply Last reply
                    0
                    • E eggsovereasy

                      Making it static will also cause the value to persist between postbacks. public static string strTeacherName;

                      B Offline
                      B Offline
                      Boro_Bob
                      wrote on last edited by
                      #10

                      That works! Thanks very much. I tried Christian's method but I was just getting a stack overflow exception caused by the Set contructor, for some reason. I still have much to learn. Thank you all for you help.

                      Words fade as the meanings change, but somehow, it don't bother me.

                      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