Arrays: Dynamically resizing [question] menu

Shout-Out

User Tag List

Results 1 to 4 of 4
  1. #1
    abuckau907's Avatar Active Member
    Reputation
    49
    Join Date
    May 2009
    Posts
    225
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrays: Dynamically resizing [question]

    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.
    Last edited by abuckau907; 02-28-2010 at 03:48 PM.

    Arrays: Dynamically resizing [question]
  2. #2
    The-Eradicator's Avatar Contributor

    Reputation
    149
    Join Date
    May 2007
    Posts
    829
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you need an array, but don't know how many items there will be, use a List.

    Code:
    Private Shared random As New Random()
    
    Public Function MakeRandoms(ByVal number As Integer) As Integer()
         Dim randoms As New List(Of Integer)()
         
         For i As Integer = 0 To number
              randoms.Add(random.Next())
         Next
         
         Return randoms.ToArray()
    End Function
    The most beautiful thing we can experience is the mysterious. It is the source of all true art and all science. He to whom this emotion is a stranger, who can no longer pause to wonder and stand rapt in awe, is as good as dead: his eyes are closed.
    Albert Einstein

  3. #3
    abuckau907's Avatar Active Member
    Reputation
    49
    Join Date
    May 2009
    Posts
    225
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thought about it. So does the array class internally use a counter variable?

  4. #4
    The-Eradicator's Avatar Contributor

    Reputation
    149
    Join Date
    May 2007
    Posts
    829
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by abuckau907 View Post
    Thought about it. So does the array class internally use a counter variable?
    Probably something like that, I don't know. Take a look at it with Reflector if you're curious.
    The most beautiful thing we can experience is the mysterious. It is the source of all true art and all science. He to whom this emotion is a stranger, who can no longer pause to wonder and stand rapt in awe, is as good as dead: his eyes are closed.
    Albert Einstein

Similar Threads

  1. [Question] Resizing with DBC's
    By cptforum in forum WoW ME Questions and Requests
    Replies: 2
    Last Post: 09-10-2008, 03:17 PM
  2. Glitchy's MES Model Resizer Question
    By Limiter in forum WoW ME Questions and Requests
    Replies: 3
    Last Post: 08-04-2008, 05:02 PM
  3. [Question] Spell Resizing
    By [Soul Eater] in forum WoW ME Questions and Requests
    Replies: 1
    Last Post: 07-25-2008, 11:11 PM
  4. [Question] Resizing .m2's without Glitchy's?
    By Andrige in forum WoW ME Questions and Requests
    Replies: 0
    Last Post: 06-14-2008, 04:48 AM
  5. [Question] Resize Help Please
    By Mercer in forum WoW ME Questions and Requests
    Replies: 4
    Last Post: 11-06-2007, 09:48 AM
All times are GMT -5. The time now is 09:22 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search