VB.NET Array: One Dimensional Example – Code Sample Syntax

Pluralsight courses
LinkedIn Premium uses PayScale to estimate job salaries

Premium (Not Free) Video Tutorials

Free Video Tutorials & Free Tools

VB.NET ArrayOneDimensional Example – Code Sample Syntax

Abstract: – Illustrates using .



 

 


 *** 1. Download Source Code *** 

VB.NET Syntax DataStructures ArrayOneDimensional download Download Source Code for All VB Console Examples in One Project

 


 *** 2. ArrayOneDimensional Syntax *** 


Purpose:
Creates one dimensional array


Syntax:
arrayname(upperbound) As type

Parameters Description
upperbound specifies number of elements in array
Arrays are zero based so there is one more element
than the upperbound specifies

Result Data Type Description
array contains a set of elements



Syntax2:
arrayname() As type
ReDim [Preserve] arrayname(upperbound)

Parameters Description
Preserve optional – specifies that existing data
in array is to be kept
upperbound specifies number of elements in array
Arrays are zero based so there is one more element
than the upperbound specifies

Result Data Type Description
array initial array is set to nothing and then
the ReDim is used to specify size.



 *** 3. ArrayOneDimensional – Quick Example *** 

Dim aryIntegers(9) as Integer
Dim aryStrings() As String 'size not specified so ReDim is needed
Dim aryObjects(2) As myBook 'array of book objects

 

 


 *** 4. ArrayOneDimensional – Full Example *** 

ArrayOneDimensional Example Output Screenshot

VB.NET Syntax DataStructures ArrayOneDimensional screenshot

Step 1: Click Visual Basic to Cut-n-paste code into clsArrayOneDimensional.vb

 Visual Basic |  copy code |? 
Public Class clsArrayOneDimensional
 
    Public Sub Main()
 
 
        '****************************************************************************************
        '
        ' Purpose: Creates one dimensional array
        '
        ' Syntax: 	arrayname(upperbound) As type
        '
        ' Parameter1: upperbound - specifies number of elements in array
        ' Arrays are zero based so there is one more element 
        '  than the upperbound specifies
        '
        ' Result: array - contains a set of elements
        '
        ' Syntax2: 	arrayname() As type
        ' ReDim [Preserve] arrayname(upperbound) 
        '
        ' Parameter1: Preserve - optional - specifies that existing data 
        ' in array is to be kept
        '
        ' Parameter2: upperbound - specifies number of elements in array
        ' Arrays are zero based so there is one more element
        '  than the upperbound specifies
        '
        ' Result: array - initial array is set to nothing and then
        ' the ReDim is used to specify size.
        '
        ' Quick Example: Dim aryIntegers(9) as Integer
        ' Dim aryStrings() As String 'size not specified so ReDim is needed
        ' Dim aryObjects(2) As myBook 'array of book objects
        '
        '****************************************************************************************
        ' This example is from http://idealprogrammer.com
 
        '****************************************************************************************
        Console.WriteLine("Example #1: Dim aryIntegers(9) as Integer")
        '****************************************************************************************
 
        Dim aryIntegers(9) As Integer
 
        Dim intCtr As Integer = 0
 
        'assign values to each element of array
        For intCtr = 0 To 9
            aryIntegers(intCtr) = intCtr
        Next
 
        ' find average value of elements in array
        Dim intAvg As Integer = 0
        Dim intElement As Integer = 0
        Dim intSum As Integer = 0
        Dim intCount As Integer = 0
 
        For Each intElement In aryIntegers
            intSum = intElement + intSum
            intCount = intCount + 1
        Next
 
        intAvg = intSum / intCount
 
 
 
 
 
        Console.WriteLine(intAvg.ToString()) ' Returns 4
 
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        Console.WriteLine("Example #2: 	Dim aryStrings() As String - size is not specified at first")
        '****************************************************************************************
 
        Dim aryStrings() As String
 
        'do something like read a file to determine length of table
 
        ReDim aryStrings(2)
 
        'assign values to elements
 
        aryStrings(0) = "First"
        aryStrings(1) = "Second"
        aryStrings(2) = "Third"
 
        ' you decide you want to add another element
 
        ReDim Preserve aryStrings(3)
 
        aryStrings(3) = "Forth"
 
        For Each strElement As String In aryStrings
            Console.WriteLine(strElement) ' Returns the array one element at a time
        Next
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        Console.WriteLine("Example #3: 	Dim aryObject() As myBook - array of class objects")
        '****************************************************************************************
 
        Dim aryObjects(2) As myBook
 
 
        'assign values to elements
        Dim oBook1 As New myBook
        oBook1.Author = "Plato"
        oBook1.Title = "Republic"
        aryObjects(0) = oBook1
 
        Dim oBook2 As New myBook
        oBook2.Author = "Aristotle"
        oBook2.Title = "Ethics"
        aryObjects(1) = oBook2
 
        Dim oBook3 As New myBook
        oBook3.Author = "Locke"
        oBook3.Title = "Essay Concerning Human Understanding"
        aryObjects(2) = oBook3
 
 
 
        For Each objElement As myBook In aryObjects
            Console.WriteLine(String.Format("{0} - {1}", objElement.Author, objElement.Title)) ' Returns the array one element at a time
        Next
 
 
        'Prevent console from closing before you press enter
        Console.ReadLine()
 
    End Sub
 
End Class
 
Friend Class myBook
 
    Private _author As String
    Public Property Author() As String
        Get
            Return _author
        End Get
        Set(ByVal value As String)
            _author = value
        End Set
    End Property
 
 
    Private _title As String
    Public Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property
 
 
End Class
 
 
 

Step 2: Click Visual Basic to Cut-n-paste code into Module1.vb
 Visual Basic |  copy code |? 
Module Module1
 
    Sub Main()
 
 
        Dim myclsArrayOneDimensional As New clsArrayOneDimensional
        myclsArrayOneDimensional.Main()
 
 
 
    End Sub
 
End Module
 
 
 
 

Prerequistes:

  1. Install Visual Basic (Express or Standard Edition)
  2. Install SQL Server Express
  3. Download Northwind and pubs Database
  4. Attach Northwind Database to Databases in Sql Express
  5. Attach pubs Database to Databases in Sql Express

Notes:

  • Console Application is used to simplify things, but Windows Forms or Web Forms could also be used
  • You can build a library of syntax examples by using same project over and over and just commenting out what you do not want to execute in Module1.vb

Instructions:
VB.NET Syntax DataStructures ArrayOneDimensional

  1. Use Visual Basic 2010 Express or Standard Edition
  2. Create new project;
    • Click File/New Project
    • Select Console Application Template
    • Select Visual Basic for Language
    • name of project could be VBNET_Syntax.
  3. Add New folder named "DataStructures"
    • Right-click project name in solution explorer;
    • add new folder;
    • name of folder could be: DataStructures
  4. Add Class Named clsArrayOneDimensional to DataStructures folder
    • Right-click DataStructures folder;
    • add new item;
    • Select class
    • Class name could be clsArrayOneDimensional
  5. Click on Visual Basic in code in step 1 above to copy code into clsArrayOneDimensional.vb
  6. Click on Visual Basic in code in step 2 above to copy code into Module1.vb
  7. Click green arrow or press F5 to run program

Related posts:

  1. Array Example – Data Type – JavaScript Syntax – JS Array Example Array Example - Data Type - JavaScript Syntax - JS...
  2. VB.NET Split() – Code Sample VB.NET Split() - Code Sample...
  3. VB.NET Abs Example – Code Sample Syntax VB.NET Abs Example - Code Sample Syntax...
  4. VB.NET Min Example – Code Sample Syntax VB.NET Min Example - Code Sample Syntax...
  5. VB.NET Max Example – Code Sample Syntax VB.NET Max Example - Code Sample Syntax...

Related posts brought to you by Yet Another Related Posts Plugin.

Comments

2 Responses to “VB.NET Array: One Dimensional Example – Code Sample Syntax”
  1. aamir says:

    Thx for the info mate. I was very struggling while coding this. This gonna help me so much . BOokmarked!

  2. aamir says:

    It’s a really very informative information. It’s very useful and knowledgeable for me. i was stuck while coding this. I will bookmark this page for coding help.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

Powered by WP Hashcash

This blog uses the cross-linker plugin developed by Web-Developers.Net