Solved

vb6 - Declare an array dynamically with an unknown number of rows and unknown number of columns

Posted on 2013-12-25
4
366 Views
Last Modified: 2013-12-29
Experts,

Here is my predicament:

I need an array that I can Redim Preserve at runtime that should be able to hold unknown number of rows and unknown number of columns.

It does not necessarily need to be an array. Anything that works is fine.

Scenario:

I want to store a bunch of folder names and files stored in it. So essentially, there could be a bunch of folders containing a bunch of files :(

Thanks!
0
Comment
Question by:Jammerules
4 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 39739766
Well, my approach to this would be this way:

Create a cFile class to store a file. This class stores only the file name, but you can add more properties to it to store file length or whatever you want.

Option Explicit

Private m_fileName As String

Public Property Get FileName() As String
    FileName = m_fileName
End Property

Public Property Let FileName(ByVal value As String)
    m_fileName = value
End Property

Open in new window


Create a cFolder class to store a folder, assuming that a folder can contain an indeterminate number of files and subfolders.

Option Explicit

Private m_Path As String
Private m_Files As Collection
Private m_Folders As Collection

Public Property Get Folders() As Collection
    If (m_Folders Is Nothing) Then
        m_Folders = New Collection
    End If
    Folders = m_Folders
End Property

Public Property Get Files() As Collection
    If (m_Files Is Nothing) Then
        m_Files = New Collection
    End If
    Files = m_Files
End Property

Public Property Get Path() As String
    Path = m_Path
End Property

Public Property Let Path(ByVal value As String)
    m_Path = value
End Property

Open in new window


Once both classes created, you can use the cFolder class as follows:

Dim myFolder As cFolder
myFolder = New cFolder
myFolder.Path = "c:\mypath" 'Set the folder path
myFolder.Files.Add "c:\mypath\myfile.txt", "c:\mypath\myfile.txt" 'Add a file to the folder
'I use the file name as the key for the collection, so later I can easily access this file by its file name: myFolder.Files.Item("c:\mypath\myfile.txt").Length = 1000
myFolder.Folders.Add "c:\mypath\mysubpath", "c:\mypath\mysubpath" 'Add a subfolder to the folder
'I use the folder path as the key for the collection, so later I can easily access this folder by its path: myFolder.Folders.Item("c:\mypath\mysubpath").Files.Add ...

Open in new window

Now, if you start by a single path, you can have a unique cFolder instance to store all you files and folders. Or if you need a multiple initial path, you can declare an array or collection of cFolder instances.

Hope that helps.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 39741812
@Jammerules

1. What kind of processing (actions) are you going to do with the data in this file structure?
2. Does this data exist only for the life of the current execution or do you need this data to persist between executions?
0
 

Author Comment

by:Jammerules
ID: 39745614
I had changed my approach and am currently using a two dimensional array with preset row length, although the second dimension is not...

I think Luis' approach would be more neat (in a way), but hey, I already coded the way I did and it is working. So, why break it when it works :). Thanks Luis for your quick response. Sorry it took a while to accept your solution.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question