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. Web Development
  3. ASP.NET
  4. Variable Declaration

Variable Declaration

Scheduled Pinned Locked Moved ASP.NET
helptutorialquestion
19 Posts 6 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.
  • P ps srinivasan

    Hello frnd I declared one variable(like dim num as integer=0),if i click the button1 i want to assign 1(num=1) if i click button2 i want to assign 2(num=2),but every time if i click the button the value is assigned properly but immedialy leaving that function again that variable value is zero,how to solve this problem?

    A Offline
    A Offline
    Abhijit Jana
    wrote on last edited by
    #3

    use static variable for that :)

    Best Regards ----------------- Abhijit Jana View My CodeProject Articles "Success is Journey it's not a destination"

    C P G 3 Replies Last reply
    0
    • P ps srinivasan

      Hello frnd I declared one variable(like dim num as integer=0),if i click the button1 i want to assign 1(num=1) if i click button2 i want to assign 2(num=2),but every time if i click the button the value is assigned properly but immedialy leaving that function again that variable value is zero,how to solve this problem?

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #4

      Problem is CG said, adding to his post, you can declare the variable inside the class not inside the function. Variables declared inside the function will be having scope only in the function.

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

      G 1 Reply Last reply
      0
      • A Abhijit Jana

        use static variable for that :)

        Best Regards ----------------- Abhijit Jana View My CodeProject Articles "Success is Journey it's not a destination"

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

        Just to be clear, that does not solve the problem, but if the problem was the value being different between class instances, this would be the solution.

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        1 Reply Last reply
        0
        • C Christian Graus

          Probably you are declaring the variable inside the functions. If you do that, a local variable is created which hides the member variable. The only code you want in the function is num = 4. If you have dim num as integer in the method, that's your problem.

          Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          P Offline
          P Offline
          ps srinivasan
          wrote on last edited by
          #6

          hi frnd i didn't declare the variable inside function,i declared that variable ,variable declaration part.

          1 Reply Last reply
          0
          • A Abhijit Jana

            use static variable for that :)

            Best Regards ----------------- Abhijit Jana View My CodeProject Articles "Success is Journey it's not a destination"

            P Offline
            P Offline
            ps srinivasan
            wrote on last edited by
            #7

            I declared the static variable in declaration part like " static num as int16=10" but is showing error,if i declare that variable inside function means that not showing error.how i want to declare static variable in declaration part.

            N 1 Reply Last reply
            0
            • P ps srinivasan

              I declared the static variable in declaration part like " static num as int16=10" but is showing error,if i declare that variable inside function means that not showing error.how i want to declare static variable in declaration part.

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #8

              ps.srinivasan wrote:

              declaration part.

              What is this declaration part ? A static variable in VB.NET can be declared like

              Static Myvar As Integer = 1

              I don't think that declaring variable into static won't solve the problem. Where you declared the variable now ?

              All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

              P 1 Reply Last reply
              0
              • N N a v a n e e t h

                ps.srinivasan wrote:

                declaration part.

                What is this declaration part ? A static variable in VB.NET can be declared like

                Static Myvar As Integer = 1

                I don't think that declaring variable into static won't solve the problem. Where you declared the variable now ?

                All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

                P Offline
                P Offline
                ps srinivasan
                wrote on last edited by
                #9

                N a v a n e e t h wrote:

                Static Myvar As Integer = 1

                I declared the variable like that.

                N a v a n e e t h wrote:

                I don't think that declaring variable into static won't solve the problem.

                so how i want to solve that problem.my question is simple if i click different button i want to assign different value for variable. Where i want declare that variable?,how i want to declare that varible?

                N 1 Reply Last reply
                0
                • P ps srinivasan

                  N a v a n e e t h wrote:

                  Static Myvar As Integer = 1

                  I declared the variable like that.

                  N a v a n e e t h wrote:

                  I don't think that declaring variable into static won't solve the problem.

                  so how i want to solve that problem.my question is simple if i click different button i want to assign different value for variable. Where i want declare that variable?,how i want to declare that varible?

                  N Offline
                  N Offline
                  N a v a n e e t h
                  wrote on last edited by
                  #10

                  ps.srinivasan wrote:

                  Where i want declare that variable?,how i want to declare that varible?

                  You need to declare your variable inside the form.

                  Public Class Form1
                  Inherits System.Windows.Forms.Form

                  Dim Flag As Integer = 1
                  
                  Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                      Flag = 1
                  End Sub
                  
                  Private Sub Button2\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                      Flag = 2
                  End Sub
                  

                  End Class

                  All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

                  P 1 Reply Last reply
                  0
                  • P ps srinivasan

                    Hello frnd I declared one variable(like dim num as integer=0),if i click the button1 i want to assign 1(num=1) if i click button2 i want to assign 2(num=2),but every time if i click the button the value is assigned properly but immedialy leaving that function again that variable value is zero,how to solve this problem?

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

                    A page object only lives while the response is created on the server. Once the response has been created and sent to the browser, the page object goes away. If you declare a variable in the page object, it will only live as long as the page object lives. Once the response is sent to the browser and the page object goes away, the variable doesn't exist any more. When you press the button, a new request is sent to the server, a new page object is created to handle the request, and the new page object contains a new instance of the variable that is assigned the initial value of zero. Web pages are stateless. If you want to keep a value between requests, you have to either send the value back and forth between the server and the browser (using a cookie, viewstate or a hidden field), or assign it to a session variable.

                    Experience is the sum of all the mistakes you have done.

                    1 Reply Last reply
                    0
                    • N N a v a n e e t h

                      ps.srinivasan wrote:

                      Where i want declare that variable?,how i want to declare that varible?

                      You need to declare your variable inside the form.

                      Public Class Form1
                      Inherits System.Windows.Forms.Form

                      Dim Flag As Integer = 1
                      
                      Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                          Flag = 1
                      End Sub
                      
                      Private Sub Button2\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                          Flag = 2
                      End Sub
                      

                      End Class

                      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

                      P Offline
                      P Offline
                      ps srinivasan
                      wrote on last edited by
                      #12

                      N a v a n e e t h wrote:

                      Public Class Form1 Inherits System.Windows.Forms.Form

                      sorry frnd my question for web from not window form Public Class WebForm2 Inherits System.Web.UI.Page Dim Myvar As Int16 = 0'this code is executing when ever i click the button. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Myvar = 1 End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Myvar = 2 End Sub End Class

                      N 1 Reply Last reply
                      0
                      • A Abhijit Jana

                        use static variable for that :)

                        Best Regards ----------------- Abhijit Jana View My CodeProject Articles "Success is Journey it's not a destination"

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

                        Abhijit Jana wrote:

                        use static variable for that

                        Static variables can't be used that way in a web application. Every user would share the same variable. Unless you have data that should be shared between all users, static variables are useless in a web application.

                        Experience is the sum of all the mistakes you have done.

                        1 Reply Last reply
                        0
                        • N N a v a n e e t h

                          Problem is CG said, adding to his post, you can declare the variable inside the class not inside the function. Variables declared inside the function will be having scope only in the function.

                          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

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

                          Making the variable an instance member doesn't help a bit, as the instance of the Page class doesn't survive between postbacks.

                          Experience is the sum of all the mistakes you have done.

                          N 1 Reply Last reply
                          0
                          • G Guffa

                            Making the variable an instance member doesn't help a bit, as the instance of the Page class doesn't survive between postbacks.

                            Experience is the sum of all the mistakes you have done.

                            N Offline
                            N Offline
                            N a v a n e e t h
                            wrote on last edited by
                            #15

                            Guffa wrote:

                            Making the variable an instance member doesn't help a bit, as the instance of the Page class doesn't survive between postbacks.

                            Yes you are right. But he didn't explained that he is doing this in webforms. I thought it's in winforms.

                            All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

                            G 1 Reply Last reply
                            0
                            • P ps srinivasan

                              N a v a n e e t h wrote:

                              Public Class Form1 Inherits System.Windows.Forms.Form

                              sorry frnd my question for web from not window form Public Class WebForm2 Inherits System.Web.UI.Page Dim Myvar As Int16 = 0'this code is executing when ever i click the button. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Myvar = 1 End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Myvar = 2 End Sub End Class

                              N Offline
                              N Offline
                              N a v a n e e t h
                              wrote on last edited by
                              #16

                              ps.srinivasan wrote:

                              sorry frnd my question for web from not window form

                              Why don't you tell this before ? :mad: As Guffa told, HTTP is stateless and it can't keep objects/values on postbacks. You can assign the value to a viewstate/cookies/session.

                              All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

                              1 Reply Last reply
                              0
                              • N N a v a n e e t h

                                Guffa wrote:

                                Making the variable an instance member doesn't help a bit, as the instance of the Page class doesn't survive between postbacks.

                                Yes you are right. But he didn't explained that he is doing this in webforms. I thought it's in winforms.

                                All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

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

                                N a v a n e e t h wrote:

                                But he didn't explained that he is doing this in webforms.

                                Well, he posted in the ASP.NET forum...

                                Experience is the sum of all the mistakes you have done.

                                N 1 Reply Last reply
                                0
                                • G Guffa

                                  N a v a n e e t h wrote:

                                  But he didn't explained that he is doing this in webforms.

                                  Well, he posted in the ASP.NET forum...

                                  Experience is the sum of all the mistakes you have done.

                                  N Offline
                                  N Offline
                                  N a v a n e e t h
                                  wrote on last edited by
                                  #18

                                  Guffa wrote:

                                  Well, he posted in the ASP.NET forum...

                                  :sigh: Grin

                                  All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

                                  1 Reply Last reply
                                  0
                                  • P ps srinivasan

                                    Hello frnd I declared one variable(like dim num as integer=0),if i click the button1 i want to assign 1(num=1) if i click button2 i want to assign 2(num=2),but every time if i click the button the value is assigned properly but immedialy leaving that function again that variable value is zero,how to solve this problem?

                                    A Offline
                                    A Offline
                                    aswini
                                    wrote on last edited by
                                    #19

                                    Hi... If u want to keep total count of some thing... use static variables...

                                    Aswini

                                    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