|
-- Posted by kame at 4:17 pm on June 24, 2004
hey guys i am writing a program but when i run it the program works and displays what i want once i have entered the info in but as soon as i have entered the info it displays an error. it says its to do with this line "lstDisplay.AddItem names(index) + Str$(marks(index))" this is all of my code Option Explicit Dim position As Integer Dim names(10) As String Dim marks(10) As String Private Sub cmdend_Click() End End Sub Private Sub cmdGo_Click() names(position) = txtInput marks(position) = txtmark position = position + 1 txtPosition = position If position > 10 Then MsgBox "Full", vbCritical End If Dim index As Integer lstDisplay.Clear For index = 1 To position lstDisplay.AddItem names(index) + Str$(marks(index)) Next End Sub Private Sub Form_Load() position = 1 txtPosition = position End Sub
-- Posted by sakurag at 8:01 pm on June 24, 2004
I don't know vb very well, but I think there might be a problem with the following lines: | Code: | ... position = position + 1 ...
|
I'm guessing after you enter in a name or something, you increment your position by 1. Then later you go onto adding items into some sort of list display container.
| Code: | For index = 1 To position lstDisplay.AddItem names(index) + Str$(marks(index)) Next
|
Now what I think is a problem is as follows. Because of the way you've implemented your position, it will always be + 1, everytime you get a new name. Now, I'm not sure, but I'm guessing VB initializes integers to 0. So say you start at 0 and you add 3 people, your count will be 0[first name] pos+1, 1[second name], pos+1, 2[third name], pos+1. At the end of this, position = 3. Now later, you start to add items, and you index from 1 - position, for this case, it's 3. That means you are indexing names(1), names(2), and names(3).. well, names(3) doesn't exist. You might want to try doing your for from 0 -> pos - 1 Or use an iterator. Note: IF VB indexes from 1->n and NOT 0->n-1, then you can fix this by incrementing position before you assign the text into the arrays.
-- Posted by imsocrazy at 3:57 pm on July 15, 2004
have you said what poistion is?
-- Posted by mephisto mortis at 5:29 pm on Sep. 3, 2004
only wussies use option explicit, but than again i dunno how to help you (forgot most of my vb) so i guess i am a wussie
-- Posted by ckings6056 at 3:45 am on Dec. 8, 2004
I havent used VB properly for god knows how long!!
|