Solved

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

Posted on 2013-12-25
4
360 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
Comment Utility
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
Comment Utility
@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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now