如何使 class-instance 接受参数?

How to make a class-instance accept parameters?

有没有办法做一个class实例,接受参数,根据这个参数生成结果

这在VB.net内置classes中很常见,但我想知道如何自己制作

Dim myNumbers as new NUMBERS
myNumbers.First = 1
myNumbers.Second = 2
Msgbox(myNumbers(1,2,3,4,5,5).max)

在上面的代码中,myNumber 是一个 class,它接受一些数字和 return 最大值。

您可以使用 Default properties in order to achieve that. If you don't want it to be Set-able you can just mark it as ReadOnly,如果您不想 return 一个特定的值,只需 return Nothing.

正在从 属性:

返回一些东西
Default Public ReadOnly Property Calculate(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer) As Integer
    Get
        Dim d As Integer = a * b + c + Me.First
        DoSomeStuff(d)
        Return d * Me.Second
    End Get
End Property

没有返回:

Default Public ReadOnly Property Calculate(ByVal a As Integer, ByVal b As String) As Object
    Get
        Dim c As String = DoStuff()
        DoSomeOtherStuff(a, b, Me.First, Me.Second, c)
        Return Nothing
    End Get
End Property

用法示例:

'With return value.
Dim num As Integer = myNumbers(34, 5, 13)

'Ignoring return value.
Dim dummy As Object = myNumbers(64, "Hello World!")

'Ignoring return value.
Dim dummy As Object = myNumbers.Calculate(13, "I am text")

唯一的缺点是您必须对 returned 值做一些事情(例如将其分配给一个变量)。简单地做:

myNumbers(64, "Hello World!")

无效。