Solved

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

Posted on 2013-12-25
4
364 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

815 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