Saturday, April 30, 2011

Arrays

In this example, we create two string arrays in the VB.NET language. The first array is created with an initialization statement; you use curly brackets on the right side and can specify each string element. You do not need to specify the size of the array on the left side. The second array uses the longer syntax: you must specify the capacity of the array in the Dim statement. Then, you can assign various elements into the memory you allocated.
 Sub Main()
  
      ' Create an array with the simple initialization syntax.
  
      Dim array() As String = {"dog", "cat", "fish"}
  
      ' Loop over the array.
  
      For Each value As String In array
  
        Console.WriteLine(value)
  
      Next
  
      ' Pass array as argument.
  
      M(array)
  
      ' Create an array in several statements.
  
      ' ... Use the maximum index in the declaration.
  
      ' ... Begin indexing at zero.
  
      Dim array2(2) As String
  
      array2(0) = "bird"
  
      array2(1) = "dog"
  
      array2(2) = "gopher"
  
      ' Loop.
  
      For Each value As String In array2
  
        Console.WriteLine(value)
  
      Next
  
      ' Pass as argument.
  
      M(array2)
  
   End Sub
  
   Sub M(ByRef array() As String)
  
      ' Write length.
  
      Console.WriteLine(array.Length)
  
   End Sub
  

The following source code shows all important operations in a HashTable:

 Public Class Form1
  
   Private Sub Button1_Click(ByVal sender As System.Object, 
  
      ByVal e As System.EventArgs) Handles Button1.Click
  
     Dim weeks As New Hashtable
  
     Dim day As DictionaryEntry
  
     weeks.Add("1", "Sun")
  
     weeks.Add("2", "Mon")
  
     weeks.Add("3", "Tue")
  
     weeks.Add("4", "Wed")
  
     weeks.Add("5", "Thu")
  
     weeks.Add("6", "Fri")
  
     weeks.Add("7", "Sat")
  
     'Display a single Item
  
     MsgBox(weeks.Item("5"))
  
     'Search an Item
  
     If weeks.ContainsValue("Tue") Then
  
       MsgBox("Find")
  
     Else
  
       MsgBox("Not find")
  
     End If
  
     'remove an Item
  
     weeks.Remove("3")
  
     'Display all key value pairs
  
     For Each day In weeks
  
       MsgBox(day.Key " -- " day.Value)
  
     Next
  
   End Sub
  
 End Class
To add an object to a queue, the Enqueue method is invoked. There is only constructor for this method, with a single argument: the object to be added to the Queue. Any object can be added to an instance of the Queue class - from basic objects such as integers and strings to other classes and even other Queue classes. It is also possible to add null values using the VB.NET Nothing keyword or the System.DBNull.Value class. Each Queue can contain many different types of objects. The code below shows a variety of objects added to an instance of a Queue class:
 Dim MyQueue As New Queue(4)
  
 Dim MyOtherQueue As New Queue
  
 Dim MyNumber As Integer = 24
  
 Dim MyText As String = "ASP Documentation Tool"
  
 MyQueue.Enqueue(MyNumber)
  
 MyQueue.Enqueue(MyText)
  
 MyQueue.Enqueue(MyOtherQueue)
  
 MyQueue.Enqueue(System.DBNull.Value)

No comments:

Post a Comment