14 Replies - 5701 Views - Last Post: 19 March 2009 - 08:42 PM Rate Topic: -----

#1 Tocon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-November 08

How to increment my array

Posted 19 March 2009 - 04:57 PM

[quote]I am getting a "NULL VALUE error in the output line in the sub displayArray. I'm told it is because my array isn't set up to increment. I took a stab at incrementing it but not quite sure how it should be written. Also, the sample program I started with displayed array contents in a messagBox. I need to display in my label box. Can't find any good examples of that either. HELP[quote]
Public Class MainForm1

Dim gender As String
Dim grade As String

Private studentList As Array = Array.CreateInstance(GetType(Student), 5)

Private Sub ButtonAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click

'Dim j As Integer
For j as Integer
studentList.SetValue(New Student(TextBoxLast.Text, TextBoxFirst.Text, gender, TextBoxDOB.Text, grade), j)

'User selection
If RadioButtonMale.Checked = True Then
gender = "Male"
ElseIf RadioButtonFemale.Checked = True Then
gender = "Female"
End If
j += 1
Next
'For j = 0 To studentList.Length + 1
'GreekOut.Text &= Letters(i) & " "
'Next j


End Sub
Private Sub ComboBoxGrade_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxGrade.SelectedValueChanged
grade = ComboBoxGrade.SelectedItem.ToString
End Sub

Private Sub ButtonList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonList.Click
Dim output As String = Nothing
displayArray(studentList, "Student List")
End Sub
Private Sub displayArray(ByVal theArray, ByVal theCaption)
Dim output As String = ""
For i As Integer = _
theArray.GetLowerBound(0) To theArray.GetUpperBound(0)
output &= theArray(i).ToString & vbCrLf 'THIS IS WHERE IT GETS HUNG UP. "NULL VALUE ERROR" WHAT IS WRONG?
Next i
MessageBox.Show(output, theCaption) 'WANT TO WRITE TO LabelList my list box. How?
End Sub
End Class

Public Class Student

Implements IComparable 'required for Array.Sort

'instance variables
Private _last As String
Private _first As String
Private _gender As String
Private _dateOfBirth As String
Private _grade As String

'constructor
Public Sub New(ByVal LastName As String, _
ByVal FirstName As String, _
ByVal Gender As String, _
ByVal DOB As String, _
ByVal Grade As String)
_last = LastName
_first = FirstName
_gender = Gender
_dateOfBirth = DOB
_grade = Grade

End Sub
Public Overrides Function ToString() As String
Return _first & ", " & _last & ", " & _gender & ", " & _dateOfBirth & ", " & _grade
End Function

Public Overrides Function Equals(ByVal anObject As Object) _
As Boolean
'object to which Me is compared converted to Person
Dim otherPerson As Student = DirectCast(anObject, Student)
'represent both objects as strings
Return String.Equals(Me.ToString, otherPerson.ToString)
End Function

Public Function CompareTo(ByVal anObject As Object) As Integer _
Implements System.IComparable.CompareTo
'object to which Me is compared converted to Person
Dim otherPerson As Student = DirectCast(anObject, Student)
'represent both objects as strings
Return String.Compare(Me.ToString, otherPerson.ToString)
End Function

End Class

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: How to increment my array

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,418
  • Joined: 29-May 08

Re: How to increment my array

Posted 19 March 2009 - 05:24 PM

Set a breakpoint on the line Private Sub displayArray(ByVal theArray, ByVal theCaption)
Then step through it checking the contents of variables.
Was This Post Helpful? 0
  • +
  • -

#3 Tocon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-November 08

Re: How to increment my array

Posted 19 March 2009 - 05:38 PM

View PostAdamSpeight2008, on 19 Mar, 2009 - 04:24 PM, said:

Set a breakpoint on the line Private Sub displayArray(ByVal theArray, ByVal theCaption)
Then step through it checking the contents of variables.


[quote]OK. That showed me there is a syntax error at the end of "For j as Integer" line 11. How should this be written? Does it need a value to instantiate it? Like For j as "Integer = 0" cause that doesn't work?
Was This Post Helpful? 0
  • +
  • -

#4 Damage  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 27
  • Posts: 1,081
  • Joined: 05-June 08

Re: How to increment my array

Posted 19 March 2009 - 05:49 PM

for i as integer = 0 to 200
blah blah blah
next


Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,418
  • Joined: 29-May 08

Re: How to increment my array

Posted 19 March 2009 - 05:54 PM

The problem is that contents of studentList(0)=Nothing.
Was This Post Helpful? 0
  • +
  • -

#6 Tocon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-November 08

Re: How to increment my array

Posted 19 March 2009 - 06:01 PM

View PostDamage, on 19 Mar, 2009 - 04:49 PM, said:

for i as integer = 0 to 200
blah blah blah
next


Quote

Thanks. That got me past that part of the problem but now I have "Index was outside the bounds of the array." for an error on the set value to the array line.

For j As Integer = 0 To 200
studentList.SetValue(New Student(TextBoxLast.Text, TextBoxFirst.Text, gender, TextBoxDOB.Text, grade), j)
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,418
  • Joined: 29-May 08

Re: How to increment my array

Posted 19 March 2009 - 06:04 PM

How many entries did you define the array to contain? (Hint it isn't 200)
Was This Post Helpful? 0
  • +
  • -

#8 Arazons  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-March 09

Re: How to increment my array

Posted 19 March 2009 - 06:09 PM

Arrays are a 0 based Index... *cough* (UpperLimit - 1) *cough*
Was This Post Helpful? 0
  • +
  • -

#9 Tocon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-November 08

Re: How to increment my array

Posted 19 March 2009 - 06:57 PM

View PostArazons, on 19 Mar, 2009 - 05:09 PM, said:

Arrays are a 0 based Index... *cough* (UpperLimit - 1) *cough*

Quote

Sure, meaning I have 5 items in the array so the upperlimit is 4. I set that now the error has jumped back the displayArray sub again."Object reference not set to an instance of an object."


output &= theArray(i).ToString & vbCrLf 'THIS IS WHERE IT GETS HUNG UP.
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,418
  • Joined: 29-May 08

Re: How to increment my array

Posted 19 March 2009 - 07:02 PM

See Post:5
Was This Post Helpful? 0
  • +
  • -

#11 Damage  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 27
  • Posts: 1,081
  • Joined: 05-June 08

Re: How to increment my array

Posted 19 March 2009 - 07:16 PM

View PostArazons, on 19 Mar, 2009 - 05:09 PM, said:

Arrays are a 0 based Index... *cough* (UpperLimit - 1) *cough*

catches me every time :P
Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,418
  • Joined: 29-May 08

Re: How to increment my array

Posted 19 March 2009 - 07:20 PM

In OP code they have used
For i As Integer = theArray.GetLowerBound(0) To theArray.GetUpperBound(0)


Which in the 5 element array they create is
For i As Integer = 0 To 4


This post has been edited by AdamSpeight2008: 19 March 2009 - 07:20 PM

Was This Post Helpful? 0
  • +
  • -

#13 Tocon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-November 08

Re: How to increment my array

Posted 19 March 2009 - 07:39 PM

View PostAdamSpeight2008, on 19 Mar, 2009 - 06:20 PM, said:

In OP code they have used
For i As Integer = theArray.GetLowerBound(0) To theArray.GetUpperBound(0)


Which in the 5 element array they create is
For i As Integer = 0 To 4


Quote

See post 9. still have the same problem. I removed the upbound line and placed the 0 To 4 in there. I had this written without the Student class yesterday and the radio buttons weren't creating any output. Is it possible that if they are not now creating any output that the array count is changing somehow?

Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,418
  • Joined: 29-May 08

Re: How to increment my array

Posted 19 March 2009 - 08:32 PM

Set a breakpoint on this line
output &= theArray(i).ToString & vbCrLf 'THIS IS WHERE IT GETS HUNG UP. "NULL VALUE ERROR" WHAT IS WRONG?



And move the mouse over this theArray(i) part of the line.
Right Click -> Quick Watch.
This will open another window from which you see the contents of the array element.
What is it?
Was This Post Helpful? 0
  • +
  • -

#15 Tocon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-November 08

Re: How to increment my array

Posted 19 March 2009 - 08:42 PM

View PostAdamSpeight2008, on 19 Mar, 2009 - 07:32 PM, said:

Set a breakpoint on this line
output &= theArray(i).ToString & vbCrLf 'THIS IS WHERE IT GETS HUNG UP. "NULL VALUE ERROR" WHAT IS WRONG?



And move the mouse over this theArray(i) part of the line.
Right Click -> Quick Watch.
This will open another window from which you see the contents of the array element.
What is it?

[quote]Didn't know you could do that. It showed everything was added all the variables were there but th gender value of the radio buttons was "Nothing". As I suspected. I wouldn't think that would stop it from running though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1