getter和setter无法正常工作的问题 [英] problem with getter and setter not working

查看:77
本文介绍了getter和setter无法正常工作的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我希望这是一个简单的...使用VWD 2005.当我打电话给我的访问者

方法(getName)我总是收到一个空字符串。调试

显示应该有一些东西,但我无法弄清楚

问题实际发生在哪里。


第一对后面的代码行:


Partial Class _Default

继承System.Web.UI.Page


Dim sName As String''for getter and setter


这里是我的getter和setter:


函数getName()

返回sName

结束函数


Sub setName(ByVal s As String)

sName = s

结束子


这里是调用setter的代码:


受保护的Sub Button2_Click(ByVal sender As对象,ByVal e As

System.EventArgs)处理Button2.Click

尝试

setName(Me.GridView1.SelectedRow.Cells(2) .Text)

Catch nre As NullReferenceException

MsgBox(请选择一行。)

结束尝试

End Sub


Lastl y,这里有一些调用getter的代码:


受保护的Sub Button4_Click(ByVal sender As Object,ByVal e As

System.EventArgs)Handles Button4.Click

MsgBox(getName())

结束子

单击Button4时,弹出消息框但是有没有什么在

它...我在哪里错了?


谢谢!

Hi all,

I hope this is an easy one... Using VWD 2005. When I call my accessor
method (getName) I always receive an empty string back. Debugging
shows there should be something there but I cannot figure out where the
problem is actually occurring.

The first couple of lines of my code behind:

Partial Class _Default
Inherits System.Web.UI.Page

Dim sName As String '' for getter and setter

Here''s my getter and setter:

Function getName()
Return sName
End Function

Sub setName(ByVal s As String)
sName = s
End Sub

Here''s the code which calls the setter:

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button2.Click
Try
setName(Me.GridView1.SelectedRow.Cells(2).Text)
Catch nre As NullReferenceException
MsgBox("Please select a row.")
End Try
End Sub

Lastly, here''s some code which calls the getter:

Protected Sub Button4_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button4.Click
MsgBox(getName())
End Sub
When Button4 is clicked, the message box pops up but there''s nothing in
it... where am I going wrong here?

Thanks!

推荐答案

你得到这个的原因是因为一旦你点击了

button4,sName的值就不再存在了,因为帖子后面有

发生了。你想做的是这样的:


私人字符串SetName

{

set {ViewState [" sName" ] = value;}

}


私人字符串GetName

{

get {return ViewState [" sName"]。ToString();}

}


另请注意,我使用的是getter。和设定者因为它们是打算使用的
。它们应该是属性,而不是方法。

the reason that you are getting this is because once you have clicked
button4, the value of sName is no longer there because a post back has
occured. what you want to do is something like this:

private string SetName
{
set{ViewState["sName"] = value;}
}

private string GetName
{
get{return ViewState["sName"].ToString();}
}

Notice also that I am using the "getter" and "setter" as they are
intended to be used. they should be properties, not methods.


C#和VB.net ...不要真正使用java setter / getters


在VB中,你做了一个


公共财产LastName

设置

Me.m_lastName =价值

结束套装

获取

返回Me.m_lastName

结束获取

结束财产


其中m_lastName是成员变量。


是的,东西消失在PostBack上,你必须知道在Page_Load中调用的对象

,单击按钮时没有。


我有一个智能"我的博客上的对象持有者:

http://spaces.msn .com / sholliday / 2005年10月24日


如果你想要花哨的话应该会有所帮助。

或者学习如何使用物品在会话[mykeyname]格式中。


" Adam Sandler" <共**** @ excite.com>在消息中写道

news:11 ********************** @ g10g2000cwb.googlegr oups.com ...
C# and VB.net... don''t really go with the java setters/getters

In VB, you do a

Public Property LastName
Set
Me.m_lastName = Value
End Set
Get
return Me.m_lastName
End Get
End Property

where m_lastName is a member variable.

Yeah, stuff "disappears" on the PostBack, you have to be aware of objects
called in Page_Load, aren''t around on a button click.

I have a "smart" object holder at my blog:

http://spaces.msn.com/sholliday/ 10/24/2005

that should help if you want to go fancy.
Or learn how to perist items in the Session["mykeyname"] format.

"Adam Sandler" <co****@excite.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
大家好,

我希望这是一个简单的...使用VWD 2005.当我调用我的访问器
方法(getName)时,我总是收到一个空字符串。调试
表明应该有一些东西,但我无法弄清楚
问题实际发生在哪里。

我的代码的前几行:

Partial Class _Default
继承System.Web.UI.Page

Dim sName As String''for getter and setter

这里是我的吸气剂和setter:

函数getName()
返回sName
结束函数

子setName(ByVal s As String)
sName = s
End Sub

这里是调用setter的代码:

受保护的Sub Button2_Click(ByVal sender As Object,ByVal e As
System .EventArgs)处理Button2.Click
尝试
setName(Me.GridView1.SelectedRow.Cells(2).Text)
Catch nre As NullReferenceException
MsgBox(" Please select a排。结束尝试
结束子

最后,这里有一些co de调用getter:

受保护的Sub Button4_Click(ByVal sender As Object,ByVal e As
System.EventArgs)处理Button4.Click
MsgBox(getName())
当点击Button4时,会弹出消息框但是
中没有任何内容......我在哪里错了?

谢谢!
Hi all,

I hope this is an easy one... Using VWD 2005. When I call my accessor
method (getName) I always receive an empty string back. Debugging
shows there should be something there but I cannot figure out where the
problem is actually occurring.

The first couple of lines of my code behind:

Partial Class _Default
Inherits System.Web.UI.Page

Dim sName As String '' for getter and setter

Here''s my getter and setter:

Function getName()
Return sName
End Function

Sub setName(ByVal s As String)
sName = s
End Sub

Here''s the code which calls the setter:

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button2.Click
Try
setName(Me.GridView1.SelectedRow.Cells(2).Text)
Catch nre As NullReferenceException
MsgBox("Please select a row.")
End Try
End Sub

Lastly, here''s some code which calls the getter:

Protected Sub Button4_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button4.Click
MsgBox(getName())
End Sub
When Button4 is clicked, the message box pops up but there''s nothing in
it... where am I going wrong here?

Thanks!



我只会使用viewstate。这样你就不必创建你不需要的变量

,并且你没有在

会话中携带不必要的数据。如果你只在一个页面上使用这个值...使用viewstate,

如果你需要在其他页面上,那么你可以使用session。我发布的代码
上面的
是在c#中。翻译相当容易。

i would just use viewstate. that way you dont have to create variables
that you dont need, and you''re not carrying around unescessary data in
session. if you''re using this value on only one page...use viewstate,
if you need it on other pages, then you can use session. code i posted
above is in c#. translation is fairly easy.


这篇关于getter和setter无法正常工作的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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