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:

  • Windows Application—This is a standard executable, in VB6 terminology. It is the way to create applications with a Windows interface, using forms and controls. This is as close to “your father’s VB” as you’ll get in VB.NET.
  • Class Library—This project type allows you to create classes that will be used in other applications. Think of it as similar to the COM components that you have been building, which VB6 called the ActiveX DLL and ActiveX EXE project types.
  • Windows Control Library—This project type is for creating what used to be called ActiveX controls. This type allows you to create new controls to be used in Windows applications.
  • Web Application—Goodbye, Visual InterDev. Goodbye, scripting languages on the server. Visual Basic now has Web Application projects, which use ASP.NET to create dynamic Web applications. These projects allow you to create HTML, ASP.NET, and VB files. Your Web applications move beyond the simple request/response mode of typical Web applications to be event-driven.
  • Web Service—If you’ve used VB6 to create COM components and then made them available over HTTP with SOAP, you understand the concept of Web Services. Web Service projects are components that you make available to other applications via the Web; the underlying protocol is HTTP instead of DCOM, and you are passing requests and receiving responses behind the scenes using XML. Some of the major promises of Web Service projects are that they are all standards-based and are platform-independent. Unlike DCOM, which was tied to a COM (that is, Windows) infrastructure, Web Service projects can be placed on any platform that supports .NET, and can then be called by any application using simple HTTP calls.
  • Web Control Library—As with Web Service projects, there’s no exact match back to VB6 for the Web Control Library projects. Thanks to the new Web Application projects in VB.NET, you can add controls to Web pages just like you would in a standard Windows Application, but VB.NET makes them HTML controls at runtime. You can design your own controls that can then be used by Web applications.
  • Console Application—Many of the Windows administrative tools are still console (or command-line, or DOS) applications. Previously, you didn’t have a good way to create these tools in VB, and you instead had to rely on C++. Now console applications are natively supported by VB.NET.
  • Windows Services—As with console applications, there was no good way to create Windows services in previous versions of VB. Windows services, of course, are programs that run in the background of Windows and can automatically start when the machine is booted, even if no one logs in.

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:

  • Sub Proceduresperform actions but do not return a value to the calling code.
  • Event-handling procedures are Subprocedures that execute in response to an event raised by user action or by an occurrence in a program.
  • Function Proceduresreturn a value to the calling code. They can perform other actions before returning.

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.

  • Property Proceduresreturn and assign values of properties on objects or modules.
  • Operator Proceduresdefine the behavior of a standard operator when one or both of the operands is a newly-defined class or structure.
  • Generic Procedures in Visual Basicdefine one or more type parameters in addition to their normal parameters, so the calling code can pass specific data types each time it makes a call.
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:

  • Single IDE for all .NET applications. Therefore no switching required to other IDEs for developing .NET applications
  • Single .NET solution for an application which has been built on code written in multiple languages
  • Code editor supporting Intellisense and code refactoring
  • Compilation from within the environment based on defined configuration options
  • Integrated debugger that works at source and machine level
  • Plug-in architecture that helps to add tools for domain specific languages
  • Customizable environment to help the user to configure the IDE based on the required settings
  • Browser that is built-in within the IDE helps to view content from internet such as help, source-code, etc. in online mode.

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:

  • Modern, general purpose.
  • Object oriented.
  • Component oriented.
  • Easy to learn.
  • Structured language.
  • It produces efficient programs.
  • It can be compiled on a variety of computer platforms.
  • Part of .Net Framework.
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:

  • Boolean Conditions
  • Automatic Garbage Collection
  • Standard Library
  • Assembly Versioning
  • Properties and Events
  • Delegates and Events Management
  • Easy-to-use Generics
  • Indexers
  • Conditional Compilation
  • Simple Multithreading

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

Author: Tarun Jangra

2 thoughts on “Visual Language Programming (May-2017) Unit-I

  1. Question 3 (b) asking for important components and i think these are : Toolbar, Properties Window, Immediate window, Code Window, Design Window, etc

Leave a Reply

Your email address will not be published. Required fields are marked *