?
Solved

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

Posted on 2013-12-25
4
Medium Priority
?
374 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 2000 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 46

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Suggested Courses

777 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