我定义了以下类型:
Public Type settings
key As String
german As String
french As String
End Type
正确的代码如何定义一个包含~100个文字条目的数组?
喜欢:
Dim translations as Array() = {
(key="send", german="Senden", french="Enregistrer"),
(key="directory", german="Verzeichnis", french="Liste"),
...
答案 0 :(得分:1)
在VBA中没有很好的语法。如果你真的需要初始化一个用户定义类型的数组,你可以使用几个辅助函数来这样做:
Public Type settings
key As String
german As String
french As String
End Type
Private Function NewTranslation(key As String, german As String, french As String) As settings
NewTranslation.key = key
NewTranslation.german = german
NewTranslation.french = french
End Function
Private Sub AddTranslation(translations() As settings, value As settings)
Dim u As Integer
u = -1
On Error Resume Next ' ubound raises an error if the array is not dimensioned yet
u = UBound(translations)
On Error GoTo 0
ReDim Preserve translations(0 To u + 1) As settings
translations(u + 1) = value
End Sub
Public Sub Main()
Dim translations() As settings
AddTranslation translations, NewTranslation("send", "Senden", "Enregister")
AddTranslation translations, NewTranslation("directory", "Verzeichnis", "Liste")
' and so on
End Sub
执行此特定问题的更好方法是使用语言代码和原始文本作为关键字的Collection(map)对象:
Private translations As New Collection
Public Sub Main()
With translations
.Add "Senden", "de:send"
.Add "Enregister", "fr:send"
.Add "Verzeichnis", "de:directory"
.Add "Liste", "fr:directory"
End With
MsgBox GetTranslation("de", "send")
End Sub
Public Function GetTranslation(language As String, s As String)
GetTranslation = s ' default to original text if no translation is available
On Error Resume Next
GetTranslation = translations(language + ":" + s)
End Function
答案 1 :(得分:0)
这不是您问题的直接答案:
我处理翻译文本的方法是将其保存在excel表格中(通常隐藏在XLA插件中),然后将其读取(或者只是当前语言所需的列)到包含数组的变体中。
如果你这样做,维护和扩展(添加新的语言或单词)要容易得多。