0

简单的初学者问题在这里,放轻松。我有几个文本框,用户可以将值放入 + 选择日期,我希望它们出现在列表框中。不幸的是,只有第二个文本框的值出现多次。这可以在这里看到:http: //i.stack.imgur.com/kCqrz.png

这是完整的表格代码: http: //pastebin.com/MDb1hSCA

这是将数据添加到数组的位置:

stockArray(nofDataDay, lowValue) = possibleLow
stockArray(nofDataDay, highValue) = possibleHigh
stockArray(nofDataDay, openValue) = possibleOpen
stockArray(nofDataDay, closeValue) = possibleClose
dateArray(nofDataDay) = Convert.ToDateTime(WeatherDateTimePicker.Text)
nofDataDay = nofDataDay + 1

这是它的显示位置:

For day = 0 To nofDataDay - 1
    StockListBox.Items.Add(dateArray(day).ToShortDateString & _
        delimiter & stockArray(day, openValue).ToString & _
        delimiter & stockArray(day, closeValue).ToString & _
        delimiter & stockArray(day, highValue).ToString & _
        delimiter & stockArray(day, lowValue).ToString & _
        delimiter & AverageStock(stockArray(day, lowValue), stockArray(day, highValue)))
Next

出于某种原因,它只是添加了 Close 值。

4

1 回答 1

0

您永远不会设置列索引变量的值(即openValue, closeValue, highValue, lowValue)。它们都默认为零,因此您只需多次添加第一列。您可以在声明它们时设置它们的值,如下所示:

Dim lowValue As Integer = 0
Dim highValue As Integer = 1
Dim openValue As Integer = 2
Dim closeValue As Integer = 3

您还需要将数组声明为更大:

Dim stockArray(30, 3) As Integer

但是,默认情况下Dim将字段声明为公共,并且由于这可能不是您真正想要的,我建议将它们更改为私有。此外,列索引确实应该是常量:

Private Const lowValue As Integer = 0
Private Const highValue As Integer = 1
Private Const openValue As Integer = 2
Private Const closeValue As Integer = 3
Private stockArray(30, 3) As Integer

但是,这种错误不可能让您更好地设计代码。我建议不要使用二维数组,而是创建一个类来存储单个项目的所有数据。然后,使用List(T)对象而不是数组来存储项目列表。例如:

Public Class MyItem
    Public Date As Date
    Public LowValue As Integer
    Public HighValue As Integer
    Public OpenValue As Integer
    Public CloseValue As Integer
End Class

Private myItems As New List(Of MyItem)()

然后,您可以像这样添加项目:

Dim item As New MyItem()
item.Date = Convert.ToDateTime(WeatherDateTimePicker.Text)
item.LowValue = possibleLow
item.HighValue = possibleHigh
' ...
myItems.Add(item)

然后你可以像这样从列表中读取项目:

For Each item As MyItem in myItems
    StockListBox.Items.Add(item.Date.ToShortDateString() & _
            delimiter & item.OpenValue.ToString() & _
            delimiter & item.CloseValue.ToString() & _
            delimiter & item.HighValue.ToString() & _
            delimiter & item.LowValue.ToString() & _
            delimiter & AverageStock(item.LowValue, item.HighValue))
Next

正如你所看到的,这样做更能自我记录,更少混乱,更不容易出错。

于 2012-12-21T20:36:23.167 回答