Udostępnij za pośrednictwem


Partition Function

Returns a string representing the calculated range that contains a number.

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

Parameters

  • Number
    Required. Long. Whole number that you want to locate within one of the calculated ranges.

  • Start
    Required. Long. Whole number that indicates the start of the set of calculated ranges. Start cannot be less than 0.

  • Stop
    Required. Long. Whole number that indicates the end of the set of calculated ranges. Stop cannot be less than or equal to Start.

  • Interval
    Required. Long. Whole number that indicates the size of each range calculated between Start and Stop. Interval cannot be less than 1.

Exceptions

Exception type

Error number

Condition

ArgumentException

5

Start < 0, Stop <= Start, or Interval < 1.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

Remarks

The Partition function calculates a set of numeric ranges, each containing the number of values specified by Interval. The first range begins at Start, and the last range ends at Stop. The Partition function then identifies which range contains Number and returns a string describing that range. The range is represented in the string as "lowervalue:uppervalue", where the low end of the range (lowervalue) is separated from the high end (uppervalue) by a colon (:).

If necessary, the Partition function inserts leading spaces before lowervalue and uppervalue so that they both have the same number of characters as the string representation of the value (Stop + 1). This ensures that if you use the output of the Partition function with several values of Number, the resulting text will be handled properly during any subsequent sort operation.

The following table shows some sample strings for ranges calculated using three sets of Start, Stop, and Interval. The "First range" and "Last range" columns show the lowest and highest ranges possible given the values of Start and Stop. The "Before first range" and "After last range" columns show the strings returned for values of Number less than Start and greater than Stop, respectively.

Start

Stop

Interval

Before first range

First range

Last range

After last range

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:    "

In the preceding table, the third line shows the result when Start and Stop define a set of numbers that cannot be evenly divided by Interval. The last range ends at Stop, making it only 11 numbers long, even though Interval is 20.

If Interval is 1, the range is "Number:Number", regardless of the Start and Stop arguments. For example, if Number is 267, Stop is 1000, and Interval is 1, Partition returns " 267: 267".

Partition can be useful when constructing database queries. You can create a SELECT query that shows how many orders occur within various value ranges, for example with invoice values from 1 to 1000, 1001 to 2000, and so on.

Example

The following example sets up a series of ranges for decades from 1950 through 2049. It locates the value of year within the appropriate range and returns a String value showing the range. If year has a value of 1984, for example, Partition returns "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 & ".")

Requirements

Namespace: Microsoft.VisualBasic

Module: Interaction

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Reference

Visual Basic Run-Time Library Members

Interaction Module (Visual Basic)