Array Programming in VB.Net

by Ruben 4/10/2008 8:41:00 PM

Array Programming in VB.Net
Array programming is a data structure consisting of a group of elements that are accessed by indexing. In most programming languages each element has the same data type and the array occupies a contiguous area of storage.

Arrays in VB.NET inherit from the Array class in the System namespace. All arrays in VB as zero based, meaning the index of the first element is zero and they are numbered sequentially. You must specify the number of array elements by indicating the upper bound of the array. The upper bound is the number that specifies the index of last element of the array. Arrays are declared using Dim, ReDim, Static, Private, Public and Protected keywords. An array can have one dimension or more than one (multidimensional arrays). The dimensionality of an array refers to the number of subscripts used to identify an individual element.

The following code demonstrates arrays.

'single dimension array
Dim cars(3) As String

'populate the elements of cars variable
cars(0) = "Olds"
cars(1) = "Nissan"
cars(2) = "Toyota"
cars(3) = "Geo"

'In order to extend the array element used Redim Preserve
ReDim Preserve cars(4)
cars(4) = "Mercedes"

'MessageBox.Show(cars(0))
'MessageBox.Show(cars(4))

Dim counter As Integer
For counter = 0 To cars.Length
    MessageBox.Show(cars(counter))
Next

'two dimensional array
Dim trucks(2, 2) As String

'populate the elements of trucks variable
trucks(0, 0) = "Toyota Tundra"
trucks(0, 1) = "Toyota Sequoia"
trucks(0, 2) = "Toyota 4Runner"

trucks(1, 0) = "Nissan Xtera"
trucks(1, 1) = "Nissan Pathfinder"
trucks(1, 2) = "Nissan Titan"

trucks(2, 0) = "Chevy Blazer"
trucks(2, 1) = "Chevy Suburban"
trucks(2, 2) = "Chevy Tahoe"

Dim i As Integer
Dim j As Integer

'outer loop
For i = 0 To trucks.GetUpperBound(0)
    'inner loop
    For j = 0 To trucks.GetUpperBound(1)
        MessageBox.Show(trucks(i, j))
    Next
Next

'tree dimensional array
Dim flights(2, 3, 4) As String

'populate the elements of flights variable
flights(0, 0, 0) = "Chicago - Dallas - 5:00am"
flights(0, 0, 1) = "Chicago - Dallas - 6:00am"
flights(0, 0, 2) = "Chicago - Dallas - 7:00am"
flights(0, 0, 3) = "Chicago - Dallas - 8:30am"
flights(0, 0, 4) = "Chicago - Dallas - 9:45am"

flights(0, 1, 0) = "Dulles - Chicago - 4:45am"
flights(0, 1, 1) = "Dulles - chicago - 5:30am"

'Total of 60 flights

FREE PDF BOOK DOWNLOAD

Create Random list of Record

by Ruben 4/4/2008 5:10:00 PM

How to select record by create random selection using VB.Net.

ex: CreateRandomList(Percentage, TotalRecs, True)


******Module*******
Imports System
Imports System.IO
Imports Microsoft.VisualBasic
Imports System.Math
Imports System.Timers

Module modFunction
    Private Sub BubbleSort(ByVal theArray() As Integer)
        Dim pass As Integer, compare As Integer
        Dim hold As Integer

        For pass = 1 To (UBound(theArray) - 1)
            For compare = 1 To (UBound(theArray) - 1)
                If theArray(compare) > theArray(compare + 1) Then
                    hold = theArray(compare)
                    theArray(compare) = theArray(compare + 1)
                    theArray(compare + 1) = hold
                End If
            Next compare
        Next pass
    End Sub

    Public Function CreateRandomList(ByVal Percentage As Long, ByVal MaxDocs As Long, ByVal Whole As Boolean) As Long
        Dim iNumDocs As Integer, iIndex As Integer
        Dim Num As Integer, Used() As Boolean, Count As Integer
        Dim iTime As New Timers.Timer

        ' Variables for the computation when we want to get sampling via percentage
        Dim iItems As Integer, iIncrement As Integer, iItemsCtr As Integer
        Dim iUpperBound As Integer, iLowerBound As Integer

        Try
            If Not Whole Then
                iItemsCtr = 1
                iItems = MaxDocs * (Percentage / 100)
                iIncrement = Round(MaxDocs / iItems)

                ' Initialize bounds
                iLowerBound = 1
                iUpperBound = iIncrement
            End If

            ' Compute for the number of documents that should be extracted
            iNumDocs = Round(MaxDocs * (Percentage / 100))

            ReDim RandomList(0 To iNumDocs)
            iIndex = 1

            ReDim Used(0 To Val(MaxDocs))

            Randomize(iTime.Interval)

            For Count = 0 To Val(iNumDocs) - 1
                Do
                    If Whole Then
                        Num = (Rnd() * (Val(MaxDocs) - 1)) + 1
                    Else
                        ' Check for the last range
                        If iItemsCtr + 1 = MaxDocs Then
                            ' Means the last range have smaller value than the increment value
                            If iUpperBound + iIncrement > MaxDocs Then
                                iIncrement = Round((MaxDocs - iUpperBound) / 2)
                            End If
                        End If

                        ' Check if last range is less than the increment
                        If iItemsCtr = MaxDocs Then
                            If iUpperBound <> MaxDocs Then
                                iUpperBound = MaxDocs
                            End If
                        End If

                        If (iUpperBound - iLowerBound = 1) Then
                            If Rnd() > 0.5 Then
                                Num = Int((iUpperBound - iLowerBound + 1) * Rnd() + iLowerBound)
                            Else
                                Num = Int((iUpperBound - iLowerBound) * Rnd() + iLowerBound)
                            End If
                        Else
                            Num = Int((iUpperBound - iLowerBound + 1) * Rnd() + iLowerBound)
                        End If

                        iLowerBound = iUpperBound + 1
                        iUpperBound = iUpperBound + iIncrement

                        If iUpperBound > MaxDocs Then
                            iUpperBound = MaxDocs
                        End If

                        iItemsCtr = iItemsCtr + 1
                    End If
                Loop Until Not Used(Num)

                Used(Num) = True

                RandomList(iIndex) = Num
                iIndex = iIndex + 1
            Next Count

            BubbleSort(RandomList)
            CreateRandomList = iNumDocs

        Catch ex As Exception
            MessageBox.Show(ex.Message, "S2xDE", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Function
End Module

FREE PDF BOOK DOWNLOAD

Recieved Updates



Enter your email address:

Delivered by FeedBurner

About the author

Name of author RUBEN CORRAL
System Developer in outSourcing company for almost 8 years. I built this blogs just for fun, sharing idea's, contribute a piece of code, especially to newbie programmers.

E-mail me Send mail

Calendar

<<  September 2008  >>
MoTuWeThFrSaSu
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

View posts in large calendar

Disclaimer

The opinions expressed herein are my own personal point of view. Sample source codes are free to modify or enhance for your own satisfaction.

Sign in

All brand names, logos and trademarks in this site are property of their respective owners.