Using custom class with dictionary, can't access items correctly

Fredrik1987

Board Regular
Joined
Nov 5, 2015
Messages
69
Hi! I've been stuck with this for a couple of days now, any help would be appreciated! :)
I'm looping through a folder and retrieving some technical drawings. This works just fine, but I can't access the items correctly.
The drawings follow this naming structure: Drawing_RevisionNumber_Type, e.g:1900_A_par.PDF, 1900_A_par.SAT etc.


I also need to check if the drawings have been revised, and replace the old with the new version (So a collection won't work?).
Here's what I've done so far (Se code):

  1. Created a class with three properties; Name, Rev. number and Filepath (cFolderList)
  2. A function that returns list of files as a dictionary object (Works just fine)
    1. Loop through all files in folder
    2. Split file name by "_"
    3. Set Key as Drawing_Type.Filetype
    4. Add file if key doen't exist

With a collection I can access the items, so I'm clearly not understanding/using the dictionary correctly?


Code to access items - This is the part that doesn't work:
Code:
Sub TestRef()


Dim MyDictionary As New Scripting.Dictionary
Dim MyItem As Object
Dim MyPath As String


MyPath = Path with drawings
Set ss = GetFileFromFolder(MyPath)


For Each MyItem In MyDictionary
    '/  This method works with a collection
    '/  How do you do the same with a dictionary?


    Debug.Print MyItem.DrawingName
    


Next


'/  Tried the following:
'/  Setting MyItem as cFolderList
'/  Changing it to a collection, then it works just fine


End Sub

Class module - cFolderList:
Code:
Option Explicit




Private p_FilePath As String
Private p_DrawingName As String
Private p_RevNumber As String




Sub Initialize( _
Tegning As String, _
RevisjonNr As String, _
FilePath As String _
)




DrawingName = Tegning
RevNr = RevisjonNr


MyPath = FilePath


End Sub






'/  Filbane
Public Property Get MyPath() As String


    MyPath = p_FilePath


End Property




Public Property Let MyPath(FilePath As String)


    p_FilePath = FilePath


End Property




'/  Drawing Name
Public Property Get DrawingName() As String


    DrawingName = p_DrawingName


End Property




Public Property Let DrawingName(DrawingName As String)


    p_DrawingName = DrawingName


End Property






'/  Rev. number
Public Property Get RevNr() As String


    RevNr = p_RevNumber


End Property




Public Property Let RevNr(RevNum As String)


    p_RevNumber = RevNum


End Property

Function:
Code:
Private Function GetFileFromFolder( _
FolderDir As String _
) As Scripting.Dictionary


'   Temporary variables
Dim Dict_Temp As New Scripting.Dictionary
Dim FileInfo As cFolderList
Dim FSO As New Scripting.FileSystemObject
Dim File As Scripting.File


'   Variables
Dim str_Name() As String
Dim str_Key As String
Dim MyFolder As Scripting.folder
Dim FilesInFolder As Scripting.files




'/  Code
Set MyFolder = FSO.GetFolder(FolderDir)
Set FilesInFolder = MyFolder.files




'/  Set key as Name_TypeOfDrawing_FileType
'/  Check if drawing exist, if so, check revision number
For Each File In FilesInFolder
    str_Name = Split(File.name, "_")                                '   0: Name, 1: Rev.number, 2: Type
    str_Key = str_Name(0) & "_" & str_Name(2) & "_" & File.Type
    
    
    
    If Dict_Temp.Exists(str_Key) = False Then
        Set FileInfo = New cFolderList
        FileInfo.Initialize str_Name(0), str_Name(1), File.Path
        
        
        Dict_Temp.Add str_Key, FileInfo
        
        
    
    Else
        
        '/  It activates correctly, but I need access to the items to check for rev. number
    
    
    End If
Next






'/  Check for content
If Dict_Temp.Count = 0 Then


    
    Call EmptyDictonary(MyFolder)




Else




    Set GetFileFromFolder = Dict_Temp




End If




End Function
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Well, as always, the solution presented itself 20 minutes after posting.
Saved the items as a collection within the dictionary, now it works like a charm!

Sorry for the long post :)
 
Upvote 0

Forum statistics

Threads
1,215,331
Messages
6,124,311
Members
449,152
Latest member
PressEscape

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top