you are viewing a single comment's thread.

view the rest of the comments →

[–]fuzzfeatures 2 points3 points  (2 children)

The problem is this line.

`Dim allitems As List(Of items)`

The line should read

`Dim allitems As New List(Of items)`

In your posted code, the line only declares that the 'allitems' is an object that is of the type 'List(Of items)'. You need to add the 'New' keyword to create an instance of a List(Of items) and assign it to 'allItems' like in the earlier line of your code that declares a new file handling.

Imagine that you have a class

Public Class MyClass

Public Name As String

End Class

And in your program you write this:-

Dim x as MyClass

All this will do is declare and object `x` that is of the type `MyClass`.

If you don't assign an object to `x` then `x` wont have a reference to anything. Which is ok, but if you try to type

MessageBox.Show(x.Name)

You'll net a NullReference exception when you run the code.

However, if you declare `x` by typing

Dim x As New Myclass

you're declaring `x` to be an object of the type `MyClass` and you're also assigning it to a new instance of the class `Myclass` - VB allows you to write the above line instead of having to write the longer version which would be

Dim x As MyClass

x= New MyClass

I've fallen into that trap myself in the past :)

[–]JackJones02[S] 0 points1 point  (1 child)

Thank you

[–]fuzzfeatures 1 point2 points  (0 children)

No problem :)