2.BYTE: Visual Basic

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

What is the full form of IDE simulation programs such as Projectile, Simple Harmonic Motion, Star

IDE stands for Integrated Development Environment. It is a War etc. If you are in business, you can also create business
programming environment that contains a lot of things in a single applications such as inventory management system , Amortization
package i.e. code editor, compiler, debugger and what you see is what Calculator , investments calculator, point-of-sale system, payroll
you get (WYCIWYG). It is actually a software application that provides system, accounting program and more to help manage your business
comprehensive facilities to computer programmers for software and increase productivity. For those of you who like games , you can
development. It combines all the basic tools that developers need to create programs such as slot machine, reversi, tic tac toe and more.
write or test software. This type of environment allows an application Besides, you can create multimedia programs such as Smart Audio
developer to write code while compiling, debugging and executing it at Player, Multimedia Player and more Visual Basic Data Types
the same place. It can be a standalone application or a part of one or In Visual Basic, Data Types are useful to define a type of data the
more compatible applications. variable can hold, such as integer, float, string, etc., in our application.
For example: The IDE for developing .NET applications is Microsoft Visual Basic is a Strongly Typed programming language. Before we
Visual Studio and IDE for developing Java Application is Eclipse, perform any operation on a variable, it’s mandatory to define a variable
NetBeans, JDeveloper, MyEclipse, BlueJ, RSA etc. with a required data type to indicate what type of data the variable can
In an Integrated development environment we can develop, run ,test, hold in our application.
and debug our application that is, an IDE has three distinct states - DATATYPES IN VISUAL BASIC
Design, Run, Debug. The visual basic provide an IDE which also has 1.Boolean Data type
three distinct state, the current state appear in the title bar. The VB IDE Holds values that can be only True or False. The
composed of these parts:- 1.The menu bar 2.The tool bar 3.The project keywords True and False correspond to the two states
explorer 4.The properties window 5.The form layout window 6.The tool of Boolean variables.
box 7.The form designer 8.The code window Dim male As Boolean
An IDE typically contains a code editor, a compiler or interpreter, and a 2.BYTE
debugger, accessed through a single graphical user interface (GUI).
Holds unsigned 8-bit (1-byte) integers that range in value from 0 through
The user writes and edits source code in the code editor. The compiler
255.
translates the source code into a readable language that is executable
Dim byteValue As Byte = 201
for a computer. And the debugger tests the software to solve any issues
3.Char Data Type
or bugs.
Holds unsigned 16-bit (2-byte) code points ranging in value from 0
Subversion.
through 65535. Each code point, or character code, represents a single
Unicode character.
VISUAL BASIC Dim charVar As Char
 In the early days of computer programming even a single program 4.Date Data Type
could take days or even weeks to complete. When Visual Basic was first Holds IEEE 64-bit (8-byte) values that represent dates ranging from
introduced in 1991, it revolutionized programming we no longer needed January 1 of the year 0001 through December 31 of the year 9999, and
to write code to create a user interface, and we no longer needed to times from 12:00:00 AM (midnight) through 11:59:59.9999999 PM. Each
worry about memory management. This new way of programming was increment represents 100 nanoseconds of elapsed time since the
known as Rabid Application Development, or RAD. The main benefit of beginning of January 1 of the year 1 in the Gregorian calendar. The
RAD programming is increased productivity – Visual Basic of 6.0 has maximum value represents 100 nanoseconds before the beginning of
many features that can help us to create better applications, faster than January 1 of the year 10000.
ever before, the following are few of features1. Code snipper ->small Dim someDateAndTime As Date = #8/13/2002 12:14 PM#
block 2. Development with Mg 3. Intelligence. 5.Integer data type
Visual Basic is the fastest and easiest way to create single user The Integer data type provides optimal performance on a 32-bit
applications for Microsoft Windows. It is an Object-Oriented processor. The other integral types are slower to load and store from
Programming development system for creating applications that run and to memory.
under any of the Microsoft Windows Environment. In the name Visual The default value of Integer is 0.
Basic, 8Visual9 part refers to the method used to create the Graphical Dim intValue1 As Integer = 90946
User Interface. The 8Basic9 part refers to the Basic programming 6.Decimal Data Type
language, as its basic syntax of statements, is retained by Visual Basic. Holds signed 128-bit (16-byte) values representing 96-bit (12-byte)
Visual Basic is an event driven programming language. This means that integer numbers scaled by a variable power of 10. The scaling factor
code is executed as a response to an event. specifies the number of digits to the right of the decimal point; it ranges
Visual Basic is the fastest and easiest way to create single user from 0 through 28. With a scale of 0 (no decimal places), the largest
applications for Microsoft Windows. It is an Object-Oriented possible value is +/-79,228,162,514,264,337,593,543,950,335 (+/-
Programming development system for creating applications that run 7.9228162514264337593543950335E+28). With 28 decimal places,
under any of the Microsoft Windows Environment. In the name Visual the largest value is +/-7.9228162514264337593543950335, and the
Basic, 8Visual9 part refers to the method used to create the Graphical smallest nonzero value is +/-0.0000000000000000000000000001 (+/-
User Interface. The 8Basic9 part refers to the Basic programming 1E-28).
language, as its basic syntax of statements, is retained by Visual Basic. Dim d1, d2, d3, d4 As Decimal
Visual Basic is an event driven programming language. This means that 7.Double Data Type
code is executed as a response to an event. Holds signed IEEE 64-bit (8-byte) double-precision floating-point
Visual Basic is a third-generation event-driven programming language numbers that range in value from -1.79769313486231570E+308
first released by Microsoft in 1991. It evolved from the earlier DOS through -4.94065645841246544E-324 for negative values and from
version called BASIC. BASIC means Beginners' All- 4.94065645841246544E-324 through 1.79769313486231570E+308 for
purpose Symbolic Instruction Code. Since then Microsoft has released positive values. Double-precision numbers store an approximation of a
many versions of Visual Basic, from Visual Basic 1.0 to the final version real number.
Visual Basic 6.0. Visual Basic is a user-friendly programming language Dim dub As Double = 4.0R
designed for beginners, and it enables anyone to develop GUI window 8.Long data type
applications easily. Holds signed 64-bit (8-byte) integers ranging in value from -
In 2002, Microsoft released Visual Basic.NET(VB.NET) to replace Visual 9,223,372,036,854,775,808 through 9,223,372,036,854,775,807
Basic 6. Thereafter, Microsoft declared VB6 a legacy programming (9.2...E+18).
language in 2008. Fortunately, Microsoft still provides some form of Dim longValue1 As Long = 4294967296
support for VB6. VB.NET is a fully object-oriented programming 9.Object Data Type
language implemented in the .NET Framework. It was created to cater Holds addresses that refer to objects. You can assign any reference type
for the development of the web as well as mobile applications. However, (string, array, class, or interface) to an Object variable.
many developers still favor Visual Basic 6.0 over its successor Visual An Object variable can also refer to data of any value type
Basic.NET. (numeric, Boolean, Char, Date, structure, or enumeration).
In VB 6, you can create any program depending on your objective. For Dim myObject As Object
math teachers, you can create mathematical programs such 10.SByte data type
as Geometric Progression, Quadratic Equation Solver, Simultaneous Holds signed 8-bit (1-byte) integers that range in value from -128
Equation Solver ,Prime Number Tester, Factors Finder, Quadratic through 127.
Function Graph Plotter and so on. For science teachers, you can create Dim sbyteValue1 As SByte = -102
11.Short data type of the reference is passed. Because a copy is passed in mutations to
Holds signed 16-bit (2-byte) integers that range in value from -32,768 the parameter via = it won't be visible to the calling function . If you pass
through 32,767. value, it's the same as if another variable is created at the method, so
Dim shortValue1 As Short = 1034 even if you modify it, the original variable (at the call site) won't have its
12.Single Data Type value changed.
Holds signed IEEE 32-bit (4-byte) single-precision floating-point Call By Reference
numbers ranging in value from -3.4028235E+38 through -1.401298E-45 Call by reference means that a reference to the original value will be
for negative values and from 1.401298E-45 through 3.4028235E+38 for sent to the function. It's almost like the original value is being directly
positive values. Single-precision numbers store an approximation of a used within the function. Operations like = will affect the original value
real number. and be immediately visible in the calling function . If you pass in a
13.String Data Type reference, when you modify the value in the method, the variable in the
Holds sequences of unsigned 16-bit (2-byte) code points that range in call site will also be modified.
value from 0 through 65535. Each code point, or character code, The default in Visual Basic is to pass arguments by value . You can
represents a single Unicode character. A string can contain from 0 to make your code easier to read by using the ByVal keyword. It is
approximately two billion (2 ^ 31) Unicode characters. good programming practice to include either the ByVal or ByRef
Dim h As String = "Hello" keyword with every declared parameter.
14.UInteger data type
The UInteger data type provides the largest unsigned value in the most What is the difference between call by value and call by reference?
efficient data width. Call by reference
The default value of UInteger is 0. 1. When we pass arguments to a procedure by reference, you’re actually
Dim uintValue1 As UInteger = 3000000000ui passing the variable itself.
15.ULong data type 2. Any changes made to the argument by the procedure will be
Holds unsigned 64-bit (8-byte) integers ranging in value from 0 through permanent
18,446,744,073,709,551,615 (more than 1.84 times 10 ^ 19). 3. The arguments can be passed by reference using the ByRef keyword
Dim ulongValue1 As ULong = 7934076125 or by omitting any qualifier.
16.UShort data type 4. Arguments passed by reference can be modified by the called
Holds unsigned 16-bit (2-byte) integers ranging in value from 0 through procedure and the caller can read the modified value back.
65,535. 5. If the procedure changes the value, the change in the value gets
Dim ushortValue1 As UShort = 65034 reflected to the data.
Call by value
Event-driven Programming 1.When we pass arguments by value, the procedure gets the copy of
the variable, which is discarded when the procedure ends.
The event-driven programming revolves around recognizing the
2. Any changes made to the arguments by the procedure won’t affect
occurrences of events and then responding to those events by taking
the variable of the calling program.
appropriate actions. In event-driven programming an application is build
3. The arguments can be passed by value using the ByVal keyword.
up as a series of responses to user-events.
4. Changes to arguments passed by value are never propagated back
Event-driven programming is a programming paradigm in which the flow
to the caller.
of program execution is determined by events - for example a user
5. If the procedure changes the value, the change affects only the copy
action such as a mouse click, key press, or a message from the
and not the variable itself.
operating system or another program. An event-driven application is
designed to detect events as they occur, and then deal with them using
an appropriate event-handling procedure. The idea is an extension ARRAY IN VISUAL BASIC
of interrupt-driven programming of the kind found in early command-line an array is a variable with a single name that represents many different
environments such as DOS, and in embedded systems (where the items. When we work with a single item, we only need to use one
application is implemented as firmware). variable. However, if we have a list of items which are of similar type to
Event-driven programs can be written in any programming language, deal with, we need to declare an array of variables instead of using a
although some languages(Visual Basic for example) are specifically variable for each item. For example, if we need to enter one hundred
designed to facilitate event-driven programming, and provide names, it is difficult to declare 100 different names. Besides, if we want
an integrated development environment (IDE) that partially automates to process those data that involves decision making, we might have to
the production of code, and provides a comprehensive selection of built- use hundreds of if...then statements, this is a waste of time and
in objects and controls, each of which can respond to a range of events. efforts.So, instead of declaring one hundred different variables, we
Virtually all object-oriented and visual languages support event-driven need to declare only one array. We differentiate each item in the array
programming. Visual Basic, Visual C++ and Java are examples of such by using subscript, the index value of each item, for example, name(1),
languages. name(2), name(3) .......etc. , makes declaring variables more streamline.
A visual programming IDE such as VB.Net provides much of the code Arrays occupy space in memory. The programmer specifies the array
for detecting events automatically when a new application is created. type and the number of elements required by the array so that the
The programmer can therefore concentrate on issues such as interface compiler may reserve the appropriate amount of memory. Arrays may
design, which involves adding controls such as command buttons, text be declared as Public (in a code module), module or local. Module
boxes, and labels to standard forms (a form represents an application's arrays are declared in the general declarations using keyword Dim or
workspace or window). Once the user interface is substantially Private. Local arrays are declared in a procedure using Dim or Static.
complete, the programmer can add event-handling code to each control Array must be declared explicitly with keyword "As".
as required. STATIC ARRAY
Basically, you can create either static or dynamic arrays. Static arrays
“call by value” and “call by reference” function call? must include a fixed number of items, and this number must be known
Let's say we have an integer variable named x. A call to a function by 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
value using x means (a copy of) the value that x stores is passed in the
constant argument:
function call and no matter what the function does with that value, the
value stored in x remains unchanged. A call to a function by reference  Array is dimensioned during design time.
using x means a reference (also called a pointer or alias) to the variable  Array size cannot be changed.
x is passed in the function call and so any changes the function makes  By using erase function, only array element values in the
using this reference will actually change the value stored in x. memory will be emptied, elements memory will not be deleted.
When referencing something by ByVal in our method we are only taking  Memory is fixed during design time.
a 'copy' of the value, so any changes are only made to our copy. When
using the ByRef we are taking the reference of our object so any Visual Basic starts indexing the array with 0. Therefore, the preceding
changes made are made to the actual object. array holds 101 items.
Call By Value Dim intData() As Integer = {1, 2, 3, 4, 5}
Call by value means that a copy of copy of the provided value will be Dim intData(5) As Integer
sent to the function. For value types (Integer, Single, etc.) this will Dim array_name() As String = {"Peter", "John", "Brock", "James", "Mari
provide a shallow copy of the value. With larger types this can be a"}
inefficient. For reference types though (String, class instances) a copy Dim misc() as Object = {"Hello friends", 16c, 12ui, "A"c}
Dim Emp(0 to 2) As String You can populate the list box items either from the properties window or
Emp{0} = "Mathew" at runtime. To add items to a ComboBox, select the ComboBox control
Emp(1) = " Anthony" and go to the properties window for the properties of this control. Click
Emp(2) = "Prince" the ellipses (...) button next to the Items property. This opens the String
DYNAMIC ARRAY Collection Editor dialog box, where you can enter the values one at a
 Sometimes we may not know how large to make an array. Instead of line
making it large enough to hold the maximum of data (which means the Properties of the ComboBox Control
most of the array may be empty) we can declare a dynamic array. The 1.AllowSelection
size of a dynamic array can vary during the course of the program, or Gets a value indicating whether the list enables selection of list items.
we might need an array until the user has entered a bunch of data and 2.AutoCompleteCustomSource
the application has processed it and displayed the results. Gets or sets a custom System.Collections .Specialized.StringCollection
 Array is dimensioned during runtime to use when the AutoCompleteSourceproperty is set to CustomSource.
 Array size can be changed (Shrink or Enlarge) any number of 3.AutoCompleteMode
times. Gets or sets an option that controls how automatic completion works for
 By using erase function, both array element values and the ComboBox.
elements memory will be deleted. 4.AutoCompleteSource
 We can utilise the memory efficiently. Gets or sets a value specifying the source of complete strings used for
automatic completion.
The first step in declaring a dynamic array is by using the Dim statement 5.DataBindings
without specifying the dimenson list, as follows: Gets the data bindings for the control.
“ Dim myArray()” 6.DataManager
Then at run time we can specify the actual array size using the ReDim Gets the CurrencyManager associated with this control.
statement,as follows: 7.DataSource
“ ReDim myArray(1 to n) when n is decided during run time “ Gets or sets the data source for this ComboBox.
You can also declare a two dimensional array using ReDim statement, 8.DropDownHeight
as follows: Gets or sets the height in pixels of the drop-down portion of the
“ReDim myArray(1 to n, 1 to m) “ when m and n are known during ComboBox.
run time 9.DropDownStyle
With a dynamic array, we can discard the data and return the resources Gets or sets a value specifying the style of the combo box.
if occupied the system. Example:= 10.DropDownWidth
Gets or sets the width of the of the drop-down portion of a combo box.
private sub command1_click()
Dim dynastring() as string What are the differences between Combo box and List box?
ReDim dynastrings(10) List Box
Dynastring(1)=”the first string” Need more data space Redim Definition:- It contains lists of items and provides the facility to select
dynastrings(100) items from the list of items. Items can only be shown at runtime.
Dynastrings(50)=”the fiftieth string” (*) It displays a box with a list of item from which a user can make a
End sub selection.
LISTBOX AND COMBO BOX (*) It doesn’t have any edit area to enter information at runtime.
List Box (*) It has 2 style properties
The List Box represents a Windows control to display a list of items to a • 0 (zero) Standard
user. A user can select an item from the list. It allows the programmer to • 1 (One) Checkbox list box.
add items at design time by using the properties window or at the (*) It has multi select property to make multiple selections.
runtime. You can populate the list box items either from the properties (*) It has “selcount” and ‘selected” Properties. To count the no. of
window or at runtime. To add items to a ListBox, select the ListBox selected item in the list & the selection status (True if selected, False if
control and get to the properties window, for the properties of this not selected) of an item in the list respectively.
control. Click the ellipses (...) button next to the Items property. This Combo Box
opens the String Collection Editor dialog box, where you can enter the Definition:- It combines a dropdown list with the data control and the
values one at a line. dropdown list is populated at runtime depending upon the data supplies
Properties of the List Box Control to it.
1.AllowSelection (*) It provides a combination of a list box & text box control.
Gets a value indicating whether the ListBox currently enables selection (*) Combo box control has an edit area where user can enter or edit
of list items. information at runtime.
2.BorderStyle (*) It has 3-type properties
Gets or sets the type of border drawn around the list box. • 0 (Zero) dropdown combo
3.ColumnWidth . • Simple combo
Gets of sets the width of columns in a multicolumn list box. . • Dropdown list.
4.HorizontalExtent (*) It has no multi select property.
Gets or sets the horizontal scrolling area of a list box. (*) No such property Is available.
5.HorizontalScrollBar
Gets or sets the value indicating whether a horizontal scrollbar is 'VB.Net program to create the simple calculator
displayed in the list box. Module Module1
6.ItemHeight
Gets or sets the height of an item in the list box. Sub Main()
7.Items Dim choice As Integer
Gets the items of the list box. Dim num1 As Integer = 0
8.MultiColumn Dim num2 As Integer = 0
Gets or sets a value indicating whether the list box supports multiple Dim result As Integer = 0
columns.
9.ScrollAlwaysVisible Console.WriteLine("############################")
Gets or sets a value indicating whether the vertical scroll bar is shown Console.WriteLine(" 1: Addition")
at all times. Console.WriteLine(" 2: Subtraction")
10.SelectedIndex Console.WriteLine(" 3: Multiplication")
Gets or sets the zero-based index of the currently selected item in a list Console.WriteLine(" 4: Division")
box. Console.WriteLine("############################")
ComboBox
The ComboBox control is used to display a drop-down list of various Console.Write("Enter choice: ")
items. It is a combination of a text box in which the user enters an item choice = Integer.Parse(Console.ReadLine())
and a drop-down list from which the user selects an item. Console.Write("Enter number1: ")
num1 = Integer.Parse(Console.ReadLine()) now learn how to write errors handling code in Visual Basic 2019. The
Console.Write("Enter number2: ") syntax for errors handling is
num2 = Integer.Parse(Console.ReadLine()) On Error GoTo program_label
where program_label is the section of code that is designed by the
Select Case choice programmer to handle the error committed by the user. Once an error is
Case 1 detected, the program will jump to the program_label section for error
result = num1 + num2 handling.
Case 2 EXAMPLE : The Division Error
result = num1 - num2 In this example, we will deal with the error of entering non-numeric data
Case 3 into the text boxes that supposed to hold numeric values. The
result = num1 * num2 program_label here is error_handler. When the user enters non-numeric
Case 4 values into the text boxes, the error message will display the phrase
result = num1 / num2 "One or both of the entries is/are non-numeric!". If no error occurs, it will
Case Else display the correct answer. Try it out yourself.
Console.WriteLine("Invalid choice") Errors Handling using Try…..Catch….End Try Structure
End Select Visual Basic 2017 has adopted a new approach in handling errors or
rather exceptions handling. It is supposed to be more efficient than the
Console.WriteLine("Result: {0}", result) old On Error Goto method, where it can handle various types of errors
End Sub within the Try…Catch…End Try structure.
End Module The structure looks like this
Try
ERROR IN VISUAL BASIC statements
When you compile or run a Visual Basic application, the following types Catch exception_variable as Exception
of errors can occur: statements to deal with exceptions
Run-time errors End Try
If a Visual Basic application tries to perform an action that the system
can't execute, a run-time error occurs, and Visual Basic throws Looping statement
an Exception object. Visual Basic can generate custom errors of any Some statements are used in visual basic programming for repetition.
data type, including Exception objects, by using the Throw statement. These statements are called looping statement. Visual basic provide a
An application can identify the error by displaying the error number and large amount of looping statements. Function of all looping statement
message of a caught exception. If an error isn't caught, the application are about same. The computer is capable of repeating a group of
ends. statement number of times without calling the procedure for each new
The code can trap and examine run-time errors. If you enclose the code set of data. The process of repeating a series of statements called
that produces the error in a Try block, you can catch any thrown error looping. Visual basic support the following looping statements. They
within a matching Catch block. For information about how to trap errors are Do-Loop, For-Next and While-Wend.
at run time and respond to them in your code, see Do-Loop: – These looping statements execute single or block of
Compile-time errors statements for as long as a condition is true. Visual basic evaluate an
If the Visual Basic compiler encounters a problem in the code, a expression, and if it is true, the statements are executed. If the
compile-time error occurs. In the Visual Studio code editor, you can expression is false, the program continues and the statement following
easily identify which line of code caused the error because a wavy line the loop is executed.
appears under that line of code. The error message appears if you either Do While <Text Expression>
point to the wavy underline or open the Error List, which also shows Statement Block
other messages. Loop
If an identifier has a wavy underline and a short underline appears under Or,
the rightmost character, you can generate a stub for the class, Do Until <Text Expression>
constructor, method, property, field, or enum. For more information, Statement block
see Generate From Usage (Visual Studio). Loop
By resolving warnings from the Visual Basic compiler, you might be able Basic difference between While and Until that is the keyword while
to write code that runs faster and has fewer bugs. These warnings continue the loop until condition is true and keyword until continue the
identify code that may cause errors when the application is run. For loop until condition is false.
example, the compiler warns you if you try to invoke a member of an
unassigned object variable, return from a function without setting the For-Next: – The For-Next is a fixed looping statement. Single or block
return value, or execute a Try block with errors in the logic to catch of statements are executed fixed number of times. The structure of For-
exceptions. For more information about warnings, Next statement begins with a go-to statement and ends with a next
Logic Errors statement. In between are the statement to be executed. Syntax of the
A logic error occurs when the program (the code) is written fine but the for-next statement is –
result it produces is not reliable. With a logic error, the Code Editor does For <Counter> = <Start> To <End> [Step Increment / Decrement]
not see anything wrong in the document and therefore cannot point out Statement Block
a problem. One of the worse types of logic errors is one that makes a Next [Counter]
computer crash sometimes, regularly, or unpredictably, while there is The arguments counter, start, end and increment / Decrement are all
nothing obviously wrong in the code. numeric. The for-to statement specifies the number of passes through
Logic errors are, or can be, difficult to spot because you will have to the loop. Within this is statement counter is variable whose value begins
know for sure that the result is wrong and why (and sometimes worse, with start, increase, and decrease by numeric value in each time until
you will have to agree or accept that it is your program that is causing a the loop is executed, until it reached end. Note that the value of counter
problem in the computer; imagine a user reports that her computer will be end during the last pass through the loop. The next statement
crashes every time she starts the application you created). Because you identifies the end of the loop. In executing, visual basic follows this step.
or the user of your program would know with certainty that the result is
questionable, you would have to use some means of correcting it. One While-Wend: – The While-Wend statement is a conditional looping
of the techniques you can use is referred to as debugging. statement this statement executes single or block of statement while a
Error Handling condition is true. Syntax of the While-Wend statement is –
Visual Basic 2019 has improved a lot in its built-in errors handling While <Test Expression>
capabilities compared to Visual Basic 6. For example, when the user Statement Block
attempts to divide a number by zero, Visual Basic 2019 will not return Wend
an error message but gives the ‘infinity’ as the answer (although this is If condition is true, all statements between while and wend are executed.
mathematically incorrect because it should be undefined) If condition is still true, the process is repeated. If condition become
Using On Error GoTo Syntax false, the program resume with the student following the wend
Visual Basic 2019 still supports the vb6 errors handling syntax, that is statement. This While-Wend is analogous to a Do-While-Loop
the On Error GoTo program_label structure. Although it has a more statement.
advanced error handling method, we shall deal with that later. We shall

You might also like