2012-10-06 43 views
1

我正在爲我的VB類做一個任務,我真的很難過,我非常感謝一些幫助,指針和/或示例。我不期望只給出確切的答案,但類似的例子會非常好。在Visual Basic(2010)任務中使用數組

CSV文件有5列,每列有25行。主要的是我需要採取最後兩列,並計算最後兩行的標記,它們都是十進制整數,並將其放在輸出中。換句話說,我需要將兩列合併,並將計算結果放在該列中。

這裏是分配:

應用程序標題:庫存控制

目的:

此Windows MDI應用程序允許用戶顯示整個清單,計算銷售價格,或以存貨的價格顯示庫存中的選定項目。

步驟:

從主文檔的用戶會選擇要麼全部或從菜單中選擇。適當的形式將顯示在MDI窗口中。如果用戶選擇了全部,則清單項目將簡單地出現,並以最後一欄中的銷售價格排列。如果使用選擇了選擇,則會出現一個選中的列表框,允許用戶選擇她希望看到的清單項目。然後所選項目將顯示在最後一欄的銷售價格欄中。用戶應該可以使用File Exit菜單項退出程序。

的算法,處理,和條件:

  1. 用戶選擇他們想要如何從顯示菜單
  2. 顯示清單的適當的形式被裝載到MDI
  3. 該程序從文件中讀取數據
  4. 程序通過將成本乘以1加上加價百分比來計算銷售價格。
  5. 程序格式化標題和細節行,並在列表框中顯示列中的信息。

計劃要求:

  1. 必須使用多文檔界面
  2. 必須使用一個類的存量項目
  3. 必須使用在父MDI形式
  4. 必須在菜單至少使用1個數組而不是您在讀入記錄時使用的strRec數組。
  5. 可以使用數組列表和列表來處理數據。

這裏是我到目前爲止的代碼:

Public Class frmMain 

Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click 
    'Close the program 
    Me.Close() 
End Sub 

Private Sub mnuHelpAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHelpAbout.Click 
    'Simple help box 
    MessageBox.Show("Inventory control program. Version 1.0") 
End Sub 


Private Sub mnuInvenListAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuInvenListAll.Click 
    'Create the child form the form 
    Dim mdiChild1 As New frmAll 
    'Set form as parent. 
    mdiChild1.MdiParent = Me 
    'display the form as Show 
    mdiChild1.Show() 
End Sub 

Private Sub mnuInvenSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuInvenSelect.Click 
    'Create the child for the form 
    Dim mdiChild2 As New frmSelect 
    'Set form as parent. 
    mdiChild2.MdiParent = Me 
    'display the form as Show 
    mdiChild2.Show() 
End Sub 

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Me.LayoutMdi(MdiLayout.TileHorizontal) 
End Sub 

End Class 


=-=-=-=-= 


Imports System.IO 
Imports System.Convert 

Public Class frmAll 
'Declare Streamreader 
Private objReader As StreamReader 


'Declare arrays to hold the information 
Private strNumber(24) As String 
Private strName(24) As String 
Private strSize(24) As String 


Private Sub frmAll_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    'Set objReader 
    objReader = New StreamReader("products.csv") 
    Call FillArray() 
End Sub 

Private Sub FillArray() 
    'Declare variables and arrays 

    Dim decCost(24, 1) As Decimal 
    Dim strFields() As String 
    Dim strRec As String 
    Dim intCount As Integer = 0 
    Dim chrdelim As Char = ToChar(",") 
    'Set strRec to read the lines 

    strRec = objReader.ReadLine 

    'Do while loop to fill array. 
    Do While strRec <> Nothing 
     strFields = strRec.Split(chrdelim) 
     strNumber(intCount) = strFields(0) 
     strName(intCount) = strFields(1) 
     strSize(intCount) = strFields(2) 
     decCost(intCount, 0) = ToDecimal(strFields(3)) 
     decCost(intCount, 1) = ToDecimal(strFields(4)) 
     'Set strRec to read the lines again 
     strRec = objReader.ReadLine 
     'increment the index 
     intCount += 1 
    Loop 
    Call Calculate(decCost) 
End Sub 

Private Sub Calculate(ByVal numIn(,) As Decimal) 
    'Define arrays to hold total cost 
    Dim decRowTotal(24) As Decimal 

    'Define variables to hold the counters for rows and columns 
    Dim intR As Integer 
    Dim intC As Integer 

    'Calcualte total cost 
    For intC = 0 To numIn.Length 
     For intR = 0 To decRowTotal.Length 
      decRowTotal(intR) += numIn(intR, intC) * 1 
     Next 
    Next 

    Call Output(numIn, decRowTotal) 

End Sub 

Private Sub Output(ByVal NumIn(,) As Decimal, ByVal RowTotalIn() As Decimal) 
    Dim strOut As String 

    Dim intR As Integer = 0 
    Dim intC As Integer = 0 

    strOut = "ID" & vbTab & "Item" & vbTab & "Size" & vbTab & "Total Price" & vbCrLf & vbCrLf 

    For intR = 0 To 24 
     strOut &= strNumber(intC) & vbTab 
     strOut &= strName(intC) & vbTab 
     strOut &= strSize(intC) & vbTab 

     For intC = 0 To 1 
      strOut &= NumIn(intR, intC).ToString 
     Next 

     strOut &= vbTab & RowTotalIn(intR) & vbCrLf 
    Next 

    rtbAll.Text = strOut 

End Sub 

End Class 


-=-=-=-=-= 


'Imports 
Imports System.IO 
Imports System.Convert 

Public Class InventoryItems 

'Declare ItemList Array 
Private ItemList As New ArrayList 
'IItem declared as new Object. 
Private IItem As New Object 

Public Function Reader() As ArrayList 
    'Declare variables for reading the file. 
    Dim chrDelim As Char = Convert.ToChar(",") 
    Dim strRec As String 
    Dim strFields() As String 
    Dim objReader As StreamReader 
    objReader = New StreamReader("products.csv") 
    strRec = objReader.ReadLine 
    'Declares a new instance of the InvenItems class 
    'and stores each of the items in their own instance 
    'of the class 
    Do While strRec <> Nothing 
     IItem = New InvenItems 
     strFields = strRec.Split(chrDelim) 
     IItem.Number = strFields(0) 
     IItem.Name = strFields(1) 
     IItem.Size = strFields(2) 
     IItem.Price = ToDecimal(strFields(3)) 
     IItem.MarkUp = ToDecimal(strFields(4)) 
     ItemList.Add(IItem) 
     strRec = objReader.ReadLine 
    Loop 

    Return ItemList 
End Function 

Public Class InvenItems 
    'Declare class variables. 
    Private strNumber As String 
    Private strName As String 
    Private strSize As String 
    Private decCost As Decimal 
    Private decMarkUp As Decimal 
    'Create constructor 
    Public Sub New() 

    End Sub 
    'Create override function 
    Public Overrides Function ToString() As String 
     Return strNumber 
    End Function 
    'Create property for Number. 
    Public Property Number As String 
     Set(ByVal value As String) 
      strNumber = value 
     End Set 
     Get 
      Return strNumber 
     End Get 
    End Property 
    'Create property for Name. 
    Public Property Name As String 
     Set(ByVal value As String) 
      strName = value 
     End Set 
     Get 
      Return strName 
     End Get 
    End Property 
    'Create property for size. 
    Public Property Size As String 
     Set(ByVal value As String) 
      strSize = value 
     End Set 
     Get 
      Return strSize 
     End Get 
    End Property 
    'Create property for cost. 
    Public Property Cost As Decimal 
     Set(ByVal value As Decimal) 
      decCost = value 
     End Set 
     Get 
      Return decCost 
     End Get 
    End Property 

    Public Property MarkUp As Decimal 
     Set(ByVal value As Decimal) 
      decMarkUp = value 
     End Set 
     Get 
      Return decMarkUp 
     End Get 
    End Property 

End Class 

End Class 

感謝任何指針,建議和實例。

+0

您遇到什麼問題?計算,表單設置,錯誤? – Hannele

+0

當我運行All窗體時它會停止。它會運行,直到我到達第一個For循環。當它貫穿整個過程時,它就結束了。此外,我相信我的循環錯了,而我的數組錯了,但我不確定要設置那些內容。就像我說的,我仍然在陣列上。 – Brodoin

+0

第一個for循環,你是否指'Calculate'中的那個?你可以在那裏設置一個斷點來確保'numIn'包含你所期望的嗎? – Hannele

回答

0

使用Option Strict On

這將告訴你,你需要在InventoryItems.Reader中使用Dim IItem = New InvenItems,這將顯示另一個問題。

+0

我會那樣做的。還有什麼其他問題?我在一個論壇上發佈了這個,一個人說我的循環,特別是我的For循環,完全關閉。 – Brodoin

+0

其他問題可供您查找;)然而,問題的IMO第4部分應爲「該程序通過將成本乘以(1加上加價分數)來計算售價。」 –

+0

@Brodoin提示:frmAll不使用InventoryItems類。 –