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.
  • 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