简单的初学者问题在这里,轻松一点。我有几个文本框,用户可以将值放入+选择日期,我希望它们出现在列表框中。不幸的是,只有第二个文本框的值出现多次。这可以在这里看到: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值。
答案 0 :(得分: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
正如您所看到的那样,以这种方式进行操作会更加自我记录,减少混淆,并且不易出错。