Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Pro VB 6
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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.:)

Reply With Quote
  #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

Reply With Quote
  #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
Reply With Quote
  #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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:51 AM.


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