Unicode string literals in VBA

后端 未结 3 1164
萌比男神i
萌比男神i 2020-12-06 19:14

I would like to declare (in a VBA class module) some private constant strings that contain Japanese characters. Is there a way to construct String literals (or

3条回答
  •  盖世英雄少女心
    2020-12-06 19:27

    Faking the Const by creating Private Property Get returning the string, because the string will be recreated every time I access the property (plus, is confusing and ugly... but, okay, the last two things are rather a matter of taste).

    You need not recreate the string each time you access the property.

    While this is still ugly as a matter of taste, make a read-only property (essentially Const, since it doesn't have a Property Let procedure), and construct the string in the Class_Initialize event:

    '## CLASS MODULE
    Private pUnicodeString As String
    
    Sub Class_Initialize()
        pUnicodeString = ChrW(22793) & ChrW(25968)
    End Sub
    
    Property Get UnicodeString() As String
        UnicodeString = pUnicodeString
    End Property
    

    And then invoke it like:

    '## STANDARD MODULE
    Sub Test()
    Dim c As myClass
    Set c = New myClass
    
    [A1].Value = c.UnicodeString
    
    End Sub
    

提交回复
热议问题