Feels dumb to ask, but...
When I create arrays...I always create a counter variable to keep track of its length. I do this because...I kept getting null reference exceptions and that was the easiest fix...but I guess it's a (very minor?) performance issue..and the code just doesn't look as good.? Most of my stuff looks like this...
Code:
Dim rtnArry(0) as Uint32
Dim rtnCounter as Int32 = 0
For xx as Int32 = 0 to 50
If rtnCounter = 0 then
rtnArry(0) = 420
rtnCounter = 1
Else
redim preserve rtnArry(rtnCounter)
rtnArry(rtnCounter) = (uint32)New Random().Next
rtnCounter +=1
End If
Next xx
I tried using UBOUND(rtnArry)..but i kept adding an extra item at the end of the array ..or I'd not instantiate¿ it and get nullreference.
Question: The problem I get stuck on is when the array has 0 elements..i can't seem to correctly set (0) and not resize +1 until needed. So..how do I do it..it must be some simple concept I'm just not seeing..I know about ubound etc...someone must have had this same roadblock? Or..should I just keep using a counter? Performance wise it's only 1 more int32, ..and a lot of IF() each time I add to it...but generally the list stays smaller than 100 so it's not really called a lot. Thanks.
ps. As a secondary question..when I 'clear' my arrays used like this..I just redim (0) and set the counter to 0..any thoughts? (ie. when I re-load a path file..in PathFile_Load() I first array.clear, then load.
----------------------------------------
this is how I *tried* doing it..but couldn't figure it out
Code:
Dim rtnArry(0) as Single
For xx as Int32 = 0 to 50
rtnArry(ubound(rtnArry)) = new Random().Next
redim preserve rtnArry(ubound(rtnArry) +1) 'always adds an extra item to the list...
Next xx
this works, and it looks sexy because there's no If, but personally I think the counter method looks better. ??
---last option?--
Code:
Dim rtnArr(0) As UInt32
For xx As Int32 = 0 To 50
If UBound(rtnArr) = 0 Then
rtnArr(0) = 420
ReDim Preserve rtnArr(UBound(rtnArr) + 1) ' (1)
Else
rtnArr(UBound(rtnArr)) = Convert.ToUInt32(New Random().Next)
ReDim Preserve rtnArr(UBound(rtnArr) + 1) 'Extra Item
End If
Next
but that's basically the second one..with an If.