user995727
user995727

Reputation: 89

Trying to make a class return a string

I'm moving code from code-behind to classes and run into a problem. I have a method which makes a string (an html invoice). In the method a final amount is created. I want to make a class called Invoice which will have the method "CreateInvoice" which will return a string and I also want to set the finalCharge property of the object. I have all the heavy lifting done but it's setting up the class is the part I'm having troubles with

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.

Thank you for helping be get sorted out!

Upvotes: 0

Views: 658

Answers (1)

Mark Brackett
Mark Brackett

Reputation: 85655

It's a Shared function - which means you don't need (or want) an instance of the class to use it. You should call it like:

Invoice.CreateInvoice(1225, 8855)

That said, I'm not entirely sure it should be Shared to begin with, since you also state:

[I]...want to set the finalCharge property

Since a Shared function can only access Shared properties (since there is no instance), you may really want it to be an instance function:

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

In which case, you'd call it like:

Dim x as New Invoice()
Dim y as String = x.CreateInvoice(1225, 8855)
Dim finalCharge as Double = x.finalCharge

Upvotes: 2

Related Questions