Question Maintaining Data Between Button Clicks

njsokalski

Well-known member
Joined
Mar 16, 2011
Messages
102
Programming Experience
5-10
I should probably be kicked out of the development field for not knowing this, but I'll ask it anyway. I have a Form with multiple Buttons, all of which modify the same variable. However, each time the variable gets reset. Here is what my code looks like:
Public Class frmTest
   Private myvar As New MyClass()

   Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
      'Code to modify myvar
   End Sub
   Private Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
      'Code to modify myvar
   End Sub
End Class


However, every time I click one of the buttons, myvar gets reinitialized before the Click eventhandler's code is executed, almost as if the first line of both eventhandlers was:
myvar=New MyClass()
I want myvar to maintain it's value between events. How can I do this, or in what Class do I need to define it so that it only gets initialized once? I'm sure this is a very simple question for people that have their experience in Windows Forms, but even though I have been doing VB.NET for almost 10 years, most of my experience is in ASP.NET and Silverlight, so I just need to get caught up in Windows Forms. Thanks.
 
Last edited by a moderator:
Barring some sort of corruption in your app or on your system, your variable is not being reset unless you reset it. There's nothing in what you've posted that suggests what the problem is or even that there's a problem. Please describe in detail exactly what you're doing, which would include the code in those two event handlers, exactly what you expect to happen and exactly what does happen.
 
I should probably be kicked out of the development field for not knowing this, but I'll ask it anyway. I have a Form with multiple Buttons, all of which modify the same variable. However, each time the variable gets reset. Here is what my code looks like:
Public Class frmTest
   Private myvar As New MyClass()

   Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
      'Code to modify myvar
   End Sub
   Private Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
      'Code to modify myvar
   End Sub
End Class


However, every time I click one of the buttons, myvar gets reinitialized before the Click eventhandler's code is executed, almost as if the first line of both eventhandlers was:
myvar=New MyClass()
I want myvar to maintain it's value between events. How can I do this, or in what Class do I need to define it so that it only gets initialized once? I'm sure this is a very simple question for people that have their experience in Windows Forms, but even though I have been doing VB.NET for almost 10 years, most of my experience is in ASP.NET and Silverlight, so I just need to get caught up in Windows Forms. Thanks.
What exactly are you doing to modify the "myvar" variable? It's good you posted code that shows us you have the variable as a class level variable so it is accessible in both the buttons, but without knowing what you're doing with it in each of the button's click events we have no way of answering your question here.
Also one of the main differences between winforms and webforms is that you don't have to take extra steps for the program to "remember" variables values because webforms are stateless.
 
I found the problem (and as always, it was a shoot yourself in the head problem). The variable wasn't really being reset, but I was calling a method in MyClass that calculates what values it started with, so it looked to me like it was being reset because it ended up with the values it would have if it had been reset. So, I guess I can say problem solved!
 
Back
Top