ASP.NET

What is ASP.NET?
ASP.NET is a framework generally written in one of two languages - VB script or C#.
Environmental Setup:
Visual Studio is IDE used to easily develop DOT NET applications.Follow the below steps to download and install Visual Studio.






Run "Hello World" program  in VB.NET using Visual Studio:
Follow the below steps to create a simple Console application which prints Hello World.




Variables in VB:
Variables are the names given to the actual data to be stored in memory.
Rules:
  • It must begin with an alphabetic character or an underscore.
  • It must only contain alphabetic characters, decimal digits, and underscores.
  • No spacing is allowed.
  • It must not begin with a number.
  • Dot(.) is not allowed.
Declaration Levels:
The scope of a variables depends on the Declaration level. The below are the declaration levels.
Local and Member Variables:
  • A local variable is one that is declared within a procedure and is available only within procedure.
  • A member variable is declared at the class level and is available globally to the class members.
Shared and Instance Variables:
  • Shared variable is declared at class level with Shared keyword. It exists in a single copy shared among all instances of the class.
  • If it is not shared it is an instance variable, and a separate copy of it is created for each instance of the class.
Variable Declaration Scope:
The keyword used to declare a variable and the location in which its declared defines the scope and duration in which a variable is available.
Keyword:
  • The first word used in a variable declaration
Declaration location:
  • "Procedure" means the variable is declared within a Sub, Function.
  • "Module" means the variable is declared near the top of the module, not inside a procedure
Scope:
  • "Procedure" means the variable is available only within the Sub, Function in which it was defined.
  • "Module" means the variable is available only within the module in which it was defined
  • "Project" means the variable is available throughout all modules in the project
Duration:
  • "Procedure" means the variable's value is gone once the procedure its available in has finished
  • "Application" means the variable's value will remain throughout running of the program
Keyword Declaration location Scope Duration
Dim Procedure Procedure Procedure
Static Procedure Procedure Application
Private Module Module Application
Public Module Project Application
Const Procedure Procedure Procedure
Private Const Module Module Application
Public Const Module Project Application
Global Module Project Application
Data Types:
Data types describes what kind of values and storage space a variable holds.
Data Type Size
Byte 1 byte
Short 2 bytes
Integer 4 bytes
Long 8 bytes
Char 2 bytes
String Calculated based on length
Float 4 bytes
Double 8 bytes
Decimal 16 bytes
Date 8 bytes
Boolean 1 byte
Variable Declaration:
  • Syntax: Dim variableName As DataType
  • Example: Dim number As Integer
Variable Initialization:
  • Syntax: Dim variableName As DataType=value
  • Example: Dim number As Integer=10
String Expressions:
  • Declaration Syntax: Dim stringName As String
  • Example: Dim name as String
  • Initialization Syntax: Dim stringName As String = "Enter the string here"
  • Example: Dim name As String = "Keshav Jha"
The following are some basic string manipulation functions.
1. The  Len Function
The length function is used to find out the number of characters in any given string.
Syntax:
Len(string)
2. The Mid function
The mid function is used to Return a substring containing a specified number of characters from a string.
Syntax:
Mid (string, start[, length])
string - String expression from which characters are returned.
start - Character position in string at which the part to be taken begins.
length - Length is Optional. Number of characters to return.
3. The Left Function
The Left function extract the left portion of a string.
Syntax:
Left("string", n)
4. The Right Function
The Right function extract the right portion of a string.
Syntax:
Right("string", n)
5. The Space Function
The space function is used to Return a string containing the specified number of blank spaces.
Syntax:
Space (number)
6. The Replace Function
The replace function is used to replacing some text in a string with some other text.
Syntax:
Replace( string, searchtext, replacetext )
7. The Trim Function
The trim function trims the empty spaces on both side of the String.
Syntax:
Trim ("String")
8. The Ltrim Function
The Ltrim function trims the empty spaces of the left portion of the string.
Syntax:
Ltrim("string")
9. The Rtrim Function
The Rtrim function trims the empty spaces of the Right portion of the string.
Syntax:
Rtrim("string")
10. The Ucase and the Lcase Functions
The Ucase function converts all the characters of a string to capital letters. On the other hand, the Lcase function converts all the characters of a string to small letters.
Syntax:
Ucase("string") or Lcase("string")
Program showing all the above functions:
Module Module1
    Sub Main()
        Dim leng As String = Len(" Keshav Jha")
        Console.WriteLine("length is :" & leng)
        Dim middle As String = Mid("Keshav Jha", 3, 4)
        Console.WriteLine("Mid is :" & middle)
        Dim leftf As String = Left("Keshav Jha", 3)
        Console.WriteLine("Left is:" & leftf)
        Dim rightr As String = Right("Keshav Jha", 6)
        Dim replaces As String = Replace("Keshav Jha", " ", "Kumar")
        Console.WriteLine("Replace is :" & replaces)
        Dim trimt As String = Trim("   Keshav Jha  ")
        Console.WriteLine("Trim is :" & trimt)
        Dim ltriml As String = LTrim("    Keshav Jha     ")
        Console.WriteLine("ltrim is :" & ltriml)
        Dim rtrimr As String = RTrim("    Keshav Jha     ")
        Console.WriteLine("rtrim is :" & rtrimr)
        Dim ucaseu As String = UCase("Keshav Jha")
        Console.WriteLine("Ucase is :" & ucaseu)
        Dim lcasel As String = LCase("Keshav Jha")
        Console.WriteLine("Ucase is :" & lcasel)
    End Sub
End Module

Conditional Statements in VB:
  • Conditional Statements are used to control program flow and to make decisions.
  • There are four types of If control structures:
  • If….Then statement
  • If….Then… Else statement 
  • If….Then….ElseIf ....Else statement.
  • The Select Case…End Select Structure
1.  If….Then Statement:
If the condition is true, the Expressions within the End If will be executed.
Syntax:
If condition Then

Expressions

End If

2. If….Then… Else statement:
The Expressions under the If block are executed based on the condition is true, Otherwise Else block is executed.
Syntax:
If condition Then

Expressions that need to be executed on satisfying the above condition.

Else

Expressions that need to be executed on failing the if condition.

End If

3. If….Then….ElseIf ....Else statement:
In circumstances where there are more than two alternative conditions, we can use the If….Then…ElseIf Statement.
Syntax:
If condition Then

Expressions that need to be executed on satisfying the above condition.

ElseIf condition Then

Expressions that need to be executed on satisfying the above condition.

ElseIf condition Then

Expressions that need to be executed on satisfying the above condition. 

..

Else

Expressions that need to be executed if none of the above conditions are true.

End If

4. The Select Case…End Select Statement:
It is the replace for the above If...Then...ElseIf...Else Statement to overcome the problems in this.
Syntax:
Select Case expression

Case value1

Block of one or more statements

Case value2

Block of one or more statements

Case value3
.
.
Case Else

Block of one or more statements

End Select

The particular block of statements are executed based on matching the expression and values.

Loops:
  • Looping involves a  procedure that runs repetitively until a certain condition is met.
  • There are three types of Loops in .net, they are For…..Next loop, the Do loop, and the While…..End While loop
1. For….Next Loop:
Syntax:
For counter=startNumber to endNumber (Step increment)

One or more statements

Next

In order to exit a For…..Next Loop,we can place the Exit For statement within the loop.

2. Do Loop:
There are several  Do Loop structures, as shown below:
a) Do While condition
Block of one or more statements
Loop

b) Do
Block of one or more statements
Loop While condition

c) Do Until condition
Block of one or more statements
Loop

d) Do
Block of one or more statements
Loop Until condition

We can also use Exit Do to escape the loop.

3.While....End While Loop:
Syntax:
While conditions

Block of one or more statements

End While

Read from User Input and Write to User Output:
Example:


Latest
Previous
Next Post »