Problem with a Dictionary in a Class

Manuel Cavero

New Member
Joined
Feb 17, 2018
Messages
26
Hello everyone:

I have two differents subs, each one calls to a different class to build the same object. The unique difference between both is the type of item used to load the dictionary inside the class. Let's have a look on the first one:

In the first one the dictionary is located inside a SUB procedure Añadir (add in English):

Code:
Option Explicit
Private Valores As Scripting.Dictionary
Private y As Integer
----------------------------------------------
Private Sub Class_Initialize()
    Set Valores = New Scripting.Dictionary
End Sub
----------------------------------------------
Private Sub Class_Terminate()
    Set Valores = Nothing
End Sub
----------------------------------------------
Public Sub Añadir(Valor As Integer)
    With Valores
        If .Exists(y) Then
            If Valor = 1 And .Item(y) > 0 Then
                .Item(y) = .Item(y) + 1
            ElseIf Valor = 1 And .Item(y) < 0 Then
                y = y + 1
                .Add y, Valor
            ElseIf Valor = -1 And .Item(y) < 0 Then
                .Item(y) = .Item(y) - 1
            ElseIf Valor = -1 And .Item(y) > 0 Then
                y = y + 1
                .Add y, Valor
            End If
        Else
            .Add y, Valor
        End If
    End With
End Sub
In the second one, the dictionary is located inside the procedure Let Property Añadir (add in English):

Code:
Option Explicit
Private Valores As Scripting.Dictionary
Private y As Integer
----------------------------------------------
Private Sub Class_Initialize()
    Set Valores = New Scripting.Dictionary
End Sub
----------------------------------------------
Private Sub Class_Terminate()
    Set Valores = Nothing
End Sub
----------------------------------------------
Public Property Let Añadir(Valor As Integer)
    With Valores
        If .Exists(y) Then
            If Valor = 1 And .Item(y) > 0 Then
                .Item(y) = .Item(y) + 1
            ElseIf Valor = 1 And .Item(y) < 0 Then
                y = y + 1
                .Add y, Valor
            ElseIf Valor = -1 And .Item(y) < 0 Then
                .Item(y) = .Item(y) - 1
            ElseIf Valor = -1 And .Item(y) > 0 Then
                y = y + 1
                .Add y, Valor
            End If
        Else
            .Add y, Valor
        End If
    End With
End Property

When the code is initialized in the first case the count property of dictionary = 0, but in the second case the count property of dictionary = 1.

How could I avoid this?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I can't reproduce that. How exactly are you testing the count?
 
Upvote 0
case2.jpg
Hi RoryA this the code for the 1st sub. In this case inside the class, there are subs and fuctions:

Code:
Sub Ejemplo1()
Dim Valor As Variant
Dim Rachas As CRachas1
Dim Promedio As Double
Dim Desviación As Double
Dim RPromedio As Range
Dim RDesviación As Range
Dim RSecuencia As Range
Dim RResumen As Range
Dim Temporal() As Variant
Dim Prueba() As Variant
'Asigno valor a las variables
With ThisWorkbook.Sheets("Hoja1")
    .Range("RDatos1").ClearContents
    Temporal = .Range("B3:B13").Value
    Set RPromedio = .Range("D3")
    Set RDesviación = .Range("E3")
    Set RSecuencia = .Range("C3")
    Set RResumen = .Range("F3")
End With
Set Rachas = New CRachas1
'Carga los valores en la clase
For Each Valor In Temporal
    If Valor > 0 Then Rachas.Añadir (1) Else Rachas.Añadir (-1)
    RPromedio.Value = Rachas.Promedio
    RDesviación.Value = Rachas.Desviación
    RSecuencia.Value = Rachas.Actual
    Set RPromedio = RPromedio.Offset(1, 0)
    Set RDesviación = RDesviación.Offset(1, 0)
    Set RSecuencia = RSecuencia.Offset(1, 0)
Next Valor
'Imprime la matríz del resúmen de la secuencia
Set RResumen = RResumen.Resize(Rachas.Cuenta)
RResumen.Value = Application.WorksheetFunction.Transpose(Rachas.Secuencia)
End Sub

In the 2nd, I remind you the class has properties, but does excatly the same as in the first one:

Code:
Sub Ejemplo2()
Dim Valor As Variant
Dim Rachas As CRachas2
Dim Promedio As Double
Dim Desviación As Double
Dim RPromedio As Range
Dim RDesviación As Range
Dim RSecuencia As Range
Dim RResumen As Range
Dim Temporal() As Variant
Dim Prueba() As Variant
'Asigno valor a las variables
With ThisWorkbook.Sheets("Hoja2")
    .Range("RDatos2").ClearContents
    Temporal = .Range("B3:B13").Value
    Set RPromedio = .Range("D3")
    Set RDesviación = .Range("E3")
    Set RSecuencia = .Range("C3")
    Set RResumen = .Range("F3")
End With
Set Rachas = New CRachas2
'Carga los valores en la clase
For Each Valor In Temporal
    If Valor > 0 Then Rachas.Añadir = 1 Else Rachas.Añadir = -1
    RPromedio.Value = Rachas.Promedio
    RDesviación.Value = Rachas.Desviación
    RSecuencia.Value = Rachas.Actual
    Set RPromedio = RPromedio.Offset(1, 0)
    Set RDesviación = RDesviación.Offset(1, 0)
    Set RSecuencia = RSecuencia.Offset(1, 0)
Next Valor
'Imprime la matríz del resúmen de la secuencia
Set RResumen = RResumen.Resize(Rachas.Cuenta)
RResumen.Value = Application.WorksheetFunction.Transpose(Rachas.Secuencia)
End Sub

And the test for both procedures:
1st: https://uploadfiles.io/92zni
2nd: https://ufile.io/5kmlw

case2.jpg
92zni
5kmlw
 
Upvote 0
I can't replicate that from the code you have posted. Can you post the complete class or, preferably, a sample workbook rather than a picture?
 
Upvote 0
Hi RoryA

I see the problem and its not vinculate to a Property or a Sub, when the class code its initialized, every single procedure its tested and its executed. This not happens when the event class initialize and runs a sub or a function procedures

Thanks a lot for your help!!!
 
Upvote 0

Forum statistics

Threads
1,223,919
Messages
6,175,368
Members
452,638
Latest member
Oluwabukunmi

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