简体   繁体   English

通过 vb.net 中的 foreach 循环在列表中插入 class 属性

[英]Inserting class property in list through foreach loop in vb.net

I am trying to insert class property in list through foreach loop in VB.NET.我正在尝试通过 VB.NET 中的 foreach 循环将 class 属性插入列表中。

Dim objCoInsurers As List(Of CoInsurer)

CoInsurer class consist of 5 properties CoInsurer class 由 5 个属性组成

Below there is the code where class properties are inserted in my list下面是在我的列表中插入 class 属性的代码

 objCoInsurers = New List(Of CoInsurer)
 Dim oCoInsurer As New CoInsurer
 For Each row As GridViewRow In grdCoInsurance.Rows
  Dim lblCoInsurerID = DirectCast(row.FindControl("lblCoInsurerID"), Label)
  Dim textCoInsurerID As String = lblCoInsurerID.Text
  Dim lblCoInsurerName = DirectCast(row.FindControl("lblCoInsurerName"), Label)
  Dim CoInsurerName As String = lblCoInsurerName.Text
oCoInsurer.InsurerID = lblCoInsurerID.Text
oCoInsurer.InsurerName = lblCoInsurerName.Text
 objCoInsurers.Add(oCoInsurer)
Next

The issue is when two list containing same value, the first list overwrite the second one, if row count is 2 in gridview or more问题是当两个列表包含相同的值时,如果 gridview 中的行数为 2 或更多,第一个列表会覆盖第二个列表

Consider this.考虑一下。 Let's say that you want to add people to a list using pen and paper.假设您想使用笔和纸将人员添加到列表中。 I present myself to you wearing a blue shirt and you write down my name.我穿着一件蓝色衬衫向你展示自己,你写下我的名字。 I then take off my blue shirt and put on a red shirt and present myself to you and you write down my name.然后我脱下我的蓝色衬衫,穿上一件红色衬衫,向你展示自己,你写下我的名字。 I then take off my red shirt and put on a green shirt and present myself to you and you write down my name.然后我脱下我的红衬衫,穿上一件绿衬衫,向你展示自己,你写下我的名字。 How many names do you have on the list?你有多少名字在名单上? How many people do those names correspond to?这些名字对应多少人? What colour shirt do the people those names correspond to have on?这些名字对应的人穿什么颜色的衬衫?

Given that OOP is based on the behaviour of real-world objects, think about what your code actually does.鉴于 OOP 是基于现实世界对象的行为,请考虑您的代码实际执行的操作。 How many ColInsurer objects does your code create?您的代码创建了多少ColInsurer对象? Only one, right?只有一个,对吧? It doesn't matter how many different colour shirts you put on that object and how many times you add it to the list, there's still only one object so it can only be wearing one shirt at a time.不管你在那个 object 上穿了多少件不同颜色的衬衫,以及你将它添加到列表中多少次,仍然只有一件 object,所以它一次只能穿一件衬衫。 At the end of your loop, every item in the list refers to the same object and that object will have whatever property values you set last.在循环结束时,列表中的每个项目都引用相同的 object 并且 object 将具有您最后设置的任何属性值。

If you want to add multiple distinct objects to the list then you actually have to create multiple distinct objects.如果您想将多个不同的对象添加到列表中,那么您实际上必须创建多个不同的对象。 That means moving the creation of the object inside the loop, so it happens every iteration.这意味着将 object 的创建移动到循环内,因此每次迭代都会发生。 You create a new object, set its properties and then add it to the list:您创建一个新的 object,设置其属性,然后将其添加到列表中:

For Each row As GridViewRow In grdCoInsurance.Rows
    Dim oCoInsurer As New CoInsurer

I am guessing your class looks something like this.我猜你的 class 看起来像这样。

Public Class CoInsurer
    Public Property InsurerID As String
    Public Property InsurerName As String
End Class

I am wondering if InsurerID is a String and not an Integer or Long .我想知道InsurerID是否是String而不是IntegerLong If it is a number, be sure to surround the assignment value in the loop with CInt() or CLng()如果是数字,请务必在循环中用CInt()CLng()包围赋值值

I got rid of some of the unneeded variables.我摆脱了一些不需要的变量。 As jmcilhinney explained, you need to create a new instance of CoInsurer for every iteration of your loop.正如 jmcilhinney 解释的那样,您需要为循环的每次迭代创建一个新的CoInsurer实例。

Private Sub OPCode()
    For Each row As GridViewRow In grdCoInsurance.Rows
        Dim oCoInsurer As New CoInsurer
        oCoInsurer.InsurerID = DirectCast(row.FindControl("lblCoInsurerID"), Label).Text
        oCoInsurer.InsurerName = DirectCast(row.FindControl("lblCoInsurerName"), Label).Text
        objCoInsurers.Add(oCoInsurer)
    Next
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM