1

我是 Vb 脚本的新手,我必须创建一个需要大量参数但在 Vb 脚本中不允许使用可选参数的函数(关键字)。

我做了一些网络研究,发现我可以使用参数数组或对象字典。我想知道在哪种情况下最好使用参数数组以及何时必须使用 Dictionary。另外,在每种情况下都可以轻松地将参数添加到我的函数参数中吗?

4

1 回答 1

0

我建议为所有参数创建一个类,并传递该类的单个实例。拥有大量参数的方法是一个糟糕的设计,如果你使用数组,它的健壮性就会降低,因为你总是需要数数才能确定你的参数所在的索引。有了一个类,你所有的“参数”都将被命名为字段或属性。

这是我正在谈论的一个简单示例:

Dim cfg : Set cfg = New CarConfig

With cfg
    .MakeName = "Ford"
    .ModelName = "Escort"
    .StyleName = "2-Door"
    .ColorName = "Blue"
    .NumWheels = 4
End With

Call BuildCar(cfg)

Sub BuildCar(usingConfig)
    With usingConfig
        Call MsgBox( "Your " & .ColorName & " " & _
            .StyleName & " " & .MakeName & " " & _
            .ModelName & " has: " & _
            cfg.NumWheels & " wheels.")
    End With
End Sub

Class CarConfig
    Public MakeName
    Public ModelName
    Public NumWheels
    Public ColorName
    Public StyleName
End Class
于 2016-09-28T11:27:50.377 回答