VB.NET Array: One Dimensional Example – Code Sample Syntax
Posted by asp.net videos on Sunday, May 22, 2011 · Leave a Comment
Premium (Not Free) Video Tutorials
Free Video Tutorials & Free Tools
Premium (Not Free) Video Tutorials
Free Video Tutorials & Free Tools
VB.NET ArrayOneDimensional Example – Code Sample Syntax
Abstract: – Illustrates using VB.NET ArrayOneDimensional Code Example.
*** 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
Step 1: Click Visual Basic to Cut-n-paste code into clsArrayOneDimensional.vb
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 |
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
Module Module1
Sub Main()
Dim myclsArrayOneDimensional As New clsArrayOneDimensional
myclsArrayOneDimensional.Main()
End Sub
End Module |
Module Module1
Sub Main()
Dim myclsArrayOneDimensional As New clsArrayOneDimensional
myclsArrayOneDimensional.Main()
End Sub
End Module
Prerequistes:
- Install Visual Basic (Express or Standard Edition)
- Install SQL Server Express
- Download Northwind and pubs Database
- Attach Northwind Database to Databases in Sql Express
- 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:
- Use Visual Basic 2010 Express or Standard Edition
- Create new project;
- Click File/New Project
- Select Console Application Template
- Select Visual Basic for Language
- name of project could be VBNET_Syntax.
- Add New folder named “DataStructures”
- Right-click project name in solution explorer;
- add new folder;
- name of folder could be: DataStructures
- Add Class Named clsArrayOneDimensional to DataStructures folder
- Right-click DataStructures folder;
- add new item;
- Select class
- Class name could be clsArrayOneDimensional
- Click on Visual Basic in code in step 1 above to copy code into clsArrayOneDimensional.vb
- Click on Visual Basic in code in step 2 above to copy code into Module1.vb
- Click green arrow or press F5 to run program
|