簡體   English   中英

Visual Basic分配的並行數組

[英]parallel array for Visual Basic assignment

對於此分配,我被要求“將文件讀入與文件中的列相對應的並行字符串數組中”。 文件中的列為:“ P_CODE”“ P_DESCRIPT”“ P_INDATE”“ P_QOH”“ P_MIN”“ P_PRICE”。 我的問題是“什么是並行數組?” 看起來像什么 我猜想我會怎么做,但是我不確定自己是否走對了。

  ========== project Inventory ========== 

選擇讀取按鈕將導致從項目的默認文件夾中讀取InventoryData.txt文件的內容。 該文件確實包含列標題行。

將文件讀入對應於文件中各列的並行字符串數組。 讀取文件后,將其關閉。 在使用Format()方法設置格式的“列表”框中,顯示並行數組中文件的內容,包括列標題。 根據慣例,將數據列左右對齊。 提示:將列表框的Font屬性設置為非比例空格字體,例如Courier。

將並行數組中的文件內容(包括列標題)寫入在項目的默認文件夾中創建的名為InventoryDataOut.txt的文件。 用豎線(|)分隔列。 每個庫存項目以一行輸出到文件。

到目前為止,這是我所做的。 我剛開始,所以這不是功能代碼。

 Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnRead.Click

        Dim dirPath As String = "C:\Users\...\Inventory\"
        Dim filePath As String = dirPath & "InventoryData.txt"

        'Arrays
        Dim P_CODE As String()
        Dim P_DESCRIPT As String()
        Dim P_INDATE As String()
        Dim P_QOH As String()
        Dim P_MIN As String()
        Dim P_PRICE As String()


    ' Open file for reading

    Dim textIn As New StreamReader(
        New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None))

    'Read in the lines of text into the String variables in the array;

    Dim i As Integer = 0
    Do While textIn.Peek <> -1
        Dim row As String = textIn.ReadLine
        Dim columns As String() = row.Split(CChar(" "))
        P_CODE(i) = columns(i)
        P_DESCRIPT(i) = columns(i)
        P_INDATE(i) = columns(i)
        P_QOH(i) = columns(i)
        P_MIN(i) = columns(i)
        P_PRICE(i) = columns(i)
        i = i + 1
    Loop


    '=====================================
    '- After reading the file, close it. 
    textIn.Close()

我得到以下警告和錯誤(對於每個數組):

警告1在給變量'P_CODE'賦值之前,先使用它。 空引用異常可能在運行時導致。

列作為獨立數組? :/


一線回答: 您做對了


但是:

  • 有更簡單的方法可以做到這一點(就用法和可讀性而言,而非性能)
  • 您的代碼中有錯別字。

StackOverflow的主要目的不是教授基本的語言參考,而是通過適當的編碼幫助您克服特定的問題(同時請記住,如果答案沒有提供回答OP問題的要素,那么答案就不是答案)

您說的錯誤與“什么是並行數組”這個問題並沒有真正的關系。 “在為變量分配值之前使用的變量” 當然 ! 您的數組為Null 聲明了變量,但尚未創建任何實例。 數組在dotNet中是如此嚴格(與JavaScript不同)

您真正的問題是什么?
它與“我應該如何為並行數組分配值”更相關,可以像“如何讀取並行數組中的文本文件中存儲的字符串值並將其寫回?”這樣重新編寫?

作業問題通常不允許使用替代方法(有時這類作業問題被否決了)

不同的方法,例如(使用帶有linq的數據庫或):


使用File.ReadAllLines()從一開始就知道行數...

在頂層聲明數組 :您將無法在btnRead之外訪問它們。否則,請單擊。

Private P_CODE As String()
Private P_DESCRIPT As String()
' ...
Private P_PRICE As String()

將文件內容加載到btnRead Click中...

Dim AllLines As String() = File.ReadAllLines(filePath)
' ^^ the above will open and close the file XD

Dim Columns As String()

' Initialize your arrays...
ReDim(P_CODE, AllLines.Length - 1)
ReDim(P_DESCRIPT, AllLines.Length - 1)
' ...
ReDim(P_PRICE, AllLines.Length - 1)

' set each cell value.
For LineIndex As Int32 = 0 To P_CODE.Length - 1
    Columns = AllLines(LineIndex).Split(" "c)
    P_CODE(LineIndex) = Columns(0)
    P_DESCRIPT(LineIndex) = Columns(1)
    ' ...
    P_PRICE(LineIndex) = Columns(5)

    ' Hey ! Your File Datas are ordered horizontaly,
    ' so Columns must be indexed from 0 to 5 
    ' instead of you "i" in your code in order 
    ' to correctly feed your parallel arrays
Next

您的老師想強迫您使用StreamReader打開文件,並教您不要忘記關閉文件...並使用StreamReader.ReadLine() ...好吧..!

' ...

Dim LineIndex As Int32 = 0
Dim textIn As New StreamReader(filePath)
' ^^ why do you bother using a FileStream ?
Dim Row As String = textIn.ReadLine()
Dim Columns As String()

Do While row IsNot Nothing
    Redim Preserve P_CODE(LineIndex)
    Redim Preserve P_DESCRIPT(LineIndex)
    ' ...
    Redim Preserve P_PRICE(LineIndex)

    Columns = Row.Split(" "c)

    P_CODE(LineIndex) = Columns(0)
    P_DESCRIPT(LineIndex) = Columns(1)
    ' ...
    P_PRICE(LineIndex) = Columns(5)

    Row = textIn.ReadLine()
    LineIndex = LineIndex + 1
Loop

textIn.Close() ' Voila !

' /!\ Add at least a Try Catch enclosing your Do/Loop if you're lazy.

告訴您的老師您將使用Using因為他強迫您使用StreamReader.Peek() (.Peek()是否必要?)

Using textIn As New StreamReader(filePath)
    Dim LineIndex As Int32 = 0
    Dim Columns As String()

    Do While textIn.Peek() <> -1
        Redim Preserve P_CODE(LineIndex)
        Redim Preserve P_DESCRIPT(LineIndex)
        ' ...
        Redim Preserve P_PRICE(LineIndex)

        Columns = textIn.ReadLine().Split(" "c)

        P_CODE(LineIndex) = Columns(0)
        P_DESCRIPT(LineIndex) = Columns(1)
        ' ...
        P_PRICE(LineIndex) = Columns(5)

        LineIndex = LineIndex + 1
    Loop
End Using ' StreamReader closed ! XD

不行 不要問我使用FileStream 如果我知道文件中每個數據塊的大小,並且我正在使用固定大小的結構,那我就可以了。 FileStream是處理大型數據緩沖區時的最佳選擇,因為它可以通過適當的異步檢查直接在內存中工作。

只是看台詞? 不行 對於這一部分,請閱讀文檔


的String.Format()

Dim Row As String

MyListBox.Items.Clear()
For LineIndex As Int32 = 0 To P_CODE.Length - 1
    Row = String.Format("{0, -12}{1,-16}{2,10}{3,10}{4,8}{5,10}", _
              P_CODE(LineIndex), _
              P_DESCRIPT(LineIndex), _
              P_INDATE(LineIndex), _
              P_QOH(LineIndex), _
              P_MIN(LineIndex), _
              P_PRICE(LineIndex))
    MyListBox.Items.Add(Row)
Next

將文件寫入InventoryDataOut.txt。

Dim Separator As String = "|"
Dim FileContent As New StringBuilder() ' System.Text

' Prepare File content...
If P_CODE.Length > 0 Then
    For LineIndex As Int32 = 0 To P_CODE.Length - 2 ' Note the -2
        FileContent.AppendLine(String.Format("{0}{6}{1}{6}{2}{6}{3}{6}{4}{6}{5}", _
              P_CODE(LineIndex), _
              P_DESCRIPT(LineIndex), _
              P_INDATE(LineIndex), _
              P_QOH(LineIndex), _
              P_MIN(LineIndex), _
              P_PRICE(LineIndex), _
              Separator)))
    Next
    FileContent.Append(String.Format("{0}{6}{1}{6}{2}{6}{3}{6}{4}{6}{5}", _
              P_CODE(P_CODE.Length - 1), _
              P_DESCRIPT(P_CODE.Length - 1), _
              P_INDATE(P_CODE.Length - 1), _
              P_QOH(P_CODE.Length - 1), _
              P_MIN(P_CODE.Length - 1), _
              P_PRICE(P_CODE.Length - 1), _
              Separator)))

    ' This ensures we don't add unnecessary line at the end of the file
    ' which would make your application explode !

    ' best move is to check for null entries upon file parsing.
    ' (Again : File.ReadAllLines() + RemoveEmptyString parameter to keep it simple)

End If

' Create your file using the stream object 
' you're forced to use by your teacher,
' then dump the the content of your StringBuilder inside like
MyDummyStream.Write(FileContent.ToString())

' And close you dummy stream or use Using XD

您的老師是否有一堆類似CSV的文件,需要從空間到管道的分隔符轉換?

似乎他只是懶惰地編寫代碼以將幾列分派到幾個並行數組中以用於顯示或其他目的。 還是為什么在使用管道代替寫入內容時,在加載文件內容時使用空格作為分隔符?
如果是錯別字那就別再讀了。 否則,您的老師只是要您創建一個將空格分隔符轉換為管道的小程序...(同時給您學習操作(並行)數組,流I / O和String.Format的任務)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM