为什么我的 ApplicationCache 传回引用而不是值?

发布于 2024-07-11 11:01:30 字数 1716 浏览 3 评论 0原文

这是我刚刚遇到的奇怪的事情。

我有一个 Web 应用程序,在 ApplicationCache 中存储了一个小型 DataTable,以减少单独的查询量,因为数据是一个不经常更改的查找表。

我在给定页面内访问此数据表两次。 一次将数据绑定到我的 Page_Load 方法中的下拉列表:

dtDeptDivAct = GetAllDeptDivActCodes()
dtDeptDivAct.DefaultView.Sort = "LongDescription ASC"
ddlDeptDivAccount.DataSource = dtDeptDivAct.DefaultView
ddlDeptDivAccount.DataTextField = "LongDescription"
ddlDeptDivAccount.DataValueField = "Id"
ddlDeptDivAccount.DataBind()

...一次在我的 ddlDeptDivAct_SelectedIndexChanged 事件中选择索引时从表中检索其他数据:

Dim dtDeptDivAct As DeptDivActDataTable

If ddlDeptDivAccount.SelectedIndex > 0 Then

   dtDeptDivAct = GetAllDeptDivActCodes()
   dtDeptDivAct.DefaultView.RowFilter = "Id = " & ddlDeptDivAccount.SelectedValue

   txtAddFundingDept.Text = DirectCast(dtDeptDivAct.DefaultView(0).Row, DeptDivActRow).Department.ToString.PadLeft(2, Char.Parse("0"))
   txtAddFundingDiv.Text = DirectCast(dtDeptDivAct.DefaultView(0).Row, DeptDivActRow).Division.ToString.PadLeft(2, Char.Parse("0"))
   txtAddFundingAct.Text = DirectCast(dtDeptDivAct.DefaultView(0).Row, DeptDivActRow).Activity.ToString.PadLeft(3, Char.Parse("0"))

Else

   txtAddFundingDept.Text = ""
   txtAddFundingDiv.Text = ""
   txtAddFundingAct.Text = ""

End If

注意:GetAllDeptDivActCodes() 方法很简单从 ApplicationCache 对象返回表的方法。

该网页工作正常。 我可以选择我的值,并将正确的值插入到文本框中。 但是,当我转到不同的页面并返回到此页面时。 我的下拉列表只有 1 个值可供选择。

当我拉出调试器时,我注意到返回网页后,当 GetAllDeptDivActCodes 方法从缓存返回 DataTable 时,DefaultView RowFilter 属性仍然应用于 DataTable,这导致了问题。

我现在已经解决了这个问题,只需在 SelectedIndexChanged 事件中完成处理后重置 DefaultView RowFilter 即可,但是当我期待单独的副本时,为什么应用程序返回似乎是对应用程序缓存中 DataTable 的引用(或对象的值)?

This is an odd thing I've just run into.

I have a web application with a small DataTable stored in the ApplicationCache to reduce the amount of queries to a separate since the data is a lookup table that doesn't change often.

I access this DataTable twice within a given page. Once to bind the data to a drop down list in my Page_Load method:

dtDeptDivAct = GetAllDeptDivActCodes()
dtDeptDivAct.DefaultView.Sort = "LongDescription ASC"
ddlDeptDivAccount.DataSource = dtDeptDivAct.DefaultView
ddlDeptDivAccount.DataTextField = "LongDescription"
ddlDeptDivAccount.DataValueField = "Id"
ddlDeptDivAccount.DataBind()

...and once to retrieve additional data from the table when an index is selected in my ddlDeptDivAct_SelectedIndexChanged event:

Dim dtDeptDivAct As DeptDivActDataTable

If ddlDeptDivAccount.SelectedIndex > 0 Then

   dtDeptDivAct = GetAllDeptDivActCodes()
   dtDeptDivAct.DefaultView.RowFilter = "Id = " & ddlDeptDivAccount.SelectedValue

   txtAddFundingDept.Text = DirectCast(dtDeptDivAct.DefaultView(0).Row, DeptDivActRow).Department.ToString.PadLeft(2, Char.Parse("0"))
   txtAddFundingDiv.Text = DirectCast(dtDeptDivAct.DefaultView(0).Row, DeptDivActRow).Division.ToString.PadLeft(2, Char.Parse("0"))
   txtAddFundingAct.Text = DirectCast(dtDeptDivAct.DefaultView(0).Row, DeptDivActRow).Activity.ToString.PadLeft(3, Char.Parse("0"))

Else

   txtAddFundingDept.Text = ""
   txtAddFundingDiv.Text = ""
   txtAddFundingAct.Text = ""

End If

Note: The GetAllDeptDivActCodes() method is a simple method that returns the table from the ApplicationCache object.

The web page works fine. I can select my value and the proper values are insterted into the TextBox. However, when I go to a different page and come back to this page. My drop down list only has 1 value available for selection.

When I pulled up the debugger, I noticed that upon returning to the web page, when the GetAllDeptDivActCodes method returns the DataTable from the cache, the DefaultView RowFilter property was still applied to the DataTable, which was causing the problem.

I have fixed the issue for now by simply resetting the the DefaultView RowFilter once processing is done in the SelectedIndexChanged event, but why is the Application returning what appears to be a reference to the DataTable in the application cache when I was expecting a seperate copy (or value) of the object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烦人精 2024-07-18 11:01:30

这是设计使然。 每当您将对象存储在应用程序状态或会话状态中时,当您访问它时,您都会返回实际的对象(或者当您将其设置为对该对象的引用时)。 按照设计,.NET 对象几乎总是通过引用传递,除非您另外指定。 例如,当将对象传递给函数时,它们是通过引用传递的。

This is by design. Whenever you store an object in the Application State or Session State you are returned the actual object (or as you put it a reference to the object) when you access it. By design .NET objects are almost always passed by reference unless you specify otherwise. Forexample when passing objects to Functions they are passed by reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文