1

嘿,我正在尝试以正确的格式将我的数组放入 ASP.net 网页中,以获取订单表。

顺序应该是:

model_number
comm_category
service
freight
sales_tax
sales
unit_price
price
id_price

目前它输出如下:

model_number
price
unit_price
id_price
sales_tax
sales
service
freight
comm_category

因此,我无法在此处使用我的代码以正确的顺序附加到我的字符串:

Public Sub AssocArray_To_String(ByRef Output As System.Text.StringBuilder, ByVal AssocArrayInput As AssocArray)
    For iParent As Integer = 0 To AssocArrayInput.Count - 1
        Dim intX As Integer = 0
        Dim arrParent As AssocArray = TryCast(AssocArrayInput.Item(iParent), AssocArray)
        If arrParent Is Nothing Then Continue For

        For iChild As Integer = 0 To arrParent.Count - 1
            Call buildItems(PHPConvert.ToString(arrParent.Keys(iChild)), PHPConvert.ToString(arrParent.Values(iChild)), intX)
            intX += 1
        Next iChild
    Next iParent
End Sub

Private Sub buildItems(ByVal nameOfItem As String, ByVal itemItself As String, ByVal intX As Integer)
    Dim tmpStuff As Decimal = 0.0
    Dim qty As Integer = 1
    Dim model_number As String = ""
    Dim comm_category As String = ""
    Dim service As Double = 0
    Dim freight As Double = 0
    Dim sales_tax As Double = 0
    Dim sales As Double = 0
    Dim unit_price As Double = 0
    Dim price As Double = 0
    Dim id_price As Double = 0

    If nameOfItem = "model_number" Then
        model_number = itemItself
        tableLoop += "<tr><td bgcolor=""#CCCCCC""><asp:Label ID=""item_count_" & intX & """ Text=""Label"">" & qty & "</asp:Label></td>"
        tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""model_number_" & intX & """ Text=""Label"">" & model_number & "</asp:Label></td>"
    ElseIf nameOfItem = "comm_category" Then
        comm_category = itemItself
        tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""comm_category_" & intX & """ Text=""Label"">" & comm_category & "</asp:Label></td>"
    ElseIf nameOfItem = "service" Then
        service = Format(Convert.ToDouble(itemItself), "$######.00")
        tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""service_" & intX & """ Text=""Label"">" & service & "</asp:Label></td>"
    ElseIf nameOfItem = "freight" Then
        freight = Format(Convert.ToDouble(itemItself), "$######.00")
        tableLoop += "<td><asp:Label ID=""freight_" & intX & """ Text=""Label"">" & freight & "</asp:Label></td>"
    ElseIf nameOfItem = "sales_tax" Then
        sales_tax = Format(Convert.ToDouble(itemItself), "$######.00")
        tableLoop += "<td><asp:Label ID=""sales_tax_" & intX & """ Text=""Label"">" & sales_tax & "</asp:Label></td>"
    ElseIf nameOfItem = "sales" Then
        sales = Format(Convert.ToDouble(itemItself), "$######.00")
        tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""sales_" & intX & """ Text=""Label"">" & sales & "</asp:Label></td>"
    ElseIf nameOfItem = "unit_price" Then
        unit_price = Format(Convert.ToDouble(itemItself), "$######.00")
        tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""unit_price_" & intX & """ Text=""Label"">" & unit_price & "</asp:Label></td></tr>"
    ElseIf nameOfItem = "price" Then
        price = Format(Convert.ToDouble(itemItself), "$######.00")
        tableLoop += "<td><asp:Label ID=""amount_" & intX & """ Text=""Label"">" & price & "</asp:Label></td>"
    ElseIf nameOfItem = "id_price" Then
        id_price = Format(Convert.ToDouble(itemItself), "$######.00")
        tableLoop += "<td><asp:Label ID=""id_price_" & intX & """ Text=""Label"">" & id_price & "</asp:Label></td>"
    End If
End Sub

最终结果是这样的:

<tr><td bgcolor="#CCCCCC"><asp:Label ID="item_count_0" Text="Label">1</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="model_number_0" Text="Label">TTN491-7BW</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="sales_1" Text="Label">3084.63</asp:Label></td>
<td><asp:Label ID="amount_2" Text="Label">3810</asp:Label></td>
<td><asp:Label ID="sales_tax_3" Text="Label">290.37</asp:Label></td>
<td><asp:Label ID="id_price_4" Text="Label">3810</asp:Label></td></tr>
<td bgcolor="#CCCCCC"><asp:Label ID="comm_category_5" Text="Label">X24</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="service_6" Text="Label">51</asp:Label></td>
<td><asp:Label ID="freight_7" Text="Label">384</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="unit_price_8" Text="Label">3135.63</asp:Label></td></tr>

应该像这样输出:

<tr><td bgcolor="#CCCCCC"><asp:Label ID="item_count_0" Text="Label">1</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="model_number_0" Text="Label">TTN491-7BW</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="comm_category_5" Text="Label">X24</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="service_6" Text="Label">51</asp:Label></td>
<td><asp:Label ID="freight_7" Text="Label">384</asp:Label></td>
<td><asp:Label ID="sales_tax_3" Text="Label">290.37</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="sales_1" Text="Label">3084.63</asp:Label></td>
<td bgcolor="#CCCCCC"><asp:Label ID="unit_price_8" Text="Label">3135.63</asp:Label></td></tr>
<td><asp:Label ID="amount_2" Text="Label">3810</asp:Label></td>
<td><asp:Label ID="id_price_4" Text="Label">3810</asp:Label></td></tr>

我怎样才能把数组按正确的顺序排列?

4

2 回答 2

1

为什么你不只是简单地直接引用你需要的项目,而不是循环通过关联键(以任意顺序返回):

Dim arrParent As AssocArray = TryCast(AssocArrayInput.Item(iParent), AssocArray)
If arrParent Is Nothing Then Continue For

Call buildItems("model_number", PHPConvert.ToString(arrParent.Values("model_number")), intX)
Call buildItems("comm_category", PHPConvert.ToString(arrParent.Values("model_number")), intX)
Call buildItems("service", PHPConvert.ToString(arrParent.Values("model_number")), intX)
Call buildItems("freight", PHPConvert.ToString(arrParent.Values("model_number")), intX)
'ETC

或者更好的是,完全摆脱BuildItems并在上面的适当位置内联逻辑。

于 2012-08-17T14:46:04.827 回答
1

你在打电话

For iChild As Integer = 0 To arrParent.Count - 1
        Call buildItems(PHPConvert.ToString(arrParent.Keys(iChild)), 
                        PHPConvert.ToString(arrParent.Values(iChild)),  
                        intX)
        intX += 1
Next iChild

因此,您将始终获得 arrParent 中项目的顺序。所以你所要做的就是以正确的顺序手动调用 buildItems(...)

Call buildItems(PHPConvert.ToString("model_number"), 
                PHPConvert.ToString(arrParent.Values("model_number")),  
                        intX)
Call buildItems(PHPConvert.ToString("comm_category"), 
                PHPConvert.ToString(arrParent.Values("comm_category")),  
                        intX)

' and so on

Furthermore I can't find a definition for tableLoop in buildItems. It seems that this is a simple string variable. You should make use of a StringBuilder as this class is way faster in concatenating strings! See MSDN.

于 2012-08-17T14:49:09.907 回答