简体   繁体   中英

Optional Parameters in VBscript

I'm new to Vb-script and I have to create a function (keyword) which needs a big number of param but using Optional parameters is not allowed in Vb-script.

I did some web research and I noticed that I can use argument array or objects dictionary. I want to know in which case it is preferred to use argument array and when I have to use Dictionary. Also, is it easy to add param to my function arguments in each case ?

I would recommend creating a class for all your parameters, and pass a single instance of that class. It's a bad design to have methods with a large number of parameters, and if you use an array, it's less robust because you'll always have to count to figure out which index your param is at. With a class, all your "parameters" will be named fields or properties.

Here's a simple example of what I'm talking about:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM