Wednesday, April 24, 2013

B.Sc. IT BT0082 (Semester 4, Visual Basic) Assignment


Spring 2013
Bachelor of Science in Information Technology (BSc IT) – Semester 4
BT0082 – Visual Basic – 4 Credits
(Book ID: B1093)
Assignment Set (60 Marks)

1.      Explain the method of creating a new project in VB.Net.
Ans.-   Start Microsoft Visual Studio.Net and from the menu select FileàNewàProject. A “New Project “ dialog will now be displayed. Select “Visual Basic Project” from “Project Type” and select “Console Application” from “Templates”. Type “MyHelloWorldApplicatio( project name)” in the “Name” text box below, then click OK.


This will show you the initial default code for your Hello World Application.


Change the name of module from Module1 to “MyHelloWorldApplication” and type Console.WriteLine(“Hello World”) inside the Sub Main() function’ body like whats show below:
Module MyHelloWorldApplication
   Sub Main()
      Console.WriteLine(“Hello World”)
   End Sub
End Module

To compile and execute your application, select “Start” from the “Debug” menu or to run the application without Debug press Ctrl+F5. A new console window containing the words “Hello World” will now be displayed. Press any key to terminate the program and close the console window.

2.      Explain Code Editor Window and Solution Explorer.
Ans.-   The Code Editor Window:- The Code Editor Window, shown in below figure. After you have designed the user interface for your project by placing controls on the form, you will turn to the Code Editor to develop the Visual Basic statements that make the controls functional. The easiest way to call up the Code Editor is to double-click a control. Then, you can begin typing the Visual Basic statements that will be executed when the user performs the most common action on that control. If you double-click on a button, for example, you can enter the statements that will be executed when the user clicks on that button.
The Code Editor works much like other text editors you’ve worked with. However, the Code Editor has a number of special features that simplify the task of editing Visual Basic code. For example, color is used to distinguish Visual Basic keywords from variables, comments, and other language elements. And many types of coding errors are automatically highlighted as you type so you can correct them. You’ll learn more about working with the Code Editor in the next chapter.
Note that the Code Editor and the Form Designer provide two different ways to work with the same Visual Basic source file. The Code Editor lets you work directly with the Visual Basic statements that make up your application. The Form Designer presents a visual representation of the forms and controls that are implemented by that code.

           

The Code Editor window is where you create and edit the Visual Basic code that your application requires. The Code Editor works much like any other text editor you have used, so you shouldn’t have much trouble learning how to use it. You can display the Code Editor by double-clicking the form or one of the controls in the Form Designer window. Or, you can click the View Code button in the Solution Explorer. Once you’ve opened the Code Editor, you can return to the Form Designer by clicking the (Design) tab at the top of the Code Editor or the View Designer button in the Solution Explorer (to the right of the View Code button). You can also move among these windows by pressing Ctrl+Tab or Shift+Ctrl+Tab.
It’s important to realize that the Form Designer and the Code Editor do not represent two different files. Instead, they provide you with two views of the same Visual Basic source file. The Form Designer gives you a visual representation of the form that is implemented by your Visual Basic code. The Code Editor lets you edit the code for the form.

The Solution Explorer:- Below figure shows the Solution Explorer, which you use to manage the projects that make up a solution and the files that make up each project. As you can see, the files in the Solution Explorer are displayed in a tree view with the files that make up a project subordinate to the project container and the project container subordinate to the solution container. If a container has a plus sign next to it, you can click on the plus sign to display its contents. Conversely, you can hide the contents of a container by clicking on the minus sign next to it.
You can also use the buttons at the top of the Solution Explorer to work with the files in a project. To display the code for a form, for example, you can highlight the form file and then click on the View Code button. And to display the user interface for a form, you can highlight the form file and then click on the View Designer button. For a code file, only the View Code button is available since a code file doesn’t contain a user interface.
To identify the files that make up a project, you can look at the icon that’s displayed to the left of the file name. The icon for a form file, for example, is a form, and the icon for a code file is a document and the letters VB. As you can see, this project consists of three form files and two code files.
Note, however, that all of the files have the file extension vb regardless of their contents. Because of that, I recommend that you give your files names that identify their contents. For example, we add the prefix frm to the names of our form files. That way, its easy to identify the form files when you work with them outside of the Solution Explorer.
The last four files shown in this figure are custom files that we developed for this project. In contrast, the first file (AssmblyInfo) was added automatically when this project was created. This file receives information whenever the project is compiled into an assembly.



You use the Solution Explorer to manage and display the files and projects in a solution. The Solution Explorer lists all of the projects for the current solution, as well as all of the files that make up each project. Plus (+) and minus (-) signs in the Solution Explorer indicate groups of files. You can click on these signs to expand and collapse the groups. You can perform most of the functions you need using the buttons at the top of the Solution Explorer window. The buttons you’ll use most are the View Code and View Designer buttons, which open the Code Editor and Form Designer windows, and the View Properties button, which opens the Properties window.

3.      Write a VB program to short and display an array of integers in ascending and descending order using the array concept in VB.Net.
Ans.-   Short an array in Ascending Order

Private Sub Form_Load()
 Dim LngArr(1 To 100) As Long
 Dim Var As Variant
 Dim i As Long
 For i = 1 To 100
  LngArr(i) = 100 - i
 Next
 BubbleSort LngArr
 For i = LBound(LngArr) To UBound(LngArr)
  Debug.Print i, LngArr(i)
 Next

 Var = Array(2, 43, 12, 56, 1, 33, 91)
 BubbleSort Var
 For i = LBound(Var) To UBound(Var)
  Debug.Print i, Var(i)
 Next
End Sub
Private Sub BubbleSort(VArr As Variant)
 Dim LB As Long, UB As Long
 Dim i As Long, j As Long
 Dim Tmp As Variant
 On Error GoTo ErrSort 'array not dimmed
 LB = LBound(VArr)     'lower bound
 UB = UBound(VArr)     'upper bound
 For i = LB To UB - 1
  For j = i + 1 To UB
   If VArr(i) > VArr(j) Then
    Tmp = VArr(i)       'swap elements
    VArr(i) = VArr(j)
    VArr(j) = Tmp
   End If
  Next
 Next
 Exit Sub
ErrSort:
 MsgBox Err.Description
End Sub


Result:-  1, 2, 12, 33, 43, 56, 91

Short an array in Descending Order

Private Sub Form_Load()
 Dim LngArr(100 To 1) As Long
 Dim Var As Variant
 Dim i As Long
 For i = 100 To 1
  LngArr(i) = I - 100
 Next
 BubbleSort LngArr
 For i = LBound(LngArr) To UBound(LngArr)
  Debug.Print i, LngArr(i)
 Next

 Var = Array(2, 43, 12, 56, 1, 33, 91)
 BubbleSort Var
 For i = LBound(Var) To UBound(Var)
  Debug.Print i, Var(i)
 Next
End Sub
Private Sub BubbleSort(VArr As Variant)
 Dim LB As Long, UB As Long
 Dim i As Long, j As Long
 Dim Tmp As Variant
 On Error GoTo ErrSort 'array not dimmed
 LB = LBound(VArr)     'lower bound
 UB = UBound(VArr)     'upper bound
 For i = LB To UB - 1
  For j = i - 1 To UB
   If VArr(i) < VArr(j) Then
    Tmp = VArr(i)       'swap elements
    VArr(i) = VArr(j)
    VArr(j) = Tmp
   End If
  Next
 Next
 Exit Sub
ErrSort:
 MsgBox Err.Description
End Sub


Result:-  91, 56, 43, 33, 12, 2, 1


4.      Explain arithmetic and relational operators in VB.Net.
Ans.-   Arithmetic Operators:- Several common arithmetic operators are allowed in VB.Net given in the following table
           
Arithmetic Operator
Meaning
+
Add
-
Subtract
*
Multiply
/
Divide
Mod
Remainder of modulo

The program below uses three operators
Imports System
Module ArithmeticOperators
   ‘The program shows the use of arithmetic operators
   ‘+-*/Mod
   Sub Main()
      ‘result of addition, subtraction, multiplication and modulus operator
      Dim sum, difference, product, modulo As Interger
      Sum = 0
      difference = 0
      product = 0
      modulo = 0
      Dim quotient As Double = 0 ‘result of division
      Dim num1 As Interger = 10 ‘operand variables
      Dim num2 As Interger = 2
      sum = num1+num2
      difference = num1-num2
      product = num1*num2
      quotient = num1/num2
      modulo 3 Mod num2       ‘remainder of 3/2
      Console.WriteLine(“num1 = {0}, sum2 = {1}”, num1, num2)
      Console.WriteLine()
      Console.WriteLine(“Sum of {0} and {1} is {2}”, num1, num2, sum)
      Console.WriteLine(“Difference of {0} and {1} is {2}”, num1, num2, difference)
      Console.WriteLine(“Product of {0} and {1} is {2}”, num1, num2, product)
      Console.WriteLine(“Quotient of {0} and {1} is {2}”, num1, num2, quotient)
      Console.WriteLine()
      Console.WriteLine(“Remainder when 3 is divided by {0} is {1}”, num2, modulo)
      End Sub
End Module

            Relational Operators:- Relational operators are used to comparison purposes in conditional statements. The common relational operators in VB.Net are:
Relational Operator
Meaning
=
Equality check
<> 
Un-equality check
> 
Greater than
< 
Less than
<=
Less than or equal to
>=
Greater than or equal to

            Relational operators always result in Boolean statement, either True or False. For example if we have two variables
                        Dim num1 = 5, num2 = 6 As Interger
then,
                        num1 = num2              will result in false
                        num1<>num2              will result in true
                        num1>num2                will result in false
                        num1<num2                will result in true
                        num1<=num2              will result in true
                        num1>=num2              will result in false

Only compatible data types can be compared. It is invalid to compare a Boolean with and Integer, if
                        Dim I = 1 As Integer
                        Dim b = True As Boolean
thin it is a syntax error to compare i and b for equality (i=b)

5.      Explain If……Then……Else statement in VB.Nett with an example.
Ans.-   Condition checking has always been the most basic and important construct in any language. VB.Net provides conditional statements in the form of “If…..Then…...Else” statement. The structure of this statement is---
If Boolean expression Then
          Statement or block of statement
Else
          Statement of block of statement
End If

Example:-
           
If i=100 Then
            Console.WriteLine(“You reached hundred”)
Else
            Console.WriteLine(“Still you are not reached hundred”)
End If

            Only the first message will be printed in the case of i being 100. In any other case (when i is not 100), the second message will be printed. You can also use a block of statements (more than one statement) under any If and Else.

6.      Describe the following methods of the Object class in VB.Net:
a.      Equals and ReferenceEquals:- The Object class supplies two versions of Equals, one shared and one not.
The shared version has this syntax-----
Overloads Public Shared Function Equals(Object, Object) As Boolean
and is used in this form
     Equals(a,b)
The non-shared version has the syntax-----
     Overloads Overridable Public function Equals(Object) As Boolean
and is used in this form
     a.equals(b)
The two versions of the Equals method are designed to determine whether two items have the same value, but you should be prepared to overload equals if it makes sense in your class. Keep in mind that because shared members of classes cannot be overridden, you can only override the non-shared version of Equals. For example if you have two ways of representing objects in a value type, you should make sure that Equals can handle this (This designers did this for the String class as well, although strictly speaking, this is not a value type.)
     The Object class also provides a shared (not overridable) version of a method called ReferenceEquals. The ReferenceEquals method determines whether two items refer to the same object; that is, whether the specified Object instances are the same instance. For example, two strings, a and b, can have a.equals(b) true and ReferenceEquals(a,b) false, as this code shows-----
Sub Main()
Dim a As String = “hello”
Dim b As String = “Hello”
Mid(b, 1, 1) = “h”
Console.WriteLine(“is a.Equals(b)true?” & a.Equals(b))
Console.WriteLine(“is ReferenceEquals(a,b)true?” & ReferenceEquals(a,b))
Console.WriteLine()
End Sub

Is a.Equals(b)true? True
Is ReferenceEqualss(a,b) true? False

b.      MemberWiseClone:-
The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.
For example, consider an object called X that references objects A and B. Object B, in turn, references object C. A shallow copy of X creates new object X2 that also references objects A and B. In contrast, a deep copy of X creates a new object X2 that references the new objects A2 and B2, which are copies of A and B. B2, in turn, references the new object C2, which is a copy of C. The example illustrates the difference between a shallow and a deep copy operation.
There are numerous ways to implement a deep copy operation if the shallow copy operation performed by the MemberwiseClone method does not meet your needs. These include the following:
·         Call a class constructor of the object to be copied to create a second object with property values taken from the first object. This assumes that the values of an object are entirely defined by its class constructor.
·         Call the MemberwiseClone method to create a shallow copy of an object, and then assign new objects whose values are the same as the original object to any properties or fields whose values are reference types. The DeepCopy method in the example illustrates this approach.
·         Serialize the object to be deep copied, and then restore the serialized data to a different object variable.
·         Use reflection with recursion to perform the deep copy operation.


For BT0081 click here                  For More Assignments Click Here                 For BT0083 click here

No comments:

Post a Comment