3

I think I'm missing something simple here. Trying to use a variable in a Class module. Let and Get work fine. But if I try to use the variable in a different sub in the Class module I just get a value of 0.

Class Module clsCombobox

Public WithEvents ComboBox As MSForms.ComboBox
Public WithEvents ComboTextBox As MSForms.TextBox

Public Num As Long

Public Property Let Number(Value As Long)
    Num = Value
End Property

Public Property Get Number() As Long
    Number = Num
End Property

Private Sub ComboBox_Change()
    Me.ComboTextBox.Value = Num
    'this gives value of 0
End Sub

Userform Module

Dim obEvents as clsCombobox
Set obEvents = New clsCombobox
obEvents.Number = 52
MsgBox obEvents.Number 'this prints 52

Sub that sets ComboBox

Private Sub GroupCombobox()
Dim i As Long
Dim obEvents As clsCombobox

Set collCombobox = New Collection

For i = 1 To 5
    Set obEvents = New clsCombobox
    Set obEvents.ComboBox = Me.Controls("cbAbility" & i)
    Set obEvents.ComboTextBox = Me.Controls("tbAbility" & i & "Text")
    collCombobox.Add obEvents
Next i

End Sub
6
  • 3
    For proper encapsulation, Num should be a Private field. Commented Mar 20, 2018 at 22:45
  • Can you share the code that sets ComboBox and ComboTextBox? Commented Mar 20, 2018 at 22:47
  • Added the code. The ComboBox_Change sub works well if I set it to = 52 or = Me.ComboBox.ListIndex. It's something about getting that variable. Commented Mar 20, 2018 at 22:54
  • who/from where is calling ComboBox_Change? Commented Mar 20, 2018 at 23:37
  • ComboBox_Change is just the event when you select any option in the dropdown list of one of the comboboxes in the collection Commented Mar 21, 2018 at 1:12

1 Answer 1

3

It doesn't look like you ever set the value. I assume you mean to do that in the loop? Perhaps not with the value of i, but here you can see the idea...

For i = 1 To 5
    Set obEvents = New clsCombobox

    'Set the value here
    obEvents.Number = 52 ' 52 or whatever is needed as Number

    Set obEvents.ComboBox = Me.Controls("cbAbility" & i)
    Set obEvents.ComboTextBox = Me.Controls("tbAbility" & i & "Text")
    collCombobox.Add obEvents
Next i
Sign up to request clarification or add additional context in comments.

6 Comments

The i is just for the loop to add a number of controls to the collection. So cbAbility1, cbAbility2 are different comboboxes. The class allows them all to act the same way when you select something in their dropdown list.
@bigbucky Maybe you simply forgot obEvents.Number = 52 inside of the for i=1 to 5 loop? When you don't set the Number then it will still have value of zero. @thuderframe you mean Number not Value?
I see. I did t realize I had to set it for each control. I thought once you set the variable it would stay set in the Class module. I’m away from computer but will try later.
No, each object is independent from the other objects, it is different memory location, each object hast its own state and data etc.
The class module is like a template for the objects which is created at design time. At runtime the objects are created based on this template and each object is isolated, lives in another memory location.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.