Small VB Quirk? (List.Add())

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Benny the Bear
Posts: 146
Joined: Sat Oct 27, 2007 2:44 am UTC
Location: Melbourne, Australia
Contact:

Small VB Quirk? (List.Add())

Postby Benny the Bear » Fri Jun 06, 2008 8:11 am UTC

Hi, I'm doing a small IT project for school where we're using VB. I'm pretty good in C#, so I figured I'd try get another .NET language under my belt.

Anyway, I have a list of 'Trucks' (objects of the truck class) and I can't seem to add anything to it. Here's the code:

Code: Select all

       
        Dim i As Integer = 0
        While i < 6
            'this will make us five trucks :)
            Dim newTruck As New Truck
            lstTrucks.Add(newTruck)
            AddHandler Me.Paint, AddressOf lstTrucks(i).PaintTruck
            i += 1
        End While


I've done something almost exactly the same in C#, I don't know why this doesn't work. I'm getting a NullReferenceException error when I try compile. This code is executed in a form's Load event.

The Truck class isn't an empty one.

Any help? I'm sure it's just a VB quirk I don't know about.

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Small VB Quirk? (List.Add())

Postby Rysto » Fri Jun 06, 2008 9:03 pm UTC

At some point in your program you need to do lstTrucks As New List, or something along those lines, don't you? I'm not a VB programmer but that seems to be your problem.

arcoain
Posts: 56
Joined: Thu Dec 20, 2007 12:34 am UTC

Re: Small VB Quirk? (List.Add())

Postby arcoain » Sat Jun 07, 2008 7:47 pm UTC

I am a VB programmer, of the "wrote the book" level.
Anyway, Rysto almost certainly has the answer. There aren't any quirks that I am familiar with to that effect.
Russell

zahlman
Posts: 638
Joined: Wed Jan 30, 2008 5:15 pm UTC

Re: Small VB Quirk? (List.Add())

Postby zahlman » Mon Jun 09, 2008 11:44 pm UTC

Benny the Bear wrote:

Code: Select all

       
        Dim i As Integer = 0
        While i < 6
            'this will make us five trucks :) <---
            i += 1



No, it won't. It will make six trucks. The first one corresponds to i = 0, the next to i = 1, so on up to i = 5 (the last value which is strictly less than 6). That makes six of them.

This is the normal way of "counting" for programmers, starting at zero and counting up to strictly-less-than a target value. It makes the math simpler in the long run. You will learn to love it, if you expect to go far as a programmer. :)
Belial wrote:I once had a series of undocumented and nonstandardized subjective experiences that indicated that anecdotal data is biased and unreliable.

fatnickc
Posts: 7
Joined: Wed Aug 01, 2007 5:50 pm UTC
Location: London, UK

Re: Small VB Quirk? (List.Add())

Postby fatnickc » Tue Jun 10, 2008 8:35 pm UTC

If we were on The Daily WTF, somebody would be sure to say 'the real quirk is VB'.
But we're not. Oh well.
As you were.

User avatar
Benny the Bear
Posts: 146
Joined: Sat Oct 27, 2007 2:44 am UTC
Location: Melbourne, Australia
Contact:

Re: Small VB Quirk? (List.Add())

Postby Benny the Bear » Fri Jun 13, 2008 7:34 am UTC

Ok guys, an explanation:

Rysto was right and the problem was fixed. I pm'd him a few days ago saying thanks. I didn't post it here because I thought the thread was about to die and didn't want to revive it back to page 1.

Zahlman, you're right, although I knew that. I changed the number at some point and didn't update the comment. I'm actually an intermediate hobbyist programmer. Thanks for the help, anyway.

Can this be closed?

The exact problem was the list was 'As List(Of T)' and not 'As New List(Of T)'

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Small VB Quirk? (List.Add())

Postby EvanED » Fri Jun 13, 2008 2:21 pm UTC

Benny the Bear wrote:Can this be closed?

Yeah, as a rule I don't usually like to close threads since further updates are hard to reconcile with the forum's rule to not make new threads on old topics, but this sort of help thread is specific enough that this situation is probably not likely to arise.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 3 guests