Share via


Partition 函式

更新:2007 年 11 月

傳回的字串代表包含數字的計算範圍。

Public Function Partition( _
   ByVal Number As Long, _ 
   ByVal Start As Long, _ 
   ByVal Stop As Long, _ 
   ByVal Interval As Long _
) As String

參數

  • Number
    必要項。Long。您想在其中一個計算範圍內找到的整數。

  • Start
    必要項。Long。整數,表示這組計算範圍的開頭。Start 不可小於 0。

  • Stop
    必要項。Long。整數,表示這組計算範圍的結尾。Stop 不可小於或等於 Start。

  • Interval
    必要項。Long。整數,表示 Start 與 Stop 間所計算之每個範圍的大小。Interval 不可小於 1。

例外狀況

例外狀況類型

錯誤代碼

條件

ArgumentException

5

Start < 0、Stop <= Start 或 Interval < 1。

如果將使用非結構化錯誤處理的 Visual Basic 6.0 應用程式升級,請參閱「錯誤代碼」資料行 (您可以將錯誤代碼與 Number 屬性 (Err 物件) 比對)。但是,請盡可能考慮以 Visual Basic 的結構化例外處理概觀 取代這類錯誤控制項。

備註

Partition 函式會計算一組數值範圍,各自會包含 Interval 所指定值的數目。第一個範圍以 Start 開始,而最後一個範圍會以 Stop 結束。Partition 函式接著會識別出哪一個範圍包含 Number,並傳回描述該範圍的字串。該範圍在字串中是以 "lowervalue:uppervalue" 表示,其中範圍的下限 (lowervalue) 與上限 (uppervalue) 之間會以冒號 (:) 分隔。

必要時,Partition 函式會在 lowervalue 和 uppervalue 之前插入前置的空格,使這兩者跟數值 (Stop + 1) 的字串表示法擁有相同數目的字元。這能確定當您使用具有數個 Number 值的 Partition 函式輸出時,產生的文字會在後續的任何排序作業中被妥善處理。

下表所顯示的範例字串,是針對以 Start、Stop 和 Interval 這三組所計算的範圍而提出。「第一範圍」和「最後範圍」欄會顯示在已知 Start 和 Stop 值的狀況下,最低和最高的可能範圍。「第一範圍之前」和「最後範圍之後」欄會分別顯示小於 Start 和大於 Stop 之 Number 值所傳回的字串。

Start

Stop

Interval

第一範圍之前

第一範圍

最後範圍

最後範圍之後

0

99

5

"   : -1"

"  0:  4"

" 95: 99"

"100:   "

20

199

10

"   : 19"

" 20: 29"

"190:199"

"200:   "

100

1010

20

"    : 99"

" 100: 119"

"1000:1010"

"1011:    "

在上表中,第三行會顯示當 Start 和 Stop 所定義的數字無法被 Interval 所整除時的結果。最後範圍會結束於 Stop,使它只有 11 位數字 (雖然 Interval 為 20)。

如果 Interval 為 1,則無論 Start 和 Stop 引數為何,範圍都是 "Number:Number"。例如,如果 Number 為 267、Stop 為 1000,且 Interval 為 1,則 Partition 會傳回 "267: 267"。

在建構資料庫查詢時,Partition 十分有用。您可以建立 SELECT 查詢,以顯示在不同的數值範圍中會出現多少訂單。例如,從 1 到 1000、1001 到 2000 等範圍的發票值。

範例

下列範例會設定一連串的範圍,包含 1950 到 2049 間每隔十年的數字。它會在適當範圍內尋找 year 的值,並傳回會顯示該範圍的 String 值。例如,如果 year 的值為 1984,則 Partition 會傳回 "1980:1989"。

Dim year As Long = 1984
' Assume the value of year is provided by data or by user input.
Dim decade As String
decade = Partition(year, 1950, 2049, 10)
MsgBox("Year " & CStr(year) & " is in decade " & decade & ".")

需求

命名空間Microsoft.VisualBasic

**模組︰**Interaction

組件:Visual Basic Runtime Library (在 Microsoft.VisualBasic.dll 中)

請參閱

參考

Visual Basic 執行階段程式庫成員

Interaction 模組 (Visual Basic)