如何返回一个类类型和填充数组? [英] How to return an array of class type and fill?

查看:70
本文介绍了如何返回一个类类型和填充数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,应该返回一个类类型的数组.

I have a function which is supposed to return an array of class type.

因此,我从数据库中选择了数据并填充了数据表.现在从数据表中,我选择列值并将其分配给类的属性.但是问题是我在做什么只会分配一个记录,但是我想用多个记录来填充类类型数组.

So from database I picked data and filled datatable. Now from datatable I am picking columns value and assigning to the properties of the class. But problem is that what I am doing will assign a single record only but I want to fill the class type array with multiple record.

我的代码:

Private Sub BindGridInfoFunctionalLocation()
        Dim v_ObjDs As New DataSet
        Try
            v_ObjDs = v_ObjBREngine.FetchSqlDS("select FunctionalLocation as 'TagNo', EquipmentDescription as 'TagDescription', Area as 'ParentHeirarchy', EqptType as 'TypeClass' from EngineeringData")

            Dim dtTableFL As DataTable
            dtTableFL.Columns.Add("TagNo", GetType(String))
            dtTableFL.Columns.Add("TagDescription", GetType(String))
            dtTableFL.Columns.Add("ParentHeirarchy", GetType(String))
            dtTableFL.Columns.Add("Class", GetType(String))

            dtTableFL = v_ObjDs.Tables(0)

            Dim AssetsCls As New AssetsDto
            'Dim AssetsClsArray As New AssetsDto()

            If v_ObjDs.Tables(0).Rows.Count > 0 Then
                'v_ObjDs.Tables(0).Rows.Add(v_ObjDs.Tables(0).NewRow())

                For Each Item In dtTableFL.Rows

                    AssetsCls.ASSETTAG = Item("TagNo")
                    AssetsCls.ASSETDESC = Item("TagDescription")
                    AssetsCls.PARENTTAG = Item("ParentHeirarchy")
                    AssetsCls.ASSETTYPE = Item("TypeClass")

                Next

                gv_InfoFunctionalLocation.DataSource = v_ObjDs
                gv_InfoFunctionalLocation.DataBind()
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

注意:

返回类型和所有内容都应该是这样的,因为我正在与客户端的Web服务进行交互,并且它仅希望通过这种方式实现.

the return type and everything should be like this since I am interacting with the client's web service and it expects it this way only.

推荐答案

在您的示例中,没有一个数组可以将表中的每个元素存储在AssetsDto中进行转换之后.此外,如果要将任何内容返回给调用方,则需要从Sub更改为Function.
另一个重要的变化是相对于用于存储Dto的数据结构.建议不要使用数组(您需要知道有效使用元素的数量),而建议使用List(Of AssetsDto),在其中可以动态添加元素,而不必担心列表的大小.

In your example there is no array where each element of the table could be stored after the transform in an AssetsDto. Moreover, you need to change from Sub to Function if you want to return anything to the caller.
Another important change is relative to the data structure used to store the Dtos. Instead of using an array (you need to know the number of elements to effectively use it), I suggest to use a List(Of AssetsDto) where you can dynamically add elements without worrying about the size to the list.

' Declare as a Function that returns a List(Of AssetsDto)
Private Function BindGridInfoFunctionalLocation() as List(Of AssetsDto)
    Dim v_ObjDs As New DataSet

    ' This is where we store each record transformed in an AssetsDto
    Dim result as List(Of AssetsDto) = new List(Of AssetsDto)
    Try
        v_ObjDs = v_ObjBREngine.FetchSqlDS("select FunctionalLocation as 'TagNo', EquipmentDescription as 'TagDescription', Area as 'ParentHeirarchy', EqptType as 'TypeClass' from EngineeringData")

        Dim dtTableFL As DataTable = v_ObjDs.Tables(0)

        ' Cycle to create the AssetsDto
        For Each Item In dtTableFL.Rows

            ' IMPORTANT. At each loop create a new AssetsDto, otherwise you
            ' will just change the values of the same instance 
            Dim AssetsCls As AssetsDto = new AssetsDto()
            AssetsCls.ASSETTAG = Item("TagNo")
            AssetsCls.ASSETDESC = Item("TagDescription")
            AssetsCls.PARENTTAG = Item("ParentHeirarchy")
            AssetsCls.ASSETTYPE = Item("TypeClass")

            ' Add the instance to the list
            result.Add(AssetsCls)
        Next
        gv_InfoFunctionalLocation.DataSource = v_ObjDs
        gv_InfoFunctionalLocation.DataBind()
    End If

    ' Return to caller the results
    Return results
    Catch ex As Exception
        Throw ex
    End Try
End Function

这篇关于如何返回一个类类型和填充数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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