为什么我的ApplicationCache传回引用而不是值? [英] Why is my ApplicationCache passing back a reference instead of a value?

查看:86
本文介绍了为什么我的ApplicationCache传回引用而不是值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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()

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

...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

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

网页工作正常.我可以选择我的值,并且适当的值将插入到TextBox中.但是,当我转到其他页面并返回此页面时.我的下拉列表中只有1个值可供选择.

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.

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

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.

我已经通过在SelectedIndexChanged事件中完成处理后立即简单地重置DefaultView RowFilter来解决此问题,但是为什么我期望应用程序返回似乎是对应用程序缓存中的DataTable的引用呢?对象的单独副本(或值)?

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?

推荐答案

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

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.

这篇关于为什么我的ApplicationCache传回引用而不是值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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