0

我正在将代码从代码隐藏移动到类并遇到问题。我有一个制作字符串(html 发票)的方法。在该方法中,创建了最终数量。我想创建一个名为 Invoice 的类,该类将具有方法“CreateInvoice”,该方法将返回一个字符串,并且我还想设置对象的 finalCharge 属性。我已经完成了所有繁重的工作,但是设置课程是我遇到麻烦的部分

Public Class Invoice
    Public finalCharge As Double
    Public invoiceString As String
    Public billingId As Integer
    Public clientID As Integer

Public Shared Function CreateInvoice(ByVal bill_id As Integer, ByVal client_id As Integer) As String
    ... 'create string invoice
    ... 'tally final charge
End Function


'On my code-behind page.
dim y as string
dim x as Invoice

y = x.CreateInvoice(1225,8855) <-- this line doesn't work.

感谢您帮助解决问题!

4

1 回答 1

2

它是一个Shared函数 - 这意味着您不需要(或不想要)该类的实例来使用它。你应该这样称呼它:

Invoice.CreateInvoice(1225, 8855)

就是说,我不完全确定应该Shared从头开始,因为您还说:

[I]...想设置 finalCharge 属性

由于一个Shared函数只能访问Shared属性(因为没有实例),你可能真的希望它是一个实例函数:

Public Class Invoice
    Public finalCharge As Double
    Public invoiceString As String
    Public billingId As Integer
    Public clientID As Integer

    Public Function CreateInvoice(ByVal bill_id As Integer, ByVal client_id As Integer) As String
        ... 'create string invoice
        Me.finalCharge = ... 'tally final charge
    End Function
End Class

在这种情况下,您可以这样称呼它:

Dim x as New Invoice()
Dim y as String = x.CreateInvoice(1225, 8855)
Dim finalCharge as Double = x.finalCharge
于 2012-05-18T20:58:22.250 回答