Populating an array

dawkirst

New member
Joined
May 25, 2006
Messages
2
Programming Experience
Beginner
Dear VB.NET programmers,

My form has two textboxes, and three buttons. With the first textbox and button the user can set the array's highest sub script. With the second textbox and button, the user can enter an X amount of "student marks" that needs to be read into the array. The third button should take the sum of all the array elements and print it to the screen.

My array isn't populating as it should: it makes all my first entries as zero, and take only the last entry into account -- what is wrong with my code? Sorry if it's a bit messy.

I have also attached the file.

Thanks ahead!



VB.NET:
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
ReDim intMarks(intCounter)
Dim i As Integer = 0
For i = j To intCounter - 1
intMarks(i) = Convert.ToInt16(Me.txtMarks.Text)
Next
j = j + 1
If j >= intCounter Then
MessageBox.Show("Array full")
End If
 

Attachments

  • ES8.zip
    26.4 KB · Views: 15
Hi Dawkirst,

I've had a quick peek at your code, I'm a little confused as to exactly what you're trying to do. However I'm guessing that the above code should perhaps look a bit more like:

VB.NET:
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
ReDim intMarks(intCounter)

If j >= intCounter Then
MessageBox.Show("Array full")
Else
intMarks(j) = Convert.ToInt16(Me.txtMarks.Text)
j = j + 1
End If

Although as I said I'm not eniterly sure what you're after.
The main advice I'd provide here is to use a Collection or Arraylist instead of your array. Because both of these are list based data structures you can add and remove from them to your hearts desire without having to worry about specifying dimensions for an array. For example:

VB.NET:
Dim MyArray as New Arraylist

Public Sub ClearArray()
MyArray.Clear()
End Sub

Public Sub AddGrade(ByVal Grade as integer)
MyArray.Add(Grade)
End Sub

Public Sub ShowGrades()

Dim TempGrade as integer

For each TempGrade in MyArray
Label1.Text &= TempGrade.ToString & " "
Next

End Sub

Hope this helps,

Regards,

Andy
 
You really should use NumericUpDown controls for the inputs. TextBoxes are very general and if you're dealing with numbers then a NumericUpDown is usually, although not always, a better choice. With a NumericUpDown there is no chance that the user can iput an non-numeric value, plus you can control the number of decimal places, which is good for restricting the value to an integer, as well as the minimum and maximum values.
 
Andy, thanks for taking the time.

I apologise for starting such a confusing thread, but I was quite desperate and in a hurry.

Collections and Arraylists are new concepts to me, but I'll definitely experiment with them. I like the sound of them being list based data structures.

jmcilhinney, thanks for the advice. NumericUpDown, too, is a new concept for me. Likewise, I'll experiment with them too.

All in all, I snapped what I was doing wrong and fixed my problem.

Thanks again!


Emile,
 
and fixed my problem
No no you are absolutelly wrong about this statement. Actually, your real problems just started in case you plan to have programming as primary occupation. Oh yeah ! Good luck
party.gif
 
Back
Top