2017-10-08 3 views
0
Module Module1 

'input vars declarations 
Private DescriptionString As String 
Private StockNumberString As String 
Private WholesalePriceDecimal As Decimal 
Private QuantityOHDecimal As Decimal 

'CONSTANT VAR 
Private Const PROFIT_PERCENT_DECIMAL As Decimal = 0.25 

'calculated vars declarations 
Private InvValueDecimal As Decimal 'this is WholesalePrice * QOH 
Private PotentialProfit As Decimal 'this is InvValue * PROFIT_PERCENT 
Private PotentialIncome As Decimal 'this is InvValue + PotentialProfit 

'declares the record variable and associates file name 
Private CurrentRecord() 
Private FurnitureInventoryFile As New FileIO.TextFieldParser("FURNFILE.TXT") 

'Main module is Level 1 
Sub Main() 

    'L1 calls L2 

    Call Housekeeping() 

    Do While Not FurnitureInventoryFile.EndOfData 
     Call ProcessRecords() 
    Loop 

    Call EndOfJob() 

End Sub 

'L2 Housekeeping() calls L3 SetFileDelimiter() and L3 WriteHeadings() 
Private Sub Housekeeping() 

    Call SetFileDelimiter() 
    Call WriteHeadings() 

End Sub 

'L2 ProcessRecords() calls L3 ReadFile(), L3 DetailCalculations() and L3 WriteDetailLine() 
Private Sub ProcessRecords() 

    Call ReadFile() 
    Call DetailCalculations() 
    Call WriteDetailLine() 

End Sub 

'L2 EndOfJob() calls L3 SummaryOutput() and L3 CloseFile() 
Private Sub EndOfJob() 

    Call SummaryOutput() 
    Call CloseFile() 

End Sub 

'L3, Called by Housekeeping(), Assigns filetype as delimited and assigns the delimiter as a comma 
Private Sub SetFileDelimiter() 

    FurnitureInventoryFile.TextFieldType = FileIO.FieldType.Delimited 
    FurnitureInventoryFile.SetDelimiters(",") 

End Sub 

'L3, Called by Housekeeping(), Writes the report heading and column heading lines 
Private Sub WriteHeadings() 

    'Report Headings 
    Console.WriteLine(Space(17) & "Inventory Report for Quality Furniture Store") 
    Console.WriteLine(Space(28) & "Christian VanderHorst") 

    '2 Blank lines 
    Console.WriteLine() 
    Console.WriteLine() 

    'Column Headings 
    Console.WriteLine(Space(22) & "Stock" & Space(8) & "Wholesale" & Space(2) & "Inventory" & Space(2) & "Potential" & Space(3) & "Potential") 
    Console.WriteLine(Space(1) & "Description" & Space(10) & "Number" & Space(2) & "QOH" & Space(6) & "Price" & Space(6) & "Value" & Space(5) & "Profit" & Space(6) & "Income") 

End Sub 

'L3, Called by ProcessRecords(), Reads the file and parses the current record into its vars 
Private Sub ReadFile() 

    CurrentRecord = FurnitureInventoryFile.ReadFields() 'reads the record 

    StockNumberString = CurrentRecord(0) 
    DescriptionString = CurrentRecord(1) 
    WholesalePriceDecimal = CurrentRecord(2) 
    QuantityOHDecimal = CurrentRecord(3) 

End Sub 

'L3, Calculates InvValueDecimal, PotentialProfitDecimal and PotentialIncomeDecimal 
Private Sub DetailCalculations() 

    InvValueDecimal = WholesalePriceDecimal * QuantityOHDecimal 
    PotentialProfit = InvValueDecimal * PROFIT_PERCENT_DECIMAL 
    PotentialIncome = InvValueDecimal + PotentialProfit 

End Sub 

'L3, Writes the detail lines 
Private Sub WriteDetailLine() 

    Console.WriteLine(Space(1) & DescriptionString.PadRight(19) & 
         Space(2) & StockNumberString & 
         Space(4) & QuantityOHDecimal.ToString("N0").PadLeft(2) & 
         Space(3) & WholesalePriceDecimal.ToString("N").PadLeft(8) & 
         Space(2) & InvValueDecimal.ToString("N").PadLeft(9) & 
         Space(5) & PotentialProfit.ToString("N").PadLeft(5) & 
         Space(2) & PotentialIncome.ToString("C").PadLeft(10)) 

End Sub 

'L3, Writes the summary line and pauses program to wait for enter keystroke 
Private Sub SummaryOutput() 

    'Blank space to move the summary line down 
    Console.WriteLine() 
    Console.WriteLine() 

    'the summary line 
    Console.WriteLine(Space(24) & "End of Furniture Profit Report") 

    'prompt to tell the user how to close the report 
    Console.WriteLine() 
    Console.WriteLine() 
    Console.WriteLine("Press ENTER to close the report") 
    Console.ReadLine() 

End Sub 

'L3, Closes the input file 
Private Sub CloseFile() 

    FurnitureInventoryFile.Close() 

End Sub 

End Moduleexception non gérée "System.IndexOutOfRangeException"

Ceci est le code complet créé ive. Tout fonctionne très bien, cependant, après la sortie 3 lignes de détail, il arrive avec

Une exception non gérée du type « System.IndexOutOfRangeException » est produite dans My_Name.InvReportFurniture.exe

Informations complémentaires: index était en dehors les limites du tableau.

et précise la déclaration

QuantityOHDecimal = CurrentRecord (3)

comme source

Je ne peux pas l'air de comprendre ce que la cause est. Si je mets une pause entre chaque écriture de ligne de détail, cela se passe de la même manière en lançant cette exception après la 3ème ligne de détail.

Voici le fichier de données.

ET010,3 tiroir tableau End, 169,00, 10
ET020,4 tiroir tableau End, 299,95, 05
ET030,8 tiroir tableau End, 148,99, 12
ST010, Table empilable 325,50, 20
BC020,4 Shelf Case Book, 130,99, 24
BC030,5 Shelf Case Book, 199,95, 08
BC040,6 Shelf Case Book, 270,99, 15
LC010, Chaise longue, 198,89, 20
VC010, Wingback Président, 320.55, 05
LC011, Pleather Recliner, 1050,00, 02
FC010, Tissu Recliner, 755,00, 04
VC015, fauteuil inclinable en cuir, 500,01, 10
BS010, Stand livre, 235,80, 05
LS010, Pied de lampe, 135,23, 15
PS010, stand de téléphone, 99,95, 19
TS010, TV stand, 270,90, 08
SS010, système stéréo, 675,00, 15
TS015, Tiffany stand, 1115,35, 02

+2

Vous devez définir 'Option Strict On'. En outre, les classes et les collections simplifieraient grandement le code – Plutonix

Répondre

3

C'est parce que la ligne avec ST010 manque un délimiteur de champ de virgule. Vous n'avez que trois champs dans cette ligne, donc l'index de champ 3 (quatrième champ) n'existe pas.

+0

Gah! Tu me frappes aussi! –