|
-
Sep 7th, 2010, 03:20 PM
#1
Thread Starter
New Member
Collection error
Hi,
I am trying to build a class that works with a form, but I keep getting the dreaded "Object reference not set to an instance of an object" Error.
The form basically collects test grades and assigns them to a collection.
My class code is as follows:
Code:
Public Class Scores
Private colScoreCollection As New Collection
Public mintSum As Integer
Public mdecAverage As Decimal
Public mintMinimum As Integer
Public mintMaximum As Integer
Public mintNumberOfScores As Integer
Public Sub New()
colScoreCollection = Nothing
mintSum = 0
mdecAverage = 0
mintMinimum = 0
mintMaximum = 0
mintNumberOfScores = 0
End Sub
Public ReadOnly Property Sum() As Integer
Get
Return mintSum
End Get
End Property
Public ReadOnly Property Average() As Decimal
Get
Return mdecAverage
End Get
End Property
Public ReadOnly Property Minimum() As Integer
Get
Return mintMinimum
End Get
End Property
Public ReadOnly Property Maximum() As Integer
Get
Return mintMaximum
End Get
End Property
Public ReadOnly Property NumberOfScores() As Integer
Get
Return mintNumberOfScores
End Get
End Property
Public Sub AddScore(ByVal intGrades As Integer)
colScoreCollection.Add(intGrades)
End Sub
End Class
My form code is:
Code:
Imports TestScores
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub btnAddScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddScore.Click
Dim intGrade As Integer
intGrade = Convert.ToInt32(nudScores.Value)
gobjScores.AddScore(intGrade)
End Sub
End Class
I had a similar project I was working on that had a collection in it, but now that one is giving me the same error. It there something with my version of Visual Studio that is causing this error possibly?
Thanks for any help.
-
Sep 7th, 2010, 03:25 PM
#2
Re: Collection error
Look at the first line of code in your constructor. Now look at your error message. What do you see?
-
Sep 7th, 2010, 03:25 PM
#3
Re: Collection error
You need to:
Code:
gobjScores = New Scores
before you do:
Code:
gobjScores.AddScore(intGrade)
Not necessarily in the same place, just before.
VB6 Library
If I helped you then please help me and rate my post!
If you solved your problem, then please mark the post resolved
-
Sep 7th, 2010, 04:55 PM
#4
Re: Collection error
The error tells you that some object is Nothing. Sometimes it is hard to figure out which object is Nothing, but in this case you should be able to spot which one it is, mainly because you have typed it, literally, yourself.
EDIT
Besides, you should really be using a List(Of Integer) instead of a Collection. It works the same way except for the fact that you can only add Integers and that it returns Integers instead of Objects. It will make life easier for you down the line, while you don't have the change anything (besides the name 'Collection' to 'List(of Integer)') now.
Last edited by NickThissen; Sep 7th, 2010 at 05:01 PM.
-
Sep 8th, 2010, 01:46 PM
#5
Thread Starter
New Member
Re: Collection error
Thanks for the replies! I was under the impression that a constructor had to be created for the collection I needed to use, I guess this is false?
But wouldn't a constructor be necessary to create a new instance of the class using a collection? That is the part I'm a bit cloudy on at the moment.
I realize it would be wiser to use a sorted list or even an array, but I'm actually using an old text book from college (trying to get back into coding again), and this was an assignment I never had to do, and the assignment specifically called for a collection.
Thanks again for the help.
-
Sep 8th, 2010, 01:55 PM
#6
Re: Collection error
You have a few problems with your class design.
1) scores itself should BE a collection, not contain an internal collection
2) all your fields are public, and you also expose read only properties for them. If the fields are public, then can be directly accessed and modified outside the class, which means the readonly fields are useless and don't encapsulate the internal values of the class as they should
3) Your constructor is useless because all it does is set numeric variables to 0 which are actually already 0 since numeric value types have a default value of 0. If you don't specify a constructor (sub new) in a class, a parameterless one is automatically generated when compiled.
-
Sep 8th, 2010, 01:55 PM
#7
Re: Collection error
I looked at it again and the problem could also be with colScoreCollection. Nowhere in your code do I see you setting them.
You use a constructor when you wish to do more than just create an instance. For example, you can set an internal variable that causes the object to behave differently. Listen to Nick, he knows what he is talking about.
I don't know what you mean by
 Originally Posted by Snowblind47
But wouldn't a constructor be necessary to create a new instance of the class using a collection?
You can create a class. You can create a collection. You can have a constructor for either of them. It basically is like calling a method at the same time.
But before you use ANY object, you have to set them to something.
VB6 Library
If I helped you then please help me and rate my post!
If you solved your problem, then please mark the post resolved
-
Sep 8th, 2010, 03:23 PM
#8
Re: Collection error
 Originally Posted by Snowblind47
I was under the impression that a constructor had to be created for the collection I needed to use, I guess this is false?
But wouldn't a constructor be necessary to create a new instance of the class using a collection? That is the part I'm a bit cloudy on at the moment.
When you do not specify a 'Public Sub New' method (a constructor), a class has a default constructor which is 'implicit', eg hidden from the code. If you simply create an empty class:
Code:
Public Class SomeClass
End Class
then you can still create a new instance by invoking its constructor:
Code:
Dim c As New SomeClass()
This will call the default constructor, which does nothing at all except create a new instance.
When you want your constructor to do something, then you create your own. You could either set some initial values, or force the user to pass some object to your class in the constructor like this
Code:
Public Class SomeClass
Public Sub New(ByVal i As Integer)
' do something with i
End Sub
End Class
Now you use it as
Code:
Dim c As New SomeClass(3)
and the default (parameterless) constructor is now no longer available (so calling New SomeClass() without parameters is not allowed anymore).
 Originally Posted by Snowblind47
I realize it would be wiser to use a sorted list or even an array, but I'm actually using an old text book from college (trying to get back into coding again), and this was an assignment I never had to do, and the assignment specifically called for a collection.
Thanks again for the help.
I just saw you are using VS02/03 in which case i don't think a List(Of T) is available so you can disregard that comment, although really, you should upgrade to at least VS2008 or VS2010. The express versions are free and I doubt you would need any of the features the non-free professional versions offer at this point.
 Originally Posted by MarMan
You use a constructor when you wish to do more than just create an instance.
What you should have said is: you use a custom constructor (eg: create your own) if you wish to do more than just create an instance. The default parameterless constructor is always available and you always have to use it if you didn't specify your own, so even without creating your own Public Sub New you are always using a constructor (the default one).
It's just a minor 'correction' but since this is exactly the point the OP is having trouble with I thought I should point it out
Last edited by NickThissen; Sep 8th, 2010 at 03:28 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|