Hot-Link Menu in Right-Side Column

Last Updated 6-30-2010

  • More Computers
  • More Games
  • More Electronics
  • More Books
  • More Software
  • Visual Basic Length and Sort Methods

    The array class has two more methods that we will touch on briefly:

    • ArrayName.Length: Returns the length (maximum size) of ArrayName.
    • Array.Sort(ArrayName): Sorts elements in array in ascending order (lowest first, highest last).

    Since the DataGridView is so versatile, we can use it again for the next example. We need to explore the two array methods .Length and .Sort. We will create an array of random numbers, display them in the left column. Then we will perform a Sort using the .Sort Method and display the ordered values in the second column, and use the .Length Method as the terminal value in the For Loops.

    Start a new project in Visual Basic and give it some name like ArraySort. Drag a DataGridView control to the form so that it looks like the diagram below:

    Array Sort Design

    Figure Array-33: Visual Basic Array Sort Form At Design

    Buy from Centurion

    We are not going to set any properties in the properties window, but instead set them at run-time in the program. Later, by setting the properties at run-time we can have our forms and controls auto-size themselves to match the data set they are displaying. Enter the program below in the Form.vb window by double-clicking somewhere on the form, but not on the DataGridView control:

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles MyBase.Load
            Dim MaximumArraySize As Integer = 20
            Dim Data(MaximumArraySize) As Integer
            Dim RowNumber As Integer
            DataGridView1.ColumnCount = 2
            DataGridView1.RowCount = MaximumArraySize
            'Make the Headers & Fill the First Column 
            For RowNumber = 0 To Data.Length - 2
                DataGridView1.Rows.Item(RowNumber).HeaderCell.Value = _
                    (RowNumber + 1).ToString
                DataGridView1.Rows.Item(RowNumber).HeaderCell.Style _
                    .Alignment = DataGridViewContentAlignment.MiddleCenter
                DataGridView1.RowHeadersWidth = 50
                DataGridView1.Item(0, RowNumber).Style.Alignment = _
                Data(RowNumber) = Rnd() * 100
                DataGridView1.Item(0, RowNumber).Value = Data(RowNumber)
            DataGridView1.Columns.Item(0).HeaderCell.Value = "Unsorted"
            DataGridView1.Columns.Item(1).HeaderCell.Value = "Sorted"
            'Resize the Display
            DataGridView1.Height = 500
            Me.Height = 520
            'Sort the data and display in second column
            For RowNumber = 0 To Data.Length - 2
                DataGridView1.Item(1, RowNumber).Value = Data(RowNumber)
                DataGridView1.Item(1, RowNumber).Style.Alignment = _
        End Sub
    End Class

    Figure Array-34: Visual Basic Array Sort Source Code

    Help Support this site - Click this ad

    A couple of new concepts we should walk through are outlined below:

    • Dim MaximumArraySize As Integer = 20: We are going to make the array about 20 elements long.

    • Dim Data(MaximumArraySize) As Integer: Declaring an integer Array.

    • DataGridView1.ColumnCount = 2: 1 column for the unsorted values and 1 column for the sorted values.

    • DataGridView1.RowCount = MaximumArraySize: Make the Data Grid big enough to hold the entire array.

    • Make the Headers and fill the first column.

    • For RowNumber=0 to Data.Length-2:
      • Data.Length = 21 and we want to go from 0 to 19 to get 20 elements.

    • DataGridView1.Rows.Item(RowNumber).HeaderCell.Value=(RowNumber+1).ToString:
      • DataGridView1: the name of the control we are going to work with.
      • .Rows: We are going to work with the Row Header.
      • .Item(RowNumber): Select the row we are going to access.
      • .HeaderCell: The title cell is what is going to be accessed.
      • .Value=: We are going to write a new value into the Header Cell.
      • (RowNumber+1): Change the output to start with '1' like Excel.
      • .ToString: Headers require a string as the proper data type.
    • DataGridView1.Rows.Item(RowNumber).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter:
      • Center the text so it is visible.
    • DataGridView1.RowHeadersWidth = 50: Make the header wide enough so we can see 20.

    • DataGrideView1.Item(0, RowNumber).Style.Alignment=DataGridViewContentAlignment.MiddleCenter:
      • Center the text in the data cell as well.
    • Data(RowNumber)=Rnd() * 100:
      • Data(RowNumber):Select the proper Array element.
      • Rnd() * 100: Generate a 'Random' Number between 0 and 100.
    • DataGridView1.Item(0, RowNumber).Value = Data(RowNumber).Value = Data(RowNumber):
      • Transfer the Array Element to the corresponding Data Grid Data Cell.

      Help Support this site - Click this ad

    • Name the Column Headers Appropriately.

    • DataGridView1.Columns.Item(0).HeaderCell.Value="Unsorted"
    • DataGridView1.Columns.Item(1).HeaderCell.Value="Sorted"

    • Resize the Display

    • DataGridView1.Height=500: 500 pixes works out to a length of about 20 cells.
    • Me.Height=520: Make the form just a little bigger than the Data Gride.

    • Sort the data in the array and display in the second column.

    • Array.Sort(Data)
    • DataGridView1.Item(1, RowNumber).Value=Data(RowNumber)

    The last thing we need to do for this section on Arrays is to press that Green Triangle and see what we get:

    Array Sort At Run Time

    Figure Array-35: Array Sort At Run Time

    Help Support this site - Click this ad

    It should be noted that with the Data Grid Control, 1 and 2 dimensional arrays are unnecessary if you are going to display the data in the control. Since the control is an object, the data can be changed, we can get the size of the array, we can sort the array and a host of other methods also. Next, we take a look at Visual Basic Structure construct. Press the Button below to take you to the structure examples.


    Visual Basic

    Introduction to VB.NET

    .NET Framework

    VS2008 IDE

    How VB is Compiled

    Start Visual Studio

    Windows Form App

    Save Your Work

    VB OOP Programming

    Visual Basic Code

    Exit Code

    Button Event Code

    Coding Recommendations


    Error List Window

    Comment Syntax

    Help Window

    Language Essentianl

    Built-In Data Types

    Declare Variables

    Declare Constants

    Code Arithmetic Expressions

    Assignment Statements

    Operator Precedence

    Type Casting

    Math Class

    String Declaration

    Conversion Functions

    Conversion Methods

    Formatting Functions

    String Formatting

    Variable Scope


    Nullable Types

    Loop Constructs

    For Next Loop

    Do While Loop

    Do Until Loop



    Exit Do | Exit For


    Nested Loops


    Array Declaration

    Rnd( ) Function

    Listbox Control


    Parallel Arrays

    Key Event Args

    Dynamic Arrays

    Redimension Array

    Set Breakpoint

    Start Debugger

    ReDim Preserve

    MultiDimensional Arrays

    DataGridView Control

    Length and Sort Methods


    Pad Right

    Split Method

    IsNumeric Function

    Multiform Projects

    Add Form To Project

    Form Object Methods

    Form Show Method

    ShowDialog Method

    Form Close Method

    Form Accept Button

    Multiform Project Example

    ASP.NET Web Programming

    Create Data Source

    Configure Access Data Source

    Add Product Class

    Extract Local Database Data

    Order PageLoad VB Code

    Add New Web Page

    Set Start Page

    Display Cart Aspx Code

    Display Cart Design View

    Sorted List Definition

    VB.NET Session State

    Create CartItem Class

    GetCartContents Function

    Add To Cart Event Handler

    Remove Cart Item Event

    Clear Cart Event Handler