Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

UNIT - 2

Programming with .NET Framework


Components and controls,
Data types,
variables,
Constant,
Enumerations,
Declaring Enumeration,
Type conversions,
Operators,

Control Structures:
conditional statements,
loops,

Arrays:
creating array in vb.net,
Dynamic arrays,
Multi-dimensional arrays,
Jagged Array,
The Array class,
Method of Array Class,

Functions:
defining Function,
Function returning a value,
Recursive function,
Param Arrays,
Passing Array as Function Arguments,
Defining Sub procedures,
Passing Parameters by Value and by reference.
Q1. Write the use of “For Each next” statement. MCA 2022 2no.
Q2. Define dynamic array.
Q3. How to convert Boolean to integer in .NET?
Q1. Write a .net program to check whether the number is even or odd using if-
then-else statement.
Q2. Write a .net program to print number from 30 to 1 using for loop.
Q3. Explain mouse based event with example.
Q1.

Q1. Mca2021 2no.


Q2.
Q1. What is a data type? How many types of data types are there in .net?
Q2.
Q1. What is an array? explain the uses of array in .net framework.

Q1. Mca2021 5th .


Q2.
Q1. Explain type conversion with example.
Q2. Explain different data types in .net.
Q1. What is variable? How do we define variable in vb.net? describe about
scope and life time of variable.
Q2. What is control flow statement? Explain it.

Q1. Explain the data type in .net framework. Mca 2020 5th.
Q2.
Q1. What is variables and how the variables are declared in vb.net.
Q2. Define an array, methods and name space in .net framework.
Q1.
Q1. What is dynamic array ? how it is declared? Give example. Msc 2nd 2022.
Q2. Differentiate radio button and check box.
Q1. How to define function in vb.net? explain with suitable example.
Q2.
Q1. What is exception handling? Explain try and catch block with help of
suitable example. Unit 3 ka question.
Or
Give syntax of any two loops used in .net. write a program to check whether a
given number is prime or not.

Components and controls,


 In Visual Basic .NET (VB.NET), components are reusable, modular, and self-contained
pieces of code that provide specific functionality.

 They play a crucial role in designing the user interface (UI) and implementing various
features in VB.NET applications.

controls,

 VB.NET Controls are the pillars that help in creating a GUI Based
Applications in VB.Net quickly and easily.
 These are objects that you can drag to the Form using the
Control toolbox in the IDE.
 Controls enable user interaction and can display information.

Here are some common controls used in VB.NET:

1. Button (Button Control):


 Represents a clickable button that users can interact with.
 Common events include Click, which is triggered when the button is pressed.

2. RadioButton(RadioButton Control):
 Radio button is one of the popular way to limiting the user to pick just
on option.
 These buttons are grouped together.

3. Label(Label Control):
 It is used to show any text to the user, typically the text in a label does
not change while the application is running.

4. TextBox(TextBox Control):
 It is used to accept textual input from the user.

5. CheckBox(CheckBox Control):
 heckboxes are similar to radio buttons in the way that they
are also used in groups, however, a user can select more than
one item in the group.

6. ListBox(ListBox Control):
 Display a list of items, and user can select one or more items.
 Useful for displaying list of options or items.

7. ComboBox(ComboBox Control):
 Provides a drop-down list of items for the user to select.
Data types,
 In Visual Basic .NET (VB.NET), data types are used to declare variables and define
the type of data that a variable can hold.

 VB.NET supports a variety of data types, including primitive data types, user-
defined types, and object types.
Syntax:

Here are some of the common data types in VB.NET:

Primitive Data Types:


1. Integer Data Type:
 Represents whole numbers without decimal points.
 Example: Dim num As Integer = 42
2. Double Data Type:
 Represents floating-point numbers with decimal points.
 Example: Dim value As Double = 3.14
3. Decimal Data Type:
 Represents decimal numbers with higher precision than Double.
 Example: Dim price As Decimal = 19.99D
4. Char Data Type:
 Represents a single character.
 Example: Dim letter As Char = "A"c
5. String Data Type:
 Represents a sequence of characters.
 Example: Dim name As String = "John"
6. Boolean Data Type:
 Represents True or False values.
 Example: Dim isTrue As Boolean = True
7. Date Data Type:
 Represents date and time values.
 Example: Dim currentDate As Date = Date.Now
variables,
 A variable is a simple name used to store the value of a specific data
type in computer memory.
 In VB.NET, each variable has a particular data type that determines
the size, range, and fixed space in computer memory.
 With the help of variable, we can perform several operations and
manipulate data values in any programming language.

Variable Declaration
 In VB.NET, the declaration of a variable involves giving the variable a name
and defining the data type to which it belongs.

 We use the following syntax:

Dim Variable_Name as Data_Type

In the above syntax, Variable_Name is the variable name while Data_Type is the
name to which the variable belongs.

Here is an example of a valid variable declaration in VB.NET:

Dim x As Integer

Constant,
Enumerations,
Declaring Enumeration,
Type conversions,
 In VB.NET, type conversions allow you to convert a value from one data type to
another.

 There are two types of type conversions: implicit and explicit.

1. Implicit Conversions(Widening):
 Implicit conversions are performed automatically by the compiler when there is no
risk of data loss.

2. Explicit Conversions(Widening):
 Explicit conversions require the developer to explicitly specify the conversion. These
conversions may result in data loss.

Type Conversion Functions


There are functions that we can use to convert from one data type to
another. They include:

1. CBool(expression): It is used to convert an expression into a Boolean data


type.
2. CByte(expression): It is used to convert an expression to a Byte data type.
3. CChar(expression): It is used to convert an expression to a Char data type.
4. CDate(expression): It is used to convert an expression to a Date data type.
5. CDbl(expression): It is used to convert an expression into a Double data type.
6. CDec(expression): It is used to convert an expression into a Decimal data
type.
7. CInt(expression): It is used to convert an expression to an Integer data type.
8. CLng(expression): It is used to convert an expression to a Long data type.
Operators,
 Operators are special symbols that perform operations on
variables and values.
 Vb.net supports a wide range of operators, classified into
different categories based on their functionality.
For example: In the expression,

The symbol + and - are the Operators, and the 3, 2, and 1 are operands.

Different Types of VB.NET Operators

Following are the different types of Operators available in VB.NET:

o Arithmetic Operators
o Assignment Operators
o Comparison Operators
o Logical and Bitwise Operators
o Bit Shift Operators
o Concatenation Operators
o Miscellaneous Operators

1. Arithmetic Operators:
 Arithmetic Operators are used to perform mathematical
operations such as addition, subtraction, multiplication, division
etc.

Arithmetic Operators in VB.NET

Operators Operation Example

^ Raise one operant to the power of Y ^ X (X to the power


another Y)

+ Addition X+Y
- subtractions X-Y

* multiplication X*Y

/ division X/Y

\ Integer division X\Y

Mod X Mod Y

2. Assignment Operators:
 Assignment operators are used to assign the value to variables.
Some assignment operators are:
Operator Name Example

= Assignment Operator X = 5, X assign a value 5


X = P + Q, (P + Q) variables or value
assign to X.

+= Addition Assignment X+=5,whichmeans


X= X+5

-= Subtraction X -= P, which is same as X = X - P


Assignment

*= Multiplication X *= P, which is same as X = X - P


Assignment

/= Division Assignment X /= P, which is same as X = X - P

3. Comparison Operators

 Comparison Operators are used to compare two values.


Operator Meaning Example

= Is equal to (A = B)

> Greater than (A > B); if yes, TRUE,

Else FALSE
< Less than (A < B); if the condition is true, returns
TRUE else FALSE

>= Greater than or A >= B


equal to

<= Less than or qual to A <= B

4. Logical and Bitwise Operators

 Logical Operators are used to check whether an expression is


True or False.
Operator Description Example

And True if both the operands are true (A And B), result
= False

Or True of at least one of the operands is true (A Or B), result =


True

Not True if the operands is false. Not A

Or

Not(A And B) is
True

Xor A Xor B is True

AndAlso A AndAlso B =
False

OrElse A OrElse B = True

IsFalse The IsFalse Operator is used to determine


whether an expression is False.

IsTrue The IsTrue Operator is used to determine


whether an expression is True.
5. Bit Shift Operators:
 The Bit Shit Operators are used to perform the bit shift operations on binary
values either to the right or to the left.

Some Bit Shift Operators:

Operator Description

<< The Binary Left Shift Operator is used to shift the bit to the left
side.

>> The Binary Right Shift Operator is used to shift the bit to the right
side.

6. Concatenation Operators:

In VB.NET, there are two concatenation Operators to bind the operands:

Operator Description Example

& It is an ampersand symbol that is used to bind two Result = Wel &
or more operand together. come,
Result =
Welcome

+ It is also used to add or concatenate two number or Result = Wel +


string. come,
Result =
Welcome

7. Miscellaneous Operators:

Miscellaneous Operators Description


GetType This operator gives the Type of objects for a specified expression.
 for declaring the code and parameters of a lambda
Function Expression  expression function.
Control Structures:
conditional statements,
 In VB.NET, the control statements are the statements that controls the execution
of the program on the basis of the specified condition.

 It is useful for determining whether a condition is true or not.

 If the condition is true, a single or block of statement is executed.

VB.NET provides the following conditional or decision-making statements.

o If-Then Statement
o If-Then Else Statement
o If-Then ElseIf Statement
o Select Case Statement
o Nested Select Case Statements

If-Then Statement
 Executes a block of code if a specified condition is true.

Syntax:

If-Then-Else Statement
 Executes one block of code if a specified condition is true and
another block if the condition is false.
If-Then-ElseIf statement
 Checks multiple conditions sequentially and executes the block of
code corresponding to the first true condition.

Select Case Statement


 Provides a concise way to handle multiple conditions based on the
value of an expression.

Nested Select Case statements


 When a Select Case statement is written inside the body of
another Select Case statement is called a nested Select Case
statement.
loops,
 A Loop is used to repeat the same process multiple times until it meets the specified
condition in a program.

 By using a loop in a program, a programmer can repeat any number of statements up


to the desired number of repetitions.

 A loop is also used to reduce the program complexity, easy to understand, and easy
to debug.

Types of Loops
There are five types of loops available in VB.NET:

o Do While Loop
o For Next Loop
o For Each Loop
o While End Loop
o With End Loop

Do While Loop
 In VB.NET, Do While loop is used to execute blocks of statements in the program, as
long as the condition remains true.

Syntax:
For Next Loop
 A For Next loop is used to repeatedly execute a sequence of code
or a block of code until a given condition is satisfied.
Syntax:

For Each Loop


 In the VB.NET, For Each loop is used to iterate block of statements
in an array or collection objects.
Syntax:

While End Loop


 The While End loop is used to execute blocks of code or
statements in a program, as long as the given condition is true.

Syntax:

With End With Statement


 Repeats a block of code with a specified condition until the condition
becomes false.
Arrays:
 An array is a linear data structure that store a fix size sequence of
elements of the same type.
 Elements in the array are stored in contiguous memory locations.
 Array elements can be accessed using their index which start from
0 and goes up to size-1.
 Array can store element of any data type.
creating array in vb.net,
Declaration of VB.NET Array
We can declare an array by specifying the data of the elements followed by
parentheses () in the VB.NET.

In the above declaration, array_name is the name of an array, and


the Data_Type represents the type of element (Integer, char, String,
Decimal)
Dynamic arrays,
 A Dynamic array is used when we do not know how many items or elements to be
inserted in an array.

 To resolve this problem, we use the dynamic array.

 It allows us to insert or store the number of elements at runtime in sequentially


manner.

 A Dynamic Array can be resized according to the program's requirements at run time
using the "ReDim" statement.

Initial Declaration of Array


Syntax:

Runtime Declaration of the VB.NET Dynamic array


(Resizing)
Syntax:
Multi-dimensional arrays,
 Multi-dimensional arrays is a data structure that can store
elements in two or more dimensions.
 The most common multi-dimensional array is two-dimensional
array, organized in row and columns.
 multi-dimensional array are used to represent tabular or grid like
data structure.
Creating a 2D Array:
Syntax for Creating a 2D Array:

Example: Creating a 2D Array of Integers:

Jagged Array,
 A jagged array is an array of arrays, where each element of the
main array is an array itself.
 Unlike a multi-dimensional array, jagged arrays can have different
lengths for each row.
Creating a Jagged array:
Syntax:

Example:
The Array class,
Method of Array Class,
Functions:
 In VB.NET, functions are block of code that perform a specific task
and return a value to calling code.
 Functions are defined using the ‘Function’ keyword.
 Functions are also useful to code reusability by reducing the duplicity
of the code.
defining Function,
The syntax to define a function is:

Example:

Function returning a value,


A function can return a value using the Return statement.
Recursive function,
 A recursive function is a function that calls itself in order to solve a
problem.
 Recursive functions are often used when a problem can be broken
down into smaller instances of the same problem.
 A recursive function is useful for solving many mathematical tasks such as
generating the Fibonacci series, the factorial of a number, etc.

Example: Recursive Factorial Function:

Param Arrays,
Passing Array as Function Arguments,
Defining Sub procedures,
Passing Parameters by Value and by reference.

You might also like