Variable Declaration
-
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?
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.FormDim 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
-
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 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.
-
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.FormDim 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
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 -
use static variable for that :)
Best Regards ----------------- Abhijit Jana View My CodeProject Articles "Success is Journey it's not a destination"
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.
-
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
-
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.
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
-
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 Classps.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
-
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
-
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.
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
-
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?