Thursday, November 11, 2010

Migrating from PHP to ASP.NET- part3

Comparing Syntax and Common Tasks

The following sections provide comparisons between PHP and .NET syntax as well as how to accomplish some of the more common programming tasks.

Comments

PHP allows you to insert comments in your code using C, C++ and Unix shell-style syntax, and anything within those comment indicators will not be executed.

In general, to comment out Visual Basic .NET code in ASP.NET you just need to use to close the block.

Code Sample 1 shows comments in each environment.

Code Sample 1. Server-side comments in PHP

/*

This is a block of text

That has been commented out

*/



Code Sample 1. Server-side comments in ASP.NET

Variables

While PHP and Visual Basic .NET have similar language constructs, they are very different syntax for them. Since Visual Basic .NET is built upon an OOP model, variable declaration is much more rigorous than in PHP where a variable is declared simply by adding a dollar sign ($) before the variable name.

In Visual Basic .NET you declare a variable by specifying its name and characteristics. The declaration statement for variables is the Dim keyword. Its location and contents determine the variable's characteristics. Variables have levels such as local and module, data types, lifetimes and finally accessibility.

While this approach may seem more complex at first than variable assignment in PHP it actually makes a developer's life easier. ASP.NET focuses on helping developers build robust applications—and specifying data types makes tasks such as variable clean up, debugging, exception and error handling, and code maintenance much easier.

Code Sample 2 shows examples of declaring variables in each environment.

Code Sample 2. Variable declaration in PHP

$head_count

$foo

$X

$obj



Code Sample 2. Variable declaration in Visual Basic .NET

Dim head_count As Integer

Dim foo As String

Dim X As Date

Dim Obj As object



Declaring Data Types


The AS clause in the declaration statement allows you to define the data type or object type of the variable you are declaring. You can specify any of the following types for a variable:

• An elementary data type, such as Boolean, Long, or Decimal

• A composite data type, such as an array or structure

• An object type, or class, from Visual Basic or another application, such as Label or TextBox

You can declare several variables of the same type in one statement without having to repeat the data type. In the following statements, the variables numStudents, numGTA and numProfessors are declared as type Integer:

Dim numStudents, numGTA , numProfessors As Integer

' All three are Integer variables.



For more information on data types, see Data Types. For more information on object-oriented programming, see Object-Oriented Programming in Visual Basic.

Declaring Lifetime

The lifetime of a variable is the period of time during which it is available for use. A local variable declared with a Dim statement exists only as long as its procedure is executing. When the procedure terminates, all its local variables disappear and their values are lost.

The concept of lifetime is extremely useful in that it allows developers to build applications with out having to concern them selves with many issues that occur in large-scale applications such as efficient memory management. By selecting the correct lifetime for a variable you can allow .NET to perform clean up operations on variables that are not being used.

For more information on lifetime, see Lifetime.

Declaring Scope

A local variable is one that is declared within a procedure (a procedure is analogous to a function). A non-local variable is one that is declared outside a procedure, but within a class or structure.

In a class or structure, the category of a non-local variable depends on whether or not it is shared. If it is declared with the Shared keyword, it is a shared variable, and it exists in a single copy shared among all instances of the class or structure. Otherwise it is an instance variable, and a separate copy of it is created for each instance of the class or structure. A given copy of an instance variable is available only to the instance for which it was created.

The scope of a variable is the set of all code that can refer to it without qualifying its name. A variable's scope is determined by where the variable is declared. Code located in a given region can use the variables defined in that region without having to qualify their names. When declaring scope, the following rules apply:

• The scope of a shared or instance variable is the structure or class in which it is declared.

• The scope of a local variable is the procedure in which it is declared.

However, if you declare a local variable within a block, its scope is that block only. A local variable is active within the defining control block. The control block can be a procedure, an if statement, a loop statement and so on.
For more information on scope, see Scope.
Declaring Accessibility

.NET supports the idea of accessibility to variables, which allows you, the developer to control what code can access specific variables. For example if you wanted to set some constants for a formula and make sure that your constant never gets changed by other code outside of its class you could declare that variable private like this:

Private myConstant As Integer
Private int myConstant ;

A variable's accessibility is determined by which keyword or keywords—Dim, Public, Protected, Friend, Protected Friend, or Private—you use in the declaration statement. In general you will only use public and private in your development.

You can declare a module, structure, class, or instance variable with any of these keywords. Within a procedure, only the Dim keyword is allowed, and the accessibility is always private.

Code Sample 2. Variable declaration in c#.net
int head_count ;

string foo ;

Date X ;

object Obj ;

No comments:

Post a Comment