2012-05-18 63 views
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. 

謝謝你的幫助來獲得整理出我有麻煩的一部分!

回答

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 
+0

真棒!謝謝。我希望你不介意我是否添加後續內容:在「CreateInvoice」中我似乎無法設置finalCharge。在「CreateInvoice」裏面我總共有兩個,但似乎無法設置全局的「finalCharge」。再次感謝!! – user995727

+0

解決了!再次感謝你的幫助!快樂的長週末。 – user995727