填充结构

时间:2015-04-23 11:42:04

标签: vb.net

我有一个包含47个字段的结构。我还有一个包含“|”的字符串划界价值。我想用字符串中的值填充结构。我能想到的唯一解决方案如下。但这涉及到硬编码。是否有更好的方法通过循环或其他任何方式实现相同的目标

 Dim lobjStructData As New SUB_PNDGORDR_QRY_RESP
        Dim lstrarrStream As String() = pstrStream.Split(CChar("|"))

        lobjStructData.ClientId = CType(lstrarrStream(0), Char())
        lobjStructData.PortfolioId = CShort(lstrarrStream(1))
        lobjStructData.Currency = CType(lstrarrStream(2), Char())
        lobjStructData.ProductId = CType(lstrarrStream(3), Char())
        lobjStructData.InstrumentClass = CShort(lstrarrStream(4))
        lobjStructData.OrderSymbol = CType(lstrarrStream(5), Char())
        lobjStructData.Qty = CDbl(lstrarrStream(6))
        lobjStructData.QtyRemaining = CDbl(lstrarrStream(7))
        lobjStructData.OrderPrice = CDbl(lstrarrStream(8))
        lobjStructData.TriggerPrice = CDbl(lstrarrStream(9))
        lobjStructData.Remarks = CType(lstrarrStream(10), Char())
        lobjStructData.QtyDiscRem = CDbl(lstrarrStream(11))
        lobjStructData.OrderDateTime = CInt(lstrarrStream(12))
        lobjStructData.IntOrderNo = CDbl(lstrarrStream(13))
        lobjStructData.OrderStat = CType(lstrarrStream(14), Char())
        lobjStructData.DiscQty = CDbl(lstrarrStream(15))
        lobjStructData.SerialNo = CInt(lstrarrStream(16))
        lobjStructData.OpenCloseFlg = CInt(lstrarrStream(17))
        lobjStructData.InstrumentId = CInt(lstrarrStream(18))
        lobjStructData.ContractSeriesId = CInt(lstrarrStream(19))
        lobjStructData.InstrumentType = CShort(lstrarrStream(20))
        lobjStructData.BuySellInd = CShort(lstrarrStream(21))
        lobjStructData.MinFillQty = CDbl(lstrarrStream(22))
        lobjStructData.ExchId = CInt(lstrarrStream(23))
        lobjStructData.OrderTyp = CShort(lstrarrStream(24))
        lobjStructData.ValidityCond = CDbl(lstrarrStream(25))
        lobjStructData.ExchTrdNo = CType(lstrarrStream(26), Char())
        lobjStructData.IntTranscode = CShort(lstrarrStream(27))
        lobjStructData.ValidityAttr = CInt(lstrarrStream(28))
        lobjStructData.BosRefId = CInt(lstrarrStream(29))
        lobjStructData.OriginatorId = CType(lstrarrStream(30), Char())
        lobjStructData.Commision = CDbl(lstrarrStream(31))
        lobjStructData.SolicitedFlg = CType(lstrarrStream(32), Char())
        lobjStructData.OrderFamily = CType(lstrarrStream(33), Char())
        lobjStructData.ReceiptTime = CInt(lstrarrStream(34))
        lobjStructData.ExchShortSellFlag = CType(lstrarrStream(35), Char())
        lobjStructData.cSourceFlag = CChar(lstrarrStream(36))
        lobjStructData.extnno = CInt(lstrarrStream(37))
        lobjStructData.sReceiptMode = CType(lstrarrStream(38), Char())
        lobjStructData.sErrorMsg = CType(lstrarrStream(39), Char())
        lobjStructData.cCoverUncover = CChar(lstrarrStream(40))
        lobjStructData.fATOrderNo = CDbl(lstrarrStream(41))
        lobjStructData.iQtyCond = CShort(lstrarrStream(42))
        lobjStructData.iReserve2 = CChar(lstrarrStream(43))
        lobjStructData.fInternalRefId = CDbl(lstrarrStream(44))

1 个答案:

答案 0 :(得分:0)

嗯,你在这里有一些裁员:

  1. 您转换为的数据类型(可能与字段的数据类型相同),
  2. 数组中的位置(与编写语句的顺序相同),
  3. 结果变量的名称
  4. 信息流的名称。
  5. 让我们试着摆脱它们。我们将删除(1)方法重载,(2)使用自动递增的位置变量,(3)使用较短的变量名称和(4)通过将转换代码提取到方法中。

    以下代码未经测试,但这个想法应该是显而易见的:

    Public Function ParseStream(pstrStream As String) As SUB_PNDGORDR_QRY_RESP
        Dim p As New Parser(pstrStream)
        Dim r As New SUB_PNDGORDR_QRY_RESP()
        With r
            p.ParseField(.ClientId)
            p.ParseField(.PortfolioId)
            p.ParseField(.Currency)
            ...
        End With
    
        Return r
    End Function 
    
    Private Class Parser
        Public Sub New(pstrStream As String)
           Me.lstrarrStream = pstrStream.Split("|"c)
        End Sub
    
        Private pos As Integer = 0
        Private lstrarrStream As String()
    
        Public Sub ParseField(ByRef field As Char())
            field = CType(lstrarrStream(pos), Char())
            pos += 1
        End Sub
    
        Public Sub ParseField(ByRef field As Short)
            field = CShort(lstrarrStream(pos))
            pos += 1
        End Sub
    
        ... other ParseField overloads...
    End Class