Compilation error: only user types defined in public object modules can be forcibly applied to or from a variant or passed to functions with late restrictions

I am struggling with a bit of VBa and Excel. I need to create a structure in VBa, which is Type. I have a problem, I get an error when I try to execute the code! I feel that I need to explain how I arrived where I am if I made a mistake.

I read that to create a type it needs to be made public. So I created a new class (in class modules). In Class1, I wrote

Public Type SpiderKeyPair
    IsComplete As Boolean
    Key As String
End Type

And inside ThisWorkbookI have the following

Public Sub Test()    
    Dim skp As SpiderKeyPair
    skp.IsComplete = True
    skp.Key = "abc"    
End Sub

There is no other code. I have a problem: I get an error

Unable to define public user type inside object module

private, , , , - ( .NET).

Class1 Module1, , , - , .

Test

Private m_spiderKeys As Collection 

Public Sub Test()                
    Dim sKey As SpiderKeyPair
    sKey.IsComplete = False
    sKey.Key = "abc"            
    m_spiderKeys.Add (sKey)    'FAILS HERE            
End Sub

, ,

, , ... SpiderKeyPair ?

+1
1

, , OOP mistaking VBA VB.NET. , . , :

:

Option Explicit

Public Sub Test()

    Dim skpObj          As SpiderKeyPair
    Dim m_spiderKeys    As New Collection
    Dim lngCounter      As Long

    For lngCounter = 1 To 4
        Set skpObj = New SpiderKeyPair
        skpObj.Key = "test" & lngCounter
        skpObj.IsComplete = CBool(lngCounter Mod 2 = 0)
        m_spiderKeys.Add skpObj
    Next lngCounter

    For Each skpObj In m_spiderKeys
        Debug.Print "-----------------"
        Debug.Print skpObj.IsComplete
        Debug.Print skpObj.Key
        Debug.Print "-----------------"
    Next skpObj

End Sub

SpiderKeyPair :

Option Explicit

Private m_bIsComplete   As Boolean
Private m_sKey          As String

Public Property Get IsComplete() As Boolean
    IsComplete = m_bIsComplete
End Property

Public Property Get Key() As String
    Key = m_sKey
End Property

Public Property Let Key(ByVal sNewValue As String)
    m_sKey = sNewValue
End Property

Public Property Let IsComplete(ByVal bNewValue As Boolean)
    m_bIsComplete = bNewValue
End Property

Test Sub , :

Falsch
test1
-----------------
-----------------
Wahr
test2

, . New. New.

0

Source: https://habr.com/ru/post/1689864/


All Articles