VB.Net Array.Sort使值无效

时间:2016-02-09 20:33:57

标签: arrays vb.net sorting

我的VB.net程序中的Array.Sort功能遇到了一些严重问题。我的代码中的所有内容,包括粘贴在这里的内容似乎都在循环中运行(在我的代码末尾),包括使用我期望的信息填充FinalDataArray,然而当我将它传递给Array.Sort,然后我得到的FinalDataArray在每个索引处都有一个空值。关于可能导致这种情况的任何想法?

我的代码的适用部分如下:

更新:我已经更新了我的代码块,以显示该程序中的整个Sub。

Private Sub btnDisplay_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplay.Click
    frmMain.CurrentFigure = txtFigure.Text
    frmFigureViewer.Show()
    Dim ArrayUpperBound = FGCFigNo.Length
    FigureResult = Enumerable.Range(0, FGCFigNo.Length).Where(Function(f) FGCFigNo(f) = frmMain.CurrentFigure).ToArray
    frmFigureViewer.lblFGCfromFGCList.Text = FGCCode(FigureResult(0))
    frmFigureViewer.lblTOPCurrentFigureTitle.Text = FGCTitle(FigureResult(0))
    frmFigureViewer.lblTOPDisplayFigure.Text = FGCFigNo(FigureResult(0))
    frmFigureViewer.lblCurrentSheet.Text = FGCSheetNo(FigureResult(0))
    frmFigureViewer.lblFinalSheet.Text = FGCSheetNo(FigureResult(FigureResult.Length - 1))
    ArtResult = FGCArtID(FigureResult(frmFigureViewer.lblCurrentSheet.Text - 1))
    Dim JpgFilePath As String = "C:\Temp\StandaloneRPSTLViewer" + "\" + Mid(ArtResult, 1, ArtResult.Length - 4) + ".jpg"
    If File.Exists(JpgFilePath) Then
        GoTo OnlyDisplay
    End If
    Dim SourceFile As String = ""
    Dim DestinationFile As String = ""
    Dim EpsFilePath As String = ARTFOLDER + "\" + ArtResult
    If Not Directory.Exists("C:\Temp\StandaloneRPSTLViewer") Then
        MkDir("C:\Temp\StandaloneRPSTLViewer")
    End If



    SourceFile = EpsFilePath
    DestinationFile = Mid(EpsFilePath, 1, Len(EpsFilePath) - 4) + " view.eps"
    FileCopy(SourceFile, DestinationFile)

    EpsFilePath = DestinationFile

    RunGS("-q", "-dNOPAUSE", "-dBATCH", "-dSAFER", "-sDEVICE=jpeg", _
      "-r600", "-dJPEGQ=100", "-dEPSCrop", "-dDownScaleFactor=0", "-dTextAlphaBits=4", "-dGraphicsAlphaBits=4", _
      "-sPAPERSIZE=letter", "-sOutputFile=" & JpgFilePath, EpsFilePath)
    Kill(EpsFilePath) 'KILLS THE "...view.eps" temporary file in the working folder.

OnlyDisplay:

    frmFigureViewer.picMyJPEG.Image = Image.FromFile(JpgFilePath)

    Fig036Result = Enumerable.Range(0, FIGURENO.Length).Where(Function(f) FIGURENO(f) = frmMain.CurrentFigure).ToArray

    'BUILD DICTIONARY HERE'
    Dim dict As Dictionary(Of String, String) = New Dictionary(Of String, String)
    Dim DictionaryFigureNumber As Integer = 1
    Dim DictionarySortPosition As Integer = 1
    Do Until DictionaryFigureNumber = 1000
        Dim DictionaryFigureLetterASCII As Integer = 65
        dict.Add(DictionaryFigureNumber, DictionarySortPosition)
        DictionarySortPosition = DictionarySortPosition + 1
        Do Until DictionaryFigureLetterASCII = 91

            dict.Add(DictionaryFigureNumber & Chr(DictionaryFigureLetterASCII), DictionarySortPosition)
            DictionarySortPosition = DictionarySortPosition + 1
            DictionaryFigureLetterASCII = DictionaryFigureLetterASCII + 1
        Loop
        DictionaryFigureNumber = DictionaryFigureNumber + 1
    Loop
    'END OF DICTIONARY BUILDING

    Dim TempLength = Fig036Result.GetLength(0)
    TempLength = TempLength - 1
    Dim CurrentPlace As Integer = 0
    Dim TempItemNo As String
    Dim ArrayString As String = ""
    Do Until CurrentPlace > TempLength

        Dim ArrayLocation As Integer = Fig036Result(CurrentPlace)
        TempItemNo = ITEMNO(ArrayLocation)
        TempSMR = SMnR(ArrayLocation)
        TempNSN = NSN(ArrayLocation)
        TempCAGE = CAGE(ArrayLocation)
        TempPartNum = PARTNUM(ArrayLocation)
        TempDescription = ITEMName(ArrayLocation)
        TempUOC = UOC(ArrayLocation)
        TempQTY = QTYFIG(ArrayLocation)
        TempIndvFGC = FGC(ArrayLocation)
        Dim TempValue As String = ""
        dict.TryGetValue(TempItemNo, TempValue)
        ArrayString = TempValue + "::" + TempItemNo + "|" + TempSMR + "|" + TempNSN + "|" + TempCAGE + "|" + TempPartNum + "|" + TempDescription + "|" + TempUOC + "|" + TempQTY + "|" + TempIndvFGC + "|"
        FinalDataArray(CurrentPlace) = ArrayString

        'FinalSortArray(CurrentPlace) = TempValue

        CurrentPlace = CurrentPlace + 1
    Loop
    Array.Sort(FinalDataArray)

End Sub

Update2:我已经更新了代码的循环部分,如下所示:

Dim TempValue As String = ""
        dict.TryGetValue(TempItemNo, TempValue)
        ArrayString = TempItemNo + "|" + TempSMR + "|" + TempNSN + "|" + TempCAGE + "|" + TempPartNum + "|" + TempDescription + "|" + TempUOC + "|" + TempQTY + "|" + TempIndvFGC + "|"
        FinalDataArray(CurrentPlace) = ArrayString

        FinalSortArray(CurrentPlace) = TempValue

        CurrentPlace = CurrentPlace + 1
    Loop
    Array.Sort(FinalSortArray, FinalDataArray)

我的两个阵列的新声明是:

    Public FinalSortArray(50000) As Integer
Public FinalDataArray(50000) As String

1 个答案:

答案 0 :(得分:0)

我认为你的问题是你不知道数组究竟有多大,它比你想象的要大,Array.Sort从后面重新填充数组......考虑一下:

Dim arr() As String
ReDim arr(10)
arr(0) = "Z"
arr(2) = "3"
arr(1) = "Boolean"
arr(5) = "Hello"
Array.Sort(arr)
For i As Integer = 0 To 10
  Debug.Print(arr(i))
Next

即使我将数组的大小指定为11并且仅使用数据填充了4个随机点,但是Array.Sort我的数据在数组的后面排序后只有arr(7)...arr(10)包含数据(按照正确的顺序)。