Solved

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

Posted on 2013-12-25
4
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

732 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