Sunday, November 14, 2010

Migrating from PHP to ASP.NET- part4

Output
The typical way of outputting data in PHP is through the echo() language construct.
closest analogue to this in ASP.NET is the Response.Write() method, or the
'

Code Sample 3. Basic output in PHP




$hello = "hi how are you\n";

echo $hello;

?>

Code Sample 3. Basic output in Visual Basic .NET

However, these methods for sending output to the browser exist primarily for backwards compatibility with classic ASP. ASP.NET's new control-based, event-oriented model allows for data to be output to the browser by simply setting properties on server controls. This technique allows for clean separation of layout and code and can make maintenance easier, requiring significantly less code in complex situations than PHP.



The current date is:

This example declares a server-side Label control called TheDate, and during the page's Load event, sets the Text property of the label to the current date and time. The HTML output of this code is identical to the other two versions, except that the Label control renders itself as a span tag containing whatever was set as the label's text.

Conditional Processing

IF/ELSE

PHP has several conditional processing expressions such as for, while, switch, and foreach but the most common is the if/else expression. Visual Basic .NET has very similar constructs with similar syntax. Code Sample 4 provides a comparison of equivalent conditional logic in PHP and Visual Basic .NET.

Code Sample 4. Basic conditional logic in PHP

if ($a > $b) {

print "a is bigger than b";

} elseif ($a == $b) {

print "a is equal to b";

} else {

print "a is smaller than b";

}



Code Sample 4. Basic conditional logic in Visual Basic .NET

If a > b

Response.Write ("a is bigger than b")

ElseIf a = b Then

Response.Write ("a is equal to b")

Else

Response.Write ("a is smaller than b")

End If



Switch

Switch statements are common language constructs for most programming languages when you wish to test a single expression for multiple values. They are commonly used to replace if statements that contain multiple elseif/else blocks.

Code Sample 5 shows a comparison between PHP's switch statement and Visual Basic's Select Case statement.

Code Sample 5. A switch statement in PHP

switch ($i) {

case 0:

print "i equals 0";

break;

case 1:

print "i equals 1";

break;

case 2:

print "i equals 2";

break;

default:

print "i is not equal to 0, 1 or 2";

}



Code Sample 5. A Select Case statement in Visual Basic .NET

Select Case Number i

Case 0

description = "0"

Wesponse.Write ("i equals 0")

Case 1

description = "1"

Response.Write ("i equals 1")

Case 2

description = "2"

Response.Write ("i equals 2")

Case Else

description = " i is not equal to 0, 1 or 2"

Response.Write ("i is not equal to 0, 1 or 2 ")

End Select

No comments:

Post a Comment