在 VB 中将列表从 WCF 传递到 WCF
我正在尝试从另一个列表填充一个列表。我认为这段代码应该可以工作,但最终我得到了相同项目的列表。
Public Sub WriteDatFile(ByRef lstReasons As System.Collections.Generic.List(Of LetterReason))
Dim tmplstReason As New TCPService.LetterReason
Dim tmplstReasons As New System.Collections.Generic.List(Of TCPService.LetterReason)
'Load the letter reasons
For Each LetterReason In lstReasons
tmplstReason._reason = LetterReason.Reason
tmplstReasons.Add(tmplstReason)
Next
RetVal = .......
End Sub
现在,当我设置断点并从调用 WCF 进行检查时,我得到:
lstReason(0).Reason = One
lstReason(1).Reason = Two
lstReason(2).Reason = Three
但是,当我设置断点时(之后负载)在此子例程中我得到以下输出:
tmplstReason(0)._reason = Three
tmplstReason(0)._reason = Three
tmplstReason(0)._reason = 3
发生了什么???有什么想法吗?
谢谢, 贾森
I'm trying to populate one list from another. I would think this code should work, but at the end of the day I get a list of identical items.
Public Sub WriteDatFile(ByRef lstReasons As System.Collections.Generic.List(Of LetterReason))
Dim tmplstReason As New TCPService.LetterReason
Dim tmplstReasons As New System.Collections.Generic.List(Of TCPService.LetterReason)
'Load the letter reasons
For Each LetterReason In lstReasons
tmplstReason._reason = LetterReason.Reason
tmplstReasons.Add(tmplstReason)
Next
RetVal = .......
End Sub
Now, when I set a breakpoint and check from the calling WCF I get this:
lstReason(0).Reason = One
lstReason(1).Reason = Two
lstReason(2).Reason = Three
But, when I set a breakpoint (after the load) in this subroutine I get the following output:
tmplstReason(0)._reason = Three
tmplstReason(0)._reason = Three
tmplstReason(0)._reason = Three
What's going on??? Any ideas?
Thanks,
Jason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在循环内创建
LetterReason
的新实例,并将新实例添加到列表中。试试这个You need to create a new instance of
LetterReason
inside the loop and add the new instance to the list. Try this仔细看。您实际上并没有更改 tmplstReason,而是更改了它的 ._reason 属性。然后将 tmplstReason 添加到列表中 3 次。
结果是,您实际上每次都将相同的内容添加到列表中,并且每次都更改该对象的 ._reason 变量。由于它们都是相同的,因此它们都具有相同的值。 :)
Look carefully. You're not actually changing tmplstReason, you're changing the ._reason property of it. You then add tmplstReason to the list 3 times.
The result is that you actually add the same thing to the list each time, and change that one object's ._reason variable each time. Since they're all the same, they all have the same value. :)