Thursday, March 22, 2012

Working with variables…

Hi,

I am trying to build a simple web page with ASP.NET but running into a problem with retaining value assigned to variables in different events. I have the this simple code:

________________________________________
Public Class WebForm1
Public var1 As String
Public var2 As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

var1 = "diff"

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

var2 = var1

End Sub
________________________________________

What happing here is that, I am assigning a value "diff" to variable "var1" when I click on command button1 and I am trying to retrieve that value assigned to "var1" to "var2" when I click on command button2.

I thought it should be straightforward but when I click command button2, it assigns nothing to "var2" and if I look at the value of "var1" in debugger, it lost the value I assigned to it during command button1 click event.

Would someone please explain what I am missing here?

Appreciate your help.

Thanks,
NSFirst you have to click button 1 to get the value assigned. Then when you click button 2 you're causing a postback which means the page doesn't remember anything from before so you lose the value you assigned when you clicked button 1.

You're doing 2 trips to the server and it doesn't remember what you did on the first trip.

So if you start learning about session or application variables your problem can be solved.
That's right - doing postback will kill the variables.

You could make them global by putting them all in a module, or, like suggested make them session variables.


Public Class WebForm1
Public var1 As String
Public var2 As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Session("var1") = "diff"

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Session("var2") = Session("var1")

End Sub

If you click Button1 then click Button2 you should have Session("var2") being equal to "diff".

Another option is to use a textbox to store diff and then pass the contents of that to var2...

Andrew
Hi,
Thanks for your feedback. My actual problem is that I am creating an object when clicked on button1 and I would like to do some work with that object when I click on button2.

For example:

Public Class WebForm1
Public Obj as Object

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Obj = Server.CreateObject (someobject)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Obj.someevent

End Sub

This is where I had the problem. Since I was losing the Obj value I could not access any events associated with Obj in button2 click event.

Instead of doing Obj = server.createobject(someobject), if I do
session("Obj") = server.createobject(someobject) will I be able to work with the created object in the button2 click event? Or is declaring the Obj variable in a module (to make it global) is the best way to do this.

Thanks again for your help.

NS
Hi Nagesh

using session variable or define it in the global file, both works fine. But which one is used it depends upon your requirment. If u want this object is available to all the users accessing this application u have to define it in the module or global file. But if u want this object values will change user to user u have to use session varible.

Thanx

Amit
Hi,
Thanks for your help. I started to work with modules. I declared a variable in the module as "public obj as object" and I was able to use it. I was able to retain the value assigned to this variable and use it in different events.
However, what's happening now is that, if two users are using this application, if user A creates this object first, and when user B clicks on button1 (which creates the object), it's not creating any new object for user B. In fact it is trying to use the object created by user A.

But, what I need is to create separate objects for each user. I understand I need to use session variables to accomplish this, but I not sure how to go about it.

For example, I have this code:

Public Class WebForm1
Public Obj as Object

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Session("Obj") = server.createobject("someobject")

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Session("Obj.values(1,2)")

End Sub

Clicking on button1 will create this object and when they click on button2, I want use one of the methods of this object and pass the variables. But, if I try it as shown above I am getting a compilation error.

Thanks again for your help.

NS

0 comments:

Post a Comment