Why doesn't this work?:
plz help :)Code:Dim sarray() As String = Nothing
Dim inc As Integer = -1
For each something in something
inc += 1
sarray.SetValue(something.value, inc) 'Error: Object reference not set to an instance of an object.
Next
Printable View
Why doesn't this work?:
plz help :)Code:Dim sarray() As String = Nothing
Dim inc As Integer = -1
For each something in something
inc += 1
sarray.SetValue(something.value, inc) 'Error: Object reference not set to an instance of an object.
Next
Because you are never creating an array. You are specifically stating that 'sarray' is Nothing and then you proceed to try to call SetValue on it. How can you call SetValue on Nothing?
Arrays are fixed-size objects. To create an array you have to specify its size and that size doesn't change. If you want your list to grow and shrink as required then you need to use a collection. To create a "dynamic array" you would use a generic List, e.g.vb.net Code:
Dim things As New List(Of String) For Each thing In someList things.Add(thing.Value) Next
thx!
kinda not directly related .... but...
i don't know about .net ... but i usually avoid dynamic arrays as i come from vb6 where with an array if you wanted to add an item to an array you would need to redim (preserve) the array ... this would create a new array (in memory) with the newly specified amount of items and then copy the contents of the old array to it then replace the old array with the new one then destroy the old array (in memory behind the scenes)... as you may understand doing this could potentially be a lot slower than using a collection ...
is this the same in .net? ... i guess i am asking is a list faster than an array to add / insert items to etc?
kris