Thursday, November 6, 2014

Variables,Data Types,Operators and Converting Data Types



Variables are named entities used to store data.

Variables
Variables are used to store data.
The name of a variable must begin with an alphabetic character and cannot contain whitespace or reserved characters.

A variable can be of a specific type, indicating the kind of data it stores. String variables store string values ("Welcome to W3Schools"), integer variables store number values (103), date variables store date values, etc.

Variables are declared using the var keyword, or by using the type (if you want to declare the type), but ASP.NET can usually determine data types automatically.
Examples
// Using the var keyword:
var greeting = "Welcome to W3Schools";
var counter = 103;
var today = DateTime.Today;

// Using data types:
string greeting = "Welcome to W3Schools";
int counter = 103;
DateTime today = DateTime.Today;


Data Types
Below is a list of  common data types:
Type
Description
Examples
int
Integer (whole numbers)
103, 12, 5168
float
Floating-point number
3.14, 3.4e38
decimal
Decimal number (higher precision)
1037.196543
bool
Boolean
true, false
string
String
"Hello W3Schools", "John"


Operators
An operator tells ASP.NET what kind of command to perform in an expression.
 The C# language supports many operators. Below is a list of common operators:
Operator
Description
Example
=
Assigns a value to a variable.
i=6
+
-
*
/
Adds a value or variable.
Subtracts a value or variable.
Multiplies a value or variable.
Divides a value or variable.
i=5+5
i=5-5
i=5*5
i=5/5
+=
-=
Increments a variable.
Decrements a variable.
i += 1
i -= 1
==
Equality. Returns true if values are equal.
if (i==10)
!=
Inequality. Returns true if values are not equal.
if (i!=10)
<
>
<=
>=
Less than.
Greater than.
Less than or equal.
Greater than or equal.
if (i<10)
if (i>10)
if (i<=10)
if (i>=10)
+
Adding strings (concatenation).
"w3" + "schools"
.
Dot. Separate objects and methods.
DateTime.Hour
()
Parenthesis. Groups values.
(i+5)
()
Parenthesis. Passes parameters.
x=Add(i,5)
[]
Brackets. Accesses values in arrays or collections.
name[3]
!
Not. Reverses true or false.
if (!ready)
&&
||
Logical AND.
Logical OR.
if (ready && clear)
if (ready || clear)


Converting Data Types
Converting from one data type to another is sometimes useful.

The most common example is to convert string input to another type, such as an integer or a date.
As a rule, user input comes as strings, even if the user entered a number. Therefore, numeric input values must be converted to numbers before they can be used in calculations.
Below is a list of common conversion methods:
Method
Description
Example
AsInt()
IsInt()
Converts a string to an integer.
if (myString.IsInt())
  {myInt=myString.AsInt();}
AsFloat()
IsFloat()
Converts a string to a floating-point number.
if (myString.IsFloat())
  {myFloat=myString.AsFloat();}
AsDecimal()
IsDecimal()
Converts a string to a decimal number.
if (myString.IsDecimal())
  {myDec=myString.AsDecimal();}
AsDateTime()
IsDateTime()
Converts a string to an ASP.NET DateTime type.
myString="10/10/2012";
myDate=myString.AsDateTime();
AsBool()
IsBool()
Converts a string to a Boolean.
myString="True";
myBool=myString.AsBool();
ToString()
Converts any data type to a string.
myInt=1234;
myString=myInt.ToString();


Reading User Input



Reading User Input
Another important feature of dynamic web pages is that you can read user input.
Input is read by the Request[] function, and posting (input) is tested by the IsPost condition:
Example
@{
var totalMessage = "";
if(IsPost)
    {
    var num1 = Request["text1"];
    var num2 = Request["text2"];
    var total = num1.AsInt() + num2.AsInt();
    totalMessage = "Total = " + total;
}
}
<!DOCTYPE html>
<html>
<body style="background-color: beige; font-family: Verdana, Arial;">
<form action="" method="post">
<p><label for="text1">First Number:</label><br>
<input type="text" name="text1"></p>
<p><label for="text2">Second Number:</label><br>
<input type="text" name="text2"></p>
<p><input type="submit" value=" Add "></p>
</form>
<p>@totalMessage</p>
</body>

If and Else Conditions



If and Else Conditions
An important feature of dynamic web pages is that you can determine what to do based on conditions.
The common way to do this is with the if ... else statements:
Example
@{
var txt = "";
if(DateTime.Now.Hour > 12)
  {txt = "Good Evening";}
else
  {txt = "Good Morning";}
}
<html>
<body>
<p>The message is @txt</p>
</body>
</html>

Working With Objects



Working With Objects
Server coding often involves objects.

The "Date" object is a typical built-in ASP.NET object, but objects can also be self-defined, a web page, a text box, a file, a database record, etc.

Objects may have methods they can perform. A database record might have a "Save" method, an image object might have a "Rotate" method, an email object might have a "Send" method, and so on.

Objects also have properties that describe their characteristics. A database record might have a FirstName and a LastName property (amongst others).
The ASP.NET Date object has a Now property (written as Date.Now), and the Now property has a Day property (written as Date.Now.Day). The example below shows how to access some properties of the Date object:
Example
<table border="1">
<tr>
<th width="100px">Name</th>
<td width="100px">Value</td>
</tr>
<tr>
<td>Day</td><td>@DateTime.Now.Day</td>
</tr>
<tr>
<td>Hour</td><td>@DateTime.Now.Hour</td>
</tr>
<tr>
<td>Minute</td><td>@DateTime.Now.Minute</td>
</tr>
<tr>
<td>Second</td><td>@DateTime.Now.Second</td>
</tr>
</td>
</table>