变量不递增 [英] Variable not incrementing

查看:55
本文介绍了变量不递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在使用Visual Web Developer并在类

部分中删除了一个公共变量。我在加载部分分配了一个值,并且在一个按钮上我设置它为
最多递增,但它总是提供相同的值,1。当我增加

时从按钮变量,不应该保持新值?我也试图将b变量称为朋友,但是它不再友好了。

;)


部分类测试


继承System.Web.UI.Page


Public i As Integer


受保护的子Page_Load(ByVal sender As Object,ByVal e As System.EventArgs)

Handles Me.Load


i = 0

>
End Sub


受保护的子Button2_Click(ByVal发送者为对象,ByVal e As

System.EventArgs)处理Button2.Click


i = i + 1


MsgBox(i,MsgBoxStyle.Information)


End Sub


结束类

Hello,

I am using Visual Web Developer and decared a Public Variable in the Class
section. I assigned a value in the load section, and on a button I set it
up to increment, but it always delivers the same value, 1. When I increment
the variable from the button, shouldn''t it hold the new value? I also
tried declaring the variable as a Friend, but it wasn''t any more friendly.
;)

Partial Class test

Inherits System.Web.UI.Page

Public i As Integer

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

i = 0

End Sub

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

i = i + 1

MsgBox(i, MsgBoxStyle.Information)

End Sub

End Class

推荐答案

HTTP是无状态的。这意味着在每次向服务器发出请求时,都会创建一个新的Page $
对象。因为它始终是一个新对象,无论你在旧对象上设置什么值,b / b都不会持久。


但是,即使一切都按照您的想象运行由于页面对象始终存在一个

实例,因此您的代码仍然无法按预计运行
。这是因为您总是在Page_Load中将i设置为0。所以

在用户点击按钮后,我将设置为0,然后在点击处理程序中增加1

。然后在下一次点击时会发生同样的事情,

我总是在1.


但是,即使你修改你的代码只设置在Page_Load中它为0时

它不是回发(或者只是没有做任何事情,因为0是

整数的默认值),你的代码会不能按照你的预期工作

在帖子开头给出。


我建议你买一本关于ASP.NET的书并做一些阅读关于它如何全部

在继续前进之前有效。如果不了解页面

生命周期和ASP.NET的基本机制,就很难走得太远。


" Mark A. Sam" < MS ** @ Plan-It-Earth.Net>在消息中写道

news:uj ************** @ TK2MSFTNGP09.phx.gbl ...
HTTP is stateless. This means on every request to the server, a new Page
object is created. Since it is always a new object, whatever values you set
on the old object, will not persist.

However, even if everything worked as you imagine with there being one
instance of a Page object all the time, your code would still not run as
expected. This is because you are always setting i to 0 in Page_Load. So
after the user clicks the button, i would be set to 0, then incremented by 1
in the click handler. Then on the next click the same thing would happen,
and i would always be at 1.

However, even if you fix your code to only set it to 0 in Page_Load when
it''s not a postback (or just not do anything, since 0 is the default for an
integer anyway), your code would not work as you expect for the reasons
given in the beginning of the post.

I recommend you get a book on ASP.NET and do some reading about how it all
works before proceeding much further. Without an understand of the page
lifecycle and the basic mechanics of ASP.NET it will be hard to get far.

"Mark A. Sam" <ms**@Plan-It-Earth.Net> wrote in message
news:uj**************@TK2MSFTNGP09.phx.gbl...
你好,
我正在使用Visual Web Developer并在类
部分中删除了一个公共变量。我在加载部分分配了一个值,并且在一个按钮上我设置了
直到递增,但它总是提供相同的值,1。当我从按钮增加变量时,不应该''它是否拥有新价值?
我也尝试将变量声明为朋友,但它不再友好了。 ;)

部分类测试

继承System.Web.UI.Page

Public i As Integer

受保护Sub Page_Load(ByVal sender As Object,ByVal e As
System.EventArgs)Handles Me.Load

我= 0

End Sub
System.EventArgs)处理Button2.Click

i = i + 1

MsgBox(i ,MsgBoxStyle.Information)

End Sub

End Class
Hello,

I am using Visual Web Developer and decared a Public Variable in the Class
section. I assigned a value in the load section, and on a button I set it
up to increment, but it always delivers the same value, 1. When I
increment the variable from the button, shouldn''t it hold the new value?
I also tried declaring the variable as a Friend, but it wasn''t any more
friendly. ;)

Partial Class test

Inherits System.Web.UI.Page

Public i As Integer

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

i = 0

End Sub

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

i = i + 1

MsgBox(i, MsgBoxStyle.Information)

End Sub

End Class



但是你必须记住,在网络的客户端/服务器模型中,每次请求此网页时,都会创建此类的全新实例并且

您的变量是从头开始再次创建。第二次使用页面时,您不会继续使用相同的页面类实例。


您必须将变量存储在更持久的位置,例如一个

cookie,缓存,viewstate或数据库,可以在

页面调用中重复使用它。


Mark A. Sam < MS ** @ Plan-It-Earth.Net>在消息中写道

news:uj ************** @ TK2MSFTNGP09.phx.gbl ...
But you must remember that in the client/server model of the web, each time
you request this web page, a BRAND NEW instance of this class is created and
your variable is created again, from scratch. You aren''t continuing with
the same page class instance the second time you use the page.

You must store your variable in a more persistant location, such as a
cookie, the cache, viewstate or a database to be able to reuse it across
page calls.

"Mark A. Sam" <ms**@Plan-It-Earth.Net> wrote in message
news:uj**************@TK2MSFTNGP09.phx.gbl...
你好,
我正在使用Visual Web Developer并在类
部分中删除了一个公共变量。我在加载部分分配了一个值,并且在一个按钮上我设置了
直到递增,但它总是提供相同的值,1。当我从按钮增加变量时,不应该''它是否拥有新价值?
我也尝试将变量声明为朋友,但它不再友好了。 ;)

部分类测试

继承System.Web.UI.Page

Public i As Integer

受保护Sub Page_Load(ByVal sender As Object,ByVal e As
System.EventArgs)Handles Me.Load

我= 0

End Sub
System.EventArgs)处理Button2.Click

i = i + 1

MsgBox(i ,MsgBoxStyle.Information)

End Sub

End Class
Hello,

I am using Visual Web Developer and decared a Public Variable in the Class
section. I assigned a value in the load section, and on a button I set it
up to increment, but it always delivers the same value, 1. When I
increment the variable from the button, shouldn''t it hold the new value?
I also tried declaring the variable as a Friend, but it wasn''t any more
friendly. ;)

Partial Class test

Inherits System.Web.UI.Page

Public i As Integer

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

i = 0

End Sub

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

i = i + 1

MsgBox(i, MsgBoxStyle.Information)

End Sub

End Class



谢谢码头。你不明白发生了什么事是对的。

这就是为什么我不跟随。我使用

相同的按钮在同一表单上放置了一个文本框,并且能够增加文本框中的值。

代码如下:

部分类测试

继承System.Web.UI.Page


Public i As Integer


受保护的子页面_Load(ByVal发送者为对象,ByVal e As System.EventArgs)

处理我.Load


''i = 0


End Sub


受保护的子按钮2_Click(ByVal发送者为对象,ByVal e作为

System.EventArgs)处理Button2.Click


''i = i + 1


'' MsgBox(i,MsgBoxStyle.Information)


TextBox1.Text = TextBox1.Text + 1


End Sub

结束类

如果每次单击按钮页面都刷新,那么为什么

文本框没有丢失它的值?也许我没有意义,但它是我的看法。

;)


上帝保佑,


Mark


Marina Levit [MVP]" <所以***** @ nospam.com>在消息中写道

news:eL ************** @ TK2MSFTNGP12.phx.gbl ...
Thank you Marina. You are right about not understanding what is happening.
Here is why I am not following. I placed a textbox on the same form using
the same button, and was able to increment the value in the textbox. The
code is below:
Partial Class test
Inherits System.Web.UI.Page

Public i As Integer

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

''i = 0

End Sub

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

''i = i + 1

''MsgBox(i, MsgBoxStyle.Information)

TextBox1.Text = TextBox1.Text + 1

End Sub

End Class
If the page refreshes each time I click the button, then why doesn''t the
textbox lose its value? Maybe I''m not making sense, but its how I see it.
;)

God Bless,

Mark

"Marina Levit [MVP]" <so*****@nospam.com> wrote in message
news:eL**************@TK2MSFTNGP12.phx.gbl...
HTTP是无状态的。这意味着在每次向服务器发出请求时,都会创建一个新的Page
对象。因为它总是一个新的对象,无论你在旧对象上设置什么值,都不会持久。

然而,即使一切都像你想象的那样有一个
一直是Page对象的实例,你的代码仍然无法按预期运行。这是因为您总是在Page_Load中将i设置为0。所以
在用户点击按钮后,我将被设置为0,然后在点击处理程序中增加
1。然后在下一次单击时会发生相同的事情,并且我将始终处于1.

但是,即使您将代码修改为仅在Page_Load中将其设置为0时/>它不是回发(或者只是没有做任何事情,因为0是默认的
一个整数),你的代码将无法正常工作
在开始时给出帖子。

我建议你买一本关于ASP.NET的书,并在继续学习之前先阅读一下有关它们的工作原理。如果不了解页面的生命周期和ASP.NET的基本机制,就很难实现。

Mark A. Sam < MS ** @ Plan-It-Earth.Net>在消息中写道
新闻:uj ************** @ TK2MSFTNGP09.phx.gbl ...
HTTP is stateless. This means on every request to the server, a new Page
object is created. Since it is always a new object, whatever values you
set on the old object, will not persist.

However, even if everything worked as you imagine with there being one
instance of a Page object all the time, your code would still not run as
expected. This is because you are always setting i to 0 in Page_Load. So
after the user clicks the button, i would be set to 0, then incremented by
1 in the click handler. Then on the next click the same thing would
happen, and i would always be at 1.

However, even if you fix your code to only set it to 0 in Page_Load when
it''s not a postback (or just not do anything, since 0 is the default for
an integer anyway), your code would not work as you expect for the reasons
given in the beginning of the post.

I recommend you get a book on ASP.NET and do some reading about how it all
works before proceeding much further. Without an understand of the page
lifecycle and the basic mechanics of ASP.NET it will be hard to get far.

"Mark A. Sam" <ms**@Plan-It-Earth.Net> wrote in message
news:uj**************@TK2MSFTNGP09.phx.gbl...
你好,
我正在使用Visual Web Developer并在
Class部分中删除了一个公共变量。我在加载部分分配了一个值,并在一个按钮上将它设置为递增,但它总是提供相同的值,1。当我从按钮增加变量时,不应该''它是否拥有新价值?
我也尝试将变量声明为朋友,但它不再友好了。 ;)

部分类测试

继承System.Web.UI.Page

Public i As Integer

受保护Sub Page_Load(ByVal sender As Object,ByVal e As
System.EventArgs)Handles Me.Load

我= 0

End Sub
System.EventArgs)处理Button2.Click

i = i + 1

MsgBox(i ,MsgBoxStyle.Information)

End Sub

End Class
Hello,

I am using Visual Web Developer and decared a Public Variable in the
Class section. I assigned a value in the load section, and on a button I
set it up to increment, but it always delivers the same value, 1. When I
increment the variable from the button, shouldn''t it hold the new value?
I also tried declaring the variable as a Friend, but it wasn''t any more
friendly. ;)

Partial Class test

Inherits System.Web.UI.Page

Public i As Integer

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

i = 0

End Sub

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

i = i + 1

MsgBox(i, MsgBoxStyle.Information)

End Sub

End Class




这篇关于变量不递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆