Results 1 to 8 of 8

Thread: Array Problem

  1. #1

    Thread Starter
    Addicted Member Frankie902's Avatar
    Join Date
    Feb 2001
    Location
    Lindenwold, NJ, USA
    Posts
    217

    Array Problem

    I have an array that has NOTHING in it.
    When I msgbox for the lbound and the ubound it gives a subscript out of range error.

    I am trying to store information in it without useing the Array function because it would delete the other information that is already in the array. I tryed:

    ReDim Preserve rvar(LBound(Owned) To (UBound(Owned) + 1))

    but it gave me a subscript out of range error. how could I do this?
    Using:
    Visual Studio .Net Enterprise
    Visual Basic 6.0 Enterprise

  2. #2
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    I dont know what the value of the Nothing constant really is, but it may be -1 or something, so the array can't accept it as an element number. Do this:
    VB Code:
    1. Dim x() As Long
    2. ReDim x(0)
    and then do redim preserve or whatever you want.
    You just proved that sig advertisements work.

  3. #3
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    BTW theres no point of doing:
    VB Code:
    1. ReDim Preserve x(LBound(x) To UBound(x) + 1)
    Just do:[/Highlight]
    ReDim Preserve x(0 To UBound(x) + 1)
    [/Highlight]
    Cause you cant change the LBound anyways.
    You just proved that sig advertisements work.

  4. #4
    Hyperactive Member
    Join Date
    Apr 2001
    Posts
    315
    Code:
    Dim I as long  
    On error resume next
        I = UBound(emptryarray)
        if Err.Number <> 0 then I = -1
    On error goto 0
    if i < 0 then ' No items
    An alternativ is what I use. I do not use element 0 therefore I always define my dynamic arrays as
    Code:
    Dim aryDynamic()  as string
    Redim aryDynamic(0)    ' Define empty
    Dim lngCurrI as long      ' current "Filled" element
    '.........
    ' put into array
    lngCurrI = lngCurrI + 1                         ' advance to next element
    If lngCurr > ubound(aryDynamic) then ' check fo xpand needed
        Redim preserve aryDynamic (lngcurrI * 1.1 + 1) ' I prefer * 2
    '                             becaus it only costs 4 bytes per empty element 
    End if
    aryDynamic(lngCurrI) = somedata
    '........
    ' Resize when finished filling
    Redim Preserve aryDynamic(lngCurrI)
    Now I can send th array anywhere even with "no" items because UBOUND(aryDynamic will always work and shoot right thru
    For I = 1 to Ubound(aryDynamic)
    '.....
    Next

  5. #5
    Addicted Member Merlin's Avatar
    Join Date
    Dec 2000
    Location
    Eau Claire, WI
    Posts
    233
    You can only Redim Preserve the the upper bound of the last dimension in the array. Additionally when you use:
    VB Code:
    1. Private Sub Form_Load()
    2.    
    3.     ReDim s(0) As String
    4.  
    5.     s(0) = "a"
    6.  
    7. End Sub

    You are actually creating an array with 1 bucket in it.

    Use:
    VB Code:
    1. Private Sub Form_Load()
    2.    
    3.     Dim s() As String
    4.    
    5.     Erase s
    6.  
    7. End Sub

    This will create a true empty array...
    poooof

    Wizard Since 1997
    SQL Server 7.0:2K, Oracle, VB 6.0 EE, VBScript, C/C++, COBOL, RPG ILE, HTML, XML, Perl

  6. #6
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    *Must excersice patience with people who dont read thread*

    Try this instead:
    VB Code:
    1. Dim x() As Variant
    2. ReDim x(0)
    3.  
    4. ReDim Preserve x(0 To UBound(x) + 1)
    You just proved that sig advertisements work.

  7. #7
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Originally posted by Frankie902
    Dim x As Variant
    ReDim Preserve x(0 To UBound(Owned) + 1)

    When I use that I get type mismatch
    If this is the exact code your using, then whya re u doing UBound(Owned) instead of UBound(x)??
    You just proved that sig advertisements work.

  8. #8

    Thread Starter
    Addicted Member Frankie902's Avatar
    Join Date
    Feb 2001
    Location
    Lindenwold, NJ, USA
    Posts
    217
    THe original array is Owned

    but I am putting it in X and then putting x into Owned
    Using:
    Visual Studio .Net Enterprise
    Visual Basic 6.0 Enterprise

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width