Problems with an array

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
Hi all, i am having problems with an array, as afar as i can tell the code im using is pretty simple and should be working... but its not.

breif: i have a class called car which has a string array in it 'Damage()'
the array get filled with item from a listbox using this code

VB.NET:
For i = 0 To CheckedListBox_Car.Items.Count - 1
            car1.damage(i) = (CheckedListBox_Car.Items.Item(i))
        Next[/CODE}
but i am getting this error.
Object reference not set to an instance of an object.
i honestly dont understand what exactly this means, but when i add a fixed value to the array eg damage(10) it works
 CheckedListBox_Car does have items as i does have a value, so i dont know how to fix this
 
The problem is that the array "car1.damage" is not set to an instance of an object, in other words it is nothing. When you add fixed value to the array, it then becomes an array of blank values instead of nothing. There is probably a simpler way to do this, but I know that something like this would work:

VB.NET:
Dim lstDamage As New List(Of String)
For i = 0 To CheckedListBox_Car.Items.Count - 1
    lstDamage.Add(CheckedListBox_Car.Items(i))
Next
car1.damage = lstDamage.ToArray
 
ow my saviour cames to the rescue again lol, this is starting to be like a damsel indestress you the mighty warrior LMFAO, cherz i get what u mean because the array does have the fixed value it tecnically doesn't exist.
but this works because it isn't relying on a value to tell it where in the array to go, the list basicly becomes the array
car1.damage = lstDamage.ToArray

cherz dude
 
but this works because it isn't relying on a value to tell it where in the array to go, the list basicly becomes the array
car1.damage = lstDamage.ToArray

Right, but it should work out the same, i is going from 0 to CheckedListBox_Car.Items.Count - 1. And the list is going to be from 0 to CheckedListBox_Car.Items.Count - 1, since that is the number of items added, so when you say "car1.damage = lstDamage.ToArray", it should come out the same. The other option is to ReDim the array like so:

VB.NET:
ReDim car1.damage(CheckedListBox_Car.Items.Count)
For i = 0 To CheckedListBox_Car.Items.Count - 1
    car1.damage(i) = (CheckedListBox_Car.Items.Item(i))
Next
 
Excelent the code worked perfectly, like any advice or code you have give to me so far thanks
+rep

EDIT: just to let you know this code worked fine
VB.NET:
Dim lstDamage As New List(Of String) For i = 0 To CheckedListBox_Car.Items.Count - 1     lstDamage.Add(CheckedListBox_Car.Items(i)) Next car1.damage = lstDamage.ToArray
 
:) Glad to help.
 
Back
Top