當方法中最後一個輸入參數或最後一個輸出參數是字串時,該字串大小可以變化。 其大小可從 0 到長度指定的最大位元組數不等。 當回傳值為字串且置於所有其他輸出參數之後時,它可以是可變大小的最終輸出欄位。
若要讓字串具有可變的大小,則必須最後才進行傳送或接收。 否則,無法可靠地判斷大小可變字串的結尾與緩衝區中下一個資料項目。 主機應用程式的邏輯只傳送所需的字串部分資料。
COBOL 從不設定字串的可變大小選項。 若要手動設定此屬性,請將 Variable Sized Final Field 屬性設為 variable。 「 變量大小最終場 」依方向細分為兩部分。 設定你想要實現的方向。
以下 COBOL 範例的最後一個資料項目是一個大型字串,可透過僅傳送該字串的大小來優化:
01 CUSTOMER-DATA.
05 CUST-HEADER.
10 CUSTOMER-NUMBER PIC 9(9).
10 LAST-NAME PIC X(20).
10 FIRST-NAME PIC X(20).
05 COMMENTS PIC X(4096).
匯入後,此 COBOL 程式碼會產生以下方法:
CustomerInformation(lCustomerNo As Long,_
strLastName As String,_
strFirstName As String,-
strComments As String)
以下 Visual Basic 程式碼呼叫該方法:
Dim objCustomer As Object
Dim lCustomerNo As Long
Dim strLastName As String
Dim strFirstName As String
Dim strComments As String
lCustomerNo = 100231
'create an instance of the invoicing object
On Error GoTo ErrorHandler1
Set objCustomer = CreateObject("Customer.Invoicing.1")
'invoke the SetInvoices method
On Error GoTo ErrorHandler2
objCustomer.CustomerInformation lCustomerNo, strLastName _
, strFirstName, strComments