Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 22nd, 2004, 11:42 AM
Registered User
 
Join Date: Nov 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 2 dimentional array

I have done a VBA code and I discover that the array can not store what I want. I will explain as follow:

k(0) = a
k(1) = b
k(2) = c
k(3) = d

    For j = 0 To 3
    n = k(j)
    ReDim feasible(4, n)
    'feasible(number of feasible start time, number of shift)

        For i = 0 To n - 1
        feasible(j, i) = Sheet1.Cells(3, 2 + j ) + i * (1 / 24)
          If feasible(j, i) > 1 Then
          feasible(j, i) = feasible(j, i) - 1
here i can store the right number in the feasible(j,i) array
          End If
        Next i
Here the number in the array become all zero
    Next j

I really don't know why, please help me. Thank you very much in advance.:)

  #2 (permalink)  
Old November 22nd, 2004, 12:22 PM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , United Kingdom.
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,

Try changing
Code:
ReDim feasible(4, n)
To
Code:
ReDim Preserve feasible(4, n)
HTH,

Chris

  #3 (permalink)  
Old November 22nd, 2004, 01:53 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Alameda, ca, USA.
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you do not tell us 'what do you want' to be saved, it is a little hard to answer... there is anything wrong in your code (beside the second loop from zero to n-1 while the second dimension of the array goes from zero to n.)
Did you put a breakpoint in the first line of the inner loop and see what values are actually storing in the array?

Marco
  #4 (permalink)  
Old November 23rd, 2004, 03:13 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

As ChrisScott recommended, you need to add Preserve.

When you redimension an array, memory for the array's new size is allocated. This is NOT the same section of memory as where the array was being stored prior to the redim.
After the allocation, the array is adjusted to point to the new memory (which is, of course, blank). If you use Preserve, the data from the old memory is copied to the new meory.

This slows things down a lot. If you can, you should try to set the array size to as large of a size as you will need, and keep track of how much of it has been filled in either one of the elements, or a separate variable.

k(0) = a
k(1) = b
k(2) = c
k(3) = d
ReDim feasible(4, 3)


    For j = 0 To 3
    n = k(j)

        For i = 0 To n - 1
            feasible(j, i) = Sheet1.Cells(3, 2 + j ) + i * (1 / 24)
            If feasible(j, i) > 1 Then
                feasible(j, i) = feasible(j, i) - 1
                ' Here I can store the right # in feasible(j, i)
            End If
        Next i
        ' Here all numbers in the array become zero
    Next j


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convering a String Array to an Integer array nkrust C# 9 November 17th, 2010 12:02 PM
2 dimentional array plomoscky Pro VB 6 1 January 14th, 2008 12:50 PM
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 11:13 AM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM
accessing a two dimentional collection array levinll Beginning VB 6 0 September 2nd, 2004 01:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.