Showing posts with label arrays. Show all posts
Showing posts with label arrays. Show all posts

Tuesday, May 10, 2011

Arrays Assignment

1. Create a string array that
will store the days of the week. The array should fill when the form loads. Add a button (you can drag it on the
form). When the button is clicked,
loop through the array and pop up a message box for each day of the
week.

  Private Sub Button1_Click(ByVal sender As System.Object,
  
  ByVal e As System.EventArgs) Handles Button1.Click
  
     Dim week As New Hashtable
  
     Dim daysofweek As DictionaryEntry
  
     week.Add("1", "Sunday")
  
     week.Add("2", "Monday")
  
     week.Add("3", "Tuesday")
  
     week.Add("4", "Wednesday")
  
     week.Add("5", "Thursday")
  
     week.Add("6", "Friday")
  
     week.Add("7", "Saturday")
  
     For Each daysofweek In week
  
       MsgBox("" & daysofweek.Value)
  
     Next
  
   End Sub
  
 End Class  


2. Create a form with textboxes
for the following:
 - first name
 - last name
 - email.
Add labels to describe each box.
Add a button for adding the user. The add user button click event will do the following:
 - add the user information
into a hashtable
 - clear the text from the
text boxes

We want to add the information into the hashtable using three keys:
FirstName, LastName, Email.

Add First Name, Last Name, and Email buttons to the bottom of the
form. Each of these buttons is
going to fire off a message box that will show the information that was
just added in.


 Public Class Form1
  
   Dim adduser As New Hashtable
  
   Private Sub btFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btFirst.Click
  
     MessageBox.Show(txtFirst.Text)
  
   End Sub
  
   Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
  
     MessageBox.Show(txtLast.Text)
  
   End Sub
  
   Private Sub btnEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmail.Click
  
     MessageBox.Show(txtEmail.Text)
  
   End Sub
  
   Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  
     adduser.Clear()
  
     adduser.Add("txtFirst", txtFirst.Text)
  
     adduser.Add("txtLast", txtLast.Text)
  
     adduser.Add("txtEmail", txtEmail.Text)
  
     txtFirst.Clear()
  
     txtLast.Clear()
  
     txtEmail.Clear()
  
   End Sub
  
 End Class  



  1. Create a form that will work
    as a printer queue. This form will
    have the following inputs:
    1. Title of the
      document
    2. Pages to be printed

When
submitted the click event will do the following:
  1. Add the title and pages to a
    hashtable
  2. Add the hashtable to a queue
    which is a class variable
  3. Clear the title and set the
    pages back to one
  4. Refresh the queue list.

The
Clear Current button will:
  1.  remove the next item in the queue
  2. Refresh the queue list
    box 
 Public Class Printer
  
   Dim jobs As New Queue
  
   Private Sub btnSendJob_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendJob.Click
  
     Dim qlist As New Hashtable
  
     jobs.Enqueue(qlist)
  
     qlist.Add("Title", txtTitle.Text)
  
     qlist.Add("Pages", NumericUpDown1.Value)
  
     refreshalljobs()
  
     NumericUpDown1.Value = 1
  
     txtTitle.Text = ""
  
   End Sub
  
   Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
  
     If jobs.Count > 0 Then
  
       jobs.Dequeue()
  
     End If
  
     refreshalljobs()
  
   End Sub
  
   Private Sub refreshalljobs()
  
     qlist.Items.Clear()
  
     For Each hash As Hashtable In jobs
  
       qlist.Items.Add(hash.Item("Title") & "(" & hash.Item("Pages") & ")")
  
     Next
  
   End Sub
  
 End Class  

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)