在 VB 中将列表从 WCF 传递到 WCF

发布于 2024-11-06 19:08:20 字数 858 浏览 3 评论 0原文

我正在尝试从另一个列表填充一个列表。我认为这段代码应该可以工作,但最终我得到了相同项目的列表。

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 技术交流群。

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

发布评论

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

评论(2

浅暮の光 2024-11-13 19:08:20

您需要在循环内创建 LetterReason 的新实例,并将新实例添加到列表中。试试这个

 For Each LetterReason In lstReasons
        Dim tmplstReason As New TCPService.LetterReason
        tmplstReason._reason = LetterReason.Reason
        tmplstReasons.Add(tmplstReason)
    Next

You need to create a new instance of LetterReason inside the loop and add the new instance to the list. Try this

 For Each LetterReason In lstReasons
        Dim tmplstReason As New TCPService.LetterReason
        tmplstReason._reason = LetterReason.Reason
        tmplstReasons.Add(tmplstReason)
    Next
暗藏城府 2024-11-13 19:08:20
tmplstReason._reason = LetterReason.Reason
tmplstReasons.Add(tmplstReason)

仔细看。您实际上并没有更改 tmplstReason,而是更改了它的 ._reason 属性。然后将 tmplstReason 添加到列表中 3 次。

结果是,您实际上每次都将相同的内容添加到列表中,并且每次都更改该对象的 ._reason 变量。由于它们都是相同的,因此它们都具有相同的值。 :)

tmplstReason._reason = LetterReason.Reason
tmplstReasons.Add(tmplstReason)

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

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