【问题标题】:Array as a class property数组作为类属性
【发布时间】:2022-01-08 22:34:06
【问题描述】:

我需要将数组作为自定义类的属性传递,但找不到方法,也没有找到对我有用的已回答问题。所以我来了。

我的“电影”类有 4 个属性,如下所示:tire、annee、duree 和流派。前三个运行良好,但我在最后一个受阻。从一个用逗号分隔的字符串中,我需要创建一系列不同类型的电影(即:冒险、动作等)。我可以轻松创建这个数组,但找不到将其分配为 movie.property 的方法。

以下代码返回不兼容错误。我可能必须实现一个 let 属性,但不明白该怎么做......有没有人知道我做错了什么?如果您需要更大的代码示例,请告诉我。

Public Sub initialiser(ByVal strLigne As String)
    Dim arrData
    arrData = Split(strLigne, vbTab)
    strtitre = arrData(0)
    strAnnee = arrData(1)
    intDuree = ConvertToInt(arrData(2))
    genres = CreerTabGenre(arrData(3))
 
End Sub
Property Get genre() As eGenre ' eGenre is an enum, and I have no choice about that (homework...)
    genre = genres() 'Here I tried all the combinations between parenthesis on both, on none, etc
End Property

编辑:也许问题是我之后如何访问该属性? tabFilms 是所有不同电影的数组。作为测试,我尝试访问数组的第一个类型,但遇到了“不正确的属性影响”错误...

Function genrePopulaire(tabFilms() As film) As String
    Dim i As Integer, j As Integer
    For i = 0 To UBound(tabFilms)
        MsgBox tabFilms(i).genre(0)
    Next i
End Function

【问题讨论】:

  • Property Get genre() As eGenre() genre=genres
  • 显然同样的问题...
  • 如果流派是一个数组,你不能将它传递给一个枚举值,而这正是你当前的 proerty 正在尝试做的事情。如果您想从流派数组中获取特定项目,那么您的流派属性 get 需要指定一个索引。例如属性获取genre(byval ipIndex as long) as eGenre,genre=genres(ipIndex) end 属性。

标签: arrays excel vba


【解决方案1】:

首先,您的拆分数组使用 vbTab 拆分字符串。你说他们用逗号分隔。如果是这样,您应该在字符串的末尾添加一个逗号。它看起来像:

Lord of the Rings; 2002; 200; SciFi;

并像这样使用拆分数组:

arrData = split(strLigne, ";")

另外,我已经尝试过将此代码与类模块一起使用。

在模块程序上试试这个:

Sub Test()

Dim Movie As Object
Set Movie = New clsMovie
Dim arrData

'strligne example
    Dim strligne As String
    strligne = "TLOTR" & vbTab & "2002" & vbTab & 200 & vbTab & "SciFi"

arrData = Split(strligne, vbTab)
With Movie
    .TestMovie arrData
'OUTPUT EXAMPLE:
    Debug.Print "Movie's name is " & .Titre & ". La duree est " & .Duree & " minutes and it was realised on " & .Annee & " and its genre is " & .Genre
End With
'you can change value at any time
movie.titre = "Harry Potter"
debug.print movie.titre & " " & movie.duree
End Sub

关于类模块(重要:称为 clsMovie):

Option Explicit

'Declare all variables as private. If you want, instead of "Private" you can do it "Public" but it can be quite dangerous on a huge project. As you are declaring them Private, it's necessary to work with Let / Get statements below. 
Private pTitre As String, pAnnee As String, pDuree As String, pGenres As String
Function TestMovie(arr)
    Titre = arr(0)
    Annee = arr(1)
    Duree = arr(2)
    Genres = arr(3)
End Function
'From here to the end are the "Let / Get" properties as we work with private variables in the class
Property Get Titre() As String
    Titre = pTitre
End Property
Private Property Let Titre(value As String)
    pTitre = value
End Property
Property Get Annee() As String
    Annee = pAnnee
End Property
Property Let Annee(value As String)
    pAnnee = value
End Property
Property Get Duree() As String
    Duree = pDuree
End Property
Property Let Duree(value As String)
    pDuree = value
End Property
Property Get Genres() As String
    Genres = pGenres
End Property
Property Let Genres(value As String)
    pGenres = value
End Property

【讨论】:

    猜你喜欢
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 2013-05-25
    • 2011-07-17
    • 1970-01-01
    • 2023-03-13
    • 2012-09-14
    相关资源
    最近更新 更多