• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

a really cryptic problem with VB arrays.

hi ,

i am a part-time vb user,
i know that a variant array can take in another array as each item, thus becoming "Array of Arrays"

i have a txt file. which looks like
#Aluminium
~pipes
--Some Data--
~tubes
--Some Data--
~bars
--Some Data--
#nickel
~sheet
--Some Data--
~pipes
--Some Data--
#titanium
~pipes
--Some Data--
~tubes
--Some Data--
~bars
--Some Data--
~sheets
--Some Data--

i want to store all '#' item in on array, then each item in that array is linked to the next '~' items and this in turn is connected to --Some Data--.

eg:
#Alumnium---
                    |-------->pipes------->' --Some Data--'
                                         ------->' --Some Data--'
                                         ------->' --Some Data--'
                    |-------->tubes------->' --Some Data--'
                                         ------->' --Some Data--'
                                         ------->' --Some Data--'


Please help, any strategy other than what i have thought

thanks in Advance

regards
Tenali
0
tenali_gowda
Asked:
tenali_gowda
  • 2
1 Solution
 
Dang123Commented:
You could try using a collection and base the key on what you want

eg:
Key: #Alumnium
Contents: pipes|tubes

Key: #Alumnium+pipes
Contents: 1|2|3

Key: #Alumnium+pipes+1
Contents: ' --Some Data--'

Key: #Alumnium+pipes+2
Contents: ' --Some Data--'

   .
   .
   .
0
 
Dang123Commented:
Another solution may be to use the treeview control (visiable or not) and let it keep track of the relations for you.

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is an example of how to do it with collections and have the values displayed on the form via listboxes.  There is no error checking added!  I assume you have a file called data.txt in the same directory as your saved project.  Here is what was inside my data.txt:

#Aluminium
~pipes
1
2
~tubes
3
4
~bars
5
6
#nickel
~sheet
7
8
~pipes
9
10
#titanium
~pipes
11
12
~tubes
13
14
~bars
15
16
~sheets
17
18

Create a new project and add a class module.  Add this code to the class module called Class1:

' Code to go in Class1
Dim storedString As String

Public Property Get stringValue() As String
    stringValue = storedString
End Property

Public Property Let stringValue(newString As String)
    storedString = newString
End Property

Now on your form add three listboxes in a row.  List1 will display alloys, List2 will display the parts for the selected alloy.  List3 will display the data for the selected alloy/part combination.  Here is the code to go on the form:

' Code to go on Form1
Option Explicit

Dim data As New Collection ' Holds data items
Dim alloyParts As New Collection ' Holds part names for each alloy

Private Sub Form_Load()
    Dim alloyPartsList As Collection
    Dim alloyPartData As Collection
   
    Dim curAlloy As String
    Dim curpart As String
    Dim curPartName As Class1
    Dim curDataItem As Class1
   
    Dim inputLine As String
    Dim identifier As String
   
    Dim dataKey As String
   
    curAlloy = ""
    curpart = ""

    Open App.Path & "\data.txt" For Input As #1
    Do While Not EOF(1)
        Line Input #1, inputLine
        identifier = Left(inputLine, 1)
        Select Case identifier
            Case "#"
                curAlloy = inputLine
                List1.AddItem curAlloy
                Set alloyPartsList = New Collection
                alloyParts.Add alloyPartsList, curAlloy
            Case "~"
                curpart = inputLine
                Set curPartName = New Class1
                curPartName.stringValue = curpart
                alloyPartsList.Add curPartName
                Set alloyPartData = New Collection
                dataKey = curAlloy & curpart
                data.Add alloyPartData, dataKey
            Case Else
                Set curDataItem = New Class1
                curDataItem.stringValue = inputLine
                alloyPartData.Add curDataItem
        End Select
    Loop
    Close #1
End Sub

Private Sub List1_Click()
    If List1.ListIndex <> -1 Then
        Dim curAlloyPartsCol As Variant
        Dim curPartItem As Variant
               
        ' populate second list box with part names for selected alloy
        List2.Clear
        List3.Clear
        Set curAlloyPartsCol = alloyParts.Item(List1.List(List1.ListIndex))
        For Each curPartItem In curAlloyPartsCol
            List2.AddItem curPartItem.stringValue
        Next curPartItem
    End If
End Sub

Private Sub List2_Click()
    If List1.ListIndex <> -1 And List2.ListIndex <> -1 Then
        Dim datacol As Variant
        Dim curDataItem As Variant
        Dim dataKey As String
               
        ' grab collection associated with alloy/part combo
        ' then display those items in list3
        List3.Clear
        dataKey = List1.List(List1.ListIndex) & List2.List(List2.ListIndex)
        Set datacol = data.Item(dataKey)
        For Each curDataItem In datacol
            List3.AddItem curDataItem.stringValue
        Next curDataItem
    End If
End Sub

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now