带字符串数组的奇数循环行为

时间:2017-07-26 18:27:14

标签: arrays string vb.net debugging for-loop

我有以下代码,用于标注和填充26长度的字符串数组:

    Dim intAllWeights As Integer = intFrontWeights + intKingpinWeights + 
    intLandingLegWeights + intCenterWeights + intBogieWeights + intRearWeights
    Dim AllWeightsString(intAllWeights - 1) As String
    Dim k As Integer = 1
    For j = 1 To intFrontWeights
        AllWeightsString(k - 1) = ("F - " + strFrontWeightDescription(j - 1) + ": " + "(x" + intFrontWeightQuantity(j - 1).ToString + "), " + dblFrontWeightWeight(j - 1).ToString + Pounds + ", " + dblFrontWeightOffset(j - 1).ToString + Inches)
        k = k + 1
    Next
    For j = 1 To intKingpinWeights
        AllWeightsString(k - 1) = ("K - " + strKingpinWeightDescription(j - 1) + ": " + "(x" + intKingpinWeightQuantity(j - 1).ToString + "), " + dblKingpinWeightWeight(j - 1).ToString + Pounds + ", " + dblKingpinWeightOffset(j - 1).ToString + Inches)
        k = k + 1
    Next
    For j = 1 To intLandingLegWeights
        AllWeightsString(k - 1) = ("L - " + strLandingLegWeightDescription(j - 1) + ": " + "(x" + intLandingLegWeightQuantity(j - 1).ToString + "), " + dblLandingLegWeightWeight(j - 1).ToString + Pounds + ", " + dblLandingLegWeightOffset(j - 1).ToString + Inches)
        k = k + 1
    Next
    For j = 1 To intCenterWeights
        AllWeightsString(k - 1) = ("C - " + strCenterWeightDescription(j - 1) + ": " + "(x" + intCenterWeightQuantity(j - 1).ToString + "), " + dblCenterWeightWeight(j - 1).ToString + Pounds + ", " + dblCenterWeightOffset(j - 1).ToString + Inches)
        k = k + 1
    Next
    For j = 1 To intBogieWeights
        AllWeightsString(k - 1) = ("B - " + strBogieWeightDescription(j - 1) + ": " + "(x" + intBogieWeightQuantity(j - 1).ToString + "), " + dblBogieWeightWeight(j - 1).ToString + Pounds + ", " + dblBogieWeightOffset(j - 1).ToString + Inches)
        k = k + 1
    Next
    For j = 1 To intRearWeights
        AllWeightsString(k - 1) = ("R - " + strRearWeightDescription(j - 1) + ": " + "(x" + intRearWeightQuantity(j - 1).ToString + "), " + dblRearWeightWeight(j - 1).ToString + Pounds + ", " + dblRearWeightOffset(j - 1).ToString + Inches)
        k = k + 1
    Next

当我逐行浏览代码时,我得到以下结果: enter image description here

当我在没有逐行的情况下运行此代码后突破时,我得到了这个: enter image description here

正如你所看到的,如果我没有F8步进我的代码,第25个索引就不会被设置为等于任何东西。任何想法为什么会这样?很困惑。

1 个答案:

答案 0 :(得分:2)

您可以大大减少此代码:

Dim buildStrings As Func(Of String, String(), Integer(), Double(), Double(), IEnumerable(Of String)) = _ 
    Function(Prefix, Description, Quantity, Weight, Offset)
        Dim Length As Integer = Description.Length
        Return Enumerable.Range(0, Length).Select(Function(j) _
            String.Format("{0} - {1}:(x{2}), {3}{4}, {5}{6}",
               Prefix, Description(j), Quantity(j), Weight(j), Pounds, Offset(j), Inches))
    End Function

Dim FrontStrings   = buildStrings("F", strFrontWeightDescription,      intFrontWeightQuantity,      dblFrontWeightWeight,      dblFrontWeightOffset)
Dim KingpinStrings = buildStrings("K", strKingpinWeightDescription,    intKingpinWeightQuantity,    dblKingpinWeightWeight,    dblKingpinWeightOffset)
Dim LandingStrings = buildStrings("L", strLandingLegWeightDescription, intLandingLegWeightQuantity, dblLandingLegWeightWeight, dblLandingLegWeightOffset)
Dim CenterStrings  = buildStrings("C", strCenterWeightDescription,     intCenterWeightQuantity,     dblCenterWeightWeight,     dblCenterWeightOffset)
Dim BogieStrings   = buildStrings("B", strBogieWeightDescription,      intBogieWeightQuantity,      dblBogieWeightWeight,      dblBogieWeightOffset)
Dim RearStrings    = buildStrings("R", strRearWeightDescription,       intRearWeightQuantity,       dblRearWeightWeight,       dblRearWeightOffset)

Dim AllWeightsString = FrontStrings.Concat(KingpinStrings).Concat(LandingStrings).Concat(CenterStrings).Concat(BogieStrings).Concat(RearStrings).
                         ToArray()

这一开始可能看起来更复杂,特别是如果匿名函数和linq功能不熟悉,但通过减少重复和总代码,它在长行中实际上更容易理解。

通过使用Class es代替分组数组,您可以使其更好,更简单。使用数组按索引匹配数据是一种应该避免的反模式。以下是类的外观:

Public Enum WeightType
    Front
    Kingpin
    LandingLeg
    Center
    Bogie
    Rear
End Enum

Public Class WeightInfo
    Const Pounds As String = "[lbs.]"
    Const Inches As String = "[in]"

    Public Property Style As WeightType
    Public Property Description As String
    Public Property Quantity As Integer
    Public Property Weight As Double
    Public Property Offset As Double

    Public ReadOnly Property StylePrefix As String
        Get
            Select Case Style
                Case WeightType.Front
                    Return "F"
                Case WeightType.Kingpin
                    Return "K"
                Case WeightType.LandingLeg
                    Return "L"
                Case WeightType.Center
                    Return "C"
                Case WeightType.Bogie
                    Return "B"
                Case WeightType.Rear
                    Return "R"
                Case Else
                    Return ""
            End Select
        End Get
    End Property

    Public Overrides Function ToString() As String
        Return String.Format("{0} - {1}:(x{2}), {3}{4}, {5}{6}", StylePrefix, Description, Quantity, Weight, Pounds, Offset, Inches)
    End Function
End Class

如果您的数据是在类实例数组中设置的,那么代码将更像这样:

Dim AllWeightsString = _ 
       FrontWeights.Select(Function(w) w.ToString).Concat(
       KingpinWeights.Select(Function(w) w.ToString)).Concat(
       LandingLegWeights.Select(Function(w) w.ToString)).Concat(
       CenterWeights.Select(Function(w) w.ToString)).Concat(
       BogieWeights.Select(Function(w) w.ToString)).Concat(
       RearWeights.Select(Function(w) w.ToString)).
       ToArray()

从语义上讲,这只是一行代码。您可以将所有数据放在 ONE 大集合(数组)中,您可以根据需要使用Where()按类型进行过滤,从而获得更好的效果。例如:

Dim FrontWeights = AllMyWeights.Where(Function(w) w.Style = WeightType.Front)