Warning: Undefined array key "amp-addthis" in /home/tgagmvup/onlinestudy.guru/wp-content/plugins/addthis/backend/AddThisSharingButtonsFeature.php on line 101
lang="en-US"> Visual Language Programming (May-2017) Unit-I - onlinestudy.guru
Site icon onlinestudy.guru

Visual Language Programming (May-2017) Unit-I

Question: -2(a)  What are the types of projects created in VB? Explain them.

Answer: -If you examine the Visual Basic project types, you see that many of them are different from what you are used to with VB6. Some of the major project types are:

These are the basic types of applications you can create. You can also create an empty project (for Windows applications, class libraries, and services) or an empty Web Application (for Web applications).




Question: -2(b) How dynamic arrays are created and modified in VB? Give an example.

Answer: –

An array is a consecutive group of memory locations that all have the same name and the same type. To refer to a particular location or element in the array, we specify the array name and the array element position number.Arrays are a variant type variable that you can use in VBA coding to store a list of data. Think of it as a mini-spreadsheet inside of a single variable. You store data into an array by referring to a reference number that corresponds with the location that the piece of data is positioned in.

Below is an example of an array that is holding all the month names within a year. Notice that the reference number starts at zero instead of one.

Arrays occupy space in memory. The programmer specifies the array type and the number of elements required by the array so that the compiler may reserve the appropriate amount of memory. Arrays may be declared as Public (in a code module), module or local. Module arrays are declared in the general declarations using keyword Dim or Private. Local arrays are declared in a procedure using Dim or Static. Array must be declared explicitly with keyword “As”.

There are two types of arrays in Visual Basic namely:

Fixed-size array : The size of array always remains the same-size doesn’t change during the program execution.

Dynamic array : The size of the array can be changed at the run time- size changes during the program execution.

Fixed-sized Arrays

When an upper bound is specified in the declaration, a Fixed-array is created. The upper limit should always be within the range of long data type.

Declaring a fixed-array

Dim numbers(5) As Integer

In the above illustration, numbers is the name of the array, and the number 6 included in the parentheses is the upper limit of the array. The above declaration creates an array with 6 elements, with index numbers running from 0 to 5.

If we want to specify the lower limit, then the parentheses should include both the lower and upper limit along with the To keyword. An example for this is given below.

Dim numbers (1 To 6 ) As Integer

In the above statement, an array of 10 elements is declared but with indexes running from 1 to 6.

A public array can be declared using the keyword Public instead of Dim as shown below.

Public numbers(5) As Integer

Multidimensional Arrays

Arrays can have multiple dimensions. A common use of multidimensional arrays is to represent tables of values consisting of information arranged in rows and columns. To identify a particular table element, we must specify two indexes: The first (by convention) identifies the element’s row and the second (by convention) identifies the element’s column.

Tables or arrays that require two indexes to identify a particular element are called two dimensional arrays. Note that multidimensional arrays can have more than two dimensions. Visual Basic supports at least 60 array dimensions, but most people will need to use more than two or three dimensional-arrays.

The following statement declares a two-dimensional array 50 by 50 array within a procedure.

Dim AvgMarks ( 50, 50)

It is also possible to define the lower limits for one or both the dimensions as for fixed size arrays. An example for this is given here.

Dim Marks ( 101 To 200, 1 To 100)

An example for three dimensional-array with defined lower limits is given below.

Dim Details( 101 To 200, 1 To 100, 1 To 100)

Static and dynamic arrays

Basically, you can create either static or dynamic arrays. Static arrays must include a fixed number of items, and this number must be known at compile time so that the compiler can set aside the necessary amount of memory. You create a static array using a Dim statement with a constant argument:

‘ This is a static array.
Dim Names(100) As String

Visual Basic starts indexing the array with 0. Therefore, the preceding array actually holds 101 items.

Most programs don’t use static arrays because programmers rarely know at compile time how many items you need and also because static arrays can’t be resized during execution. Both these issues are solved by dynamic arrays. You declare and create dynamic arrays in two distinct steps. In general, you declare the array to account for its visibility (for example, at the beginning of a module if you want to make it visible by all the procedures of the module) using a Dim command with an empty pair of brackets. Then you create the array when you actually need it, using a ReDim statement:

‘ An array defined in a BAS module (with Private scope)
Dim Customers() As String

Sub Main()
‘ Here you create the array.
ReDim Customer(1000) As String
End Sub

If you’re creating an array that’s local to a procedure, you can do everything with a single ReDim statement:

Sub PrintReport()
‘ This array is visible only to the procedure.
ReDim Customers(1000) As String
‘ …
End Sub

If you don’t specify the lower index of an array, Visual Basic assumes it to be 0, unless an Option Base 1 statement is placed at the beginning of the module. My suggestion is this: Never use an Option Base statement because it makes code reuse more difficult. (You can’t cut and paste routines without worrying about the current Option Base.) If you want to explicitly use a lower index different from 0, use this syntax instead:

ReDim Customers(1 To 1000) As String

Dynamic arrays can be re-created at will, each time with a different number of items. When you re-create a dynamic array, its contents are reset to 0 (or to an empty string) and you lose the data it contains. If you want to resize an array without losing its contents, use the ReDim Preserve command:

ReDim Preserve Customers(2000) As String

When you’re resizing an array, you can’t change the number of its dimensions nor the type of the values it contains. Moreover, when you’re using ReDim Preserve on a multidimensional array, you can resize only its last dimension:

ReDim Cells(1 To 100, 10) As Integer

ReDim Preserve Cells(1 To 100, 20) As Integer ‘ This works.
ReDim Preserve Cells(1 To 200, 20) As Integer ‘ This doesn’t.

Finally, you can destroy an array using the Erase statement. If the array is dynamic, Visual Basic releases the memory allocated for its elements (and you can’t read or write them any longer); if the array is static, its elements are set to 0 or to empty strings.

You can use the LBound and UBound functions to retrieve the lower and upper indices. If the array has two or more dimensions, you need to pass a second argument to these functions to specify the dimension you need:

Print LBound(Cells, 1) ‘ Displays 1, lower index of 1st dimension
Print LBound(Cells) ‘ Same as above
Print UBound(Cells, 2) ‘ Displays 20, upper index of 2nd dimension
‘ Evaluate total number of elements.
NumEls = (UBound(Cells) _ LBound(Cells) + 1) * _
(UBound(Cells, 2) _ LBound(Cells, 2) + 1)




Question: -3(a) what are procedures? What are the different types of procedures in VB? Also explain the mode of passing parameters to procedures.

Answer: –

A procedure is a block of code that performs some operation. The events we have been using so far are a special form of procedure known as an event procedure.
For example, associating code with a CommandButton to quit an application is a procedure.
The basic Syntax for a procedure is:
[Private | Public][Static] Sub procName ([arglist])
Parts of the Procedure
Part Description
Public Indicates that the procedure is available to all modules. If Option Private is used in the module, the procedure is not available to modules outside of the project.
Private Indicates that the procedure is only available to other procedures or functions in the current module or form.
Static Indicates that all variables declared within the procedure are retained, even when the procedure is out of scope.
procName The name of the procedure. Must be unique to the module if declared Private, otherwise unique to the project. The name of the procedure follows the naming rule for Variables.
arglist A list of variables passed to the procedure as arguments, and their data types. Multiple arguments are separated by commas. Arguments may be Optional, and may be Read Only.
 

The following example is a Private Procedure to print the sum of two numbers on the Form.

Private Sub printSum(ByVal x As Integer, ByVal y As Integer)
Debug.Print Str(x + y)
End Sub

 

Types of Procedures

Visual Basic uses several types of procedures:

Some functions written in C# return a reference return value. Function callers can modify the return value, and this modification is reflected in the state of the called object. Starting with Visual Basic 2017, Visual Basic code can consume reference return values, although it cannot return a value by reference. For more information, see Reference return values.

Passing Parameters by Value

This is the default mechanism for passing parameters to a method. In this mechanism, when a method is called, a new storage location is created for each value parameter. The values of the actual parameters are copied into them. So, the changes made to the parameter inside the method have no effect on the argument.

In VB.Net, you declare the reference parameters using the ByVal keyword. The following example demonstrates the concept:

Module paramByval

Sub swap(ByVal x As Integer, ByVal y As Integer)

Dim temp As Integer

temp = x ‘ save the value of x

x = y    ‘ put y into x

y = temp ‘put temp into y

End Sub

Sub Main()      ‘

‘local variable definition

Dim a As Integer = 100

Dim b As Integer = 200

Console.WriteLine(“Before swap, value of a : {0}”, a)

Console.WriteLine(“Before swap, value of b : {0}”, b)      ‘ calling a function to swap the values ‘

swap(a, b)

Console.WriteLine(“After swap, value of a : {0}”, a)

Console.WriteLine(“After swap, value of b : {0}”, b)

Console.ReadLine()

End Sub

End Module

When the above code is compiled and executed, it produces the following result:

Before swap, value of a :100Before swap, value of b :200After swap, value of a :100After swap, value of b :200

It shows that there is no change in the values though they had been changed inside the function.

Passing Parameters by Reference

A reference parameter is a reference to a memory location of a variable. When you pass parameters by reference, unlike value parameters, a new storage location is not created for these parameters. The reference parameters represent the same memory location as the actual parameters that are supplied to the method.

In VB.Net, you declare the reference parameters using the ByRef keyword. The following example demonstrates this:

Module paramByref

Sub swap(ByRef x As Integer, ByRef y As Integer)

Dim temp As Integer

temp = x ‘ save the value of x

x = y    ‘ put y into x

y = temp ‘put temp into y

End Sub

Sub Main()

‘ local variable definition

Dim a As Integer = 100

Dim b As Integer = 200

Console.WriteLine(“Before swap, value of a : {0}”, a)

Console.WriteLine(“Before swap, value of b : {0}”, b)      ‘ calling a function to swap the values ‘

swap(a, b)

Console.WriteLine(“After swap, value of a : {0}”, a)

Console.WriteLine(“After swap, value of b : {0}”, b)

Console.ReadLine()

End Sub

End Module

When the above code is compiled and executed, it produces the following result:

Before swap, value of a : 100Before swap,

value of b : 200After swap,

value of a : 200After swap,

value of b : 100




Question:- 3(b) Explain important components of VB IDE which make it powerful development framework.

Answer: – An Integrated Development Environment (IDE) is software that facilitates application development. In the context of .NET-based applications, Visual Studio is the most commonly used IDE. Some of the key features included are:

Visual Basic .NET (VB.NET) is an object-oriented computer programming language implemented on the .NET Framework. Although it is an evolution of classic Visual Basic language, it is not backwards-compatible with VB6, and any code written in the old version does not compile under VB.NET.

The following reasons make VB.Net a widely used professional language:

Strong Programming Features VB.Net

VB.Net has numerous strong programming features that make it endearing to multitude of programmers worldwide. Let us mention some of these features:

VISUAL BASIC IDE (INTEGRATED DEVELOPMENT ENVIRONMENT ) is the environment in which its program are written and executed. IDE stands for Integrated Development Environment. IDE is a term commonly used to describe the interface and environment for creating your application. It is called IDE because you can access all of the development tools that you need for developing an application.

VISUAL BASIC IDE contains different components. These components are:
Tool Bar:-

It provides quick access to commonly used commands in the programming environment. You click a button on the toolbar to carry out the action represented by that button. The Standard toolbar is displayed when you start Visual Basic. You can change toolbar settings by selecting Toolbars option form View menu.

Toolbar can be removed from beneath of the menu bar, if you click on the Left Edge and drag it away from the menu bar.

Form Designer:-

Form objects are the basic building blocks of Visual Basic application.It is the actual window with which a user interacts at the start of application. Forms have their own properties, events, and methods with which you can control their appearance and behavior.

The Form Window is central to developing Visual Basic applications.It is the window where you draw your application

Tool Box:-

The Toolbox is a long box with many different icons on it. It is the selection menu for controls (objects) used in your application. To work in Visual Basic we’ve to know about its tools so that we can make different forms, projects and how to edit them.

Property Window:-

Properties Window is used to establish initial property values for objects (Like Form etc.). The drop-down box at the top of the window lists all objects in the current form.Two views are available: Alphabetic and Categorized.

If you are not getting property window on IDE then click on Project Explorer option from view menu. Or, Use key F4 or Alt+V+W for displaying property window.

Project Explorer:-

Project Explorer Window displays a list of all forms used in making up your application. You can also obtain a view of the Form or Code windows (window containing the actual Basic coding) from Project Explorer window.

If you are not viewing Project Explorer on IDE then click on Project Explorer option from the view menu. OR, Use Ctrl+R for displaying Project Explorer Window.

Menu Bar:-

It is a horizontal strip that appears across the top of the screen and contains the names of menus.Menu Bar lists the menus that you can use in the active window.

You can modify the menu bar using the Commands tab of the Customize dialog box. For that go to the View menu ,then select Toolbars. Now Click on the customize option. OR, You can use key combination Alt+V+T+C for that.




This article is contributed by Tarun Jangra. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


 

click here for solved UNIT-II

Exit mobile version