Igor Cheglakov
Igor Cheglakov

Reputation: 555

Trying to access dictionary from a class. Run-time error 91

I want to use a class, the class should return a dictionary subjectData with the keys name, inn, ogrn and address with string values. The code of the class module:

'Create dictionary
Public subjectData As Scripting.Dictionary

'Initialize dictionary       
Private Sub class_initalize()
    With subjectData
        .Add "name", ""
        .Add "inn", ""
        .Add "ogrn", ""
        .Add "address", ""
    End With
End Sub

When I try to make exemplar of the class in the main module and access it's properties I get runtime error 91 "object variable with block variable not set"

The code:

Option Explicit

Private Sub Document_New()
  Dim client As New LegalSubject
  client.subjectData.Item(name) = "Microsoft corporation" '<--- Error!
End Sub

I figured this is some kind of problem with variable not being set to object, but it doesn't work with set either. I get "Compile Error: Variable not defined".

Option Explicit

Private Sub Document_New()
  Dim client As New LegalSubject
  Set client = LegalSubject
  client.subjectData.Item(name) = "Microsoft corporation"
End Sub

Upvotes: 0

Views: 375

Answers (1)

DisplayName
DisplayName

Reputation: 13386

1) the real name of Initialize event of a Class is Class_Initialize()

2) in the Initialize event you have to set the dictionary object before using it

Private Sub Class_Initialize()
    Set subjectData = New Scripting.Dictionary
    With subjectData
        .Add "name", ""
        .Add "inn", ""
        .Add "ogrn", ""
        .Add "address", ""
    End With
End Sub

Upvotes: 2

Related Questions