Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Wednesday, 29 January 2014

Procedures And Functions



Procedures And Functions

There are two kinds of procedures in VBScript: A sub procedure and a function. The difference lies on their behaviors but their coding (programming) depends of your goal

A procedure can be included in the body of an HTML but to separate the script behavior from the rest of the file, it is usually a good idea to include the procedures in the head section of the file.

The advantage of including a script in the head section is that it is more likely to be interpreted before the section it refers to is reached. If you have done any type of programming before, you may know that interpreters (and compilers) read a program in a top-down approach. Therefore, if the browser (actually the VBScript interpreter) finds a thing in the body section but doesn't know what that thing is because it is in the bottom part of the body section, it may not interpret your script accurately. But if the script is in the head section, the interpreter will have "seen" it before reaching the body section

Sub Procedures

A sub procedure is a section of code that carries an assignment but doesn't give back a result. To create a sub procedure, start the section of code with the Sub keyword followed by a name for the sub procedure. To differentiate the name of the sub procedure with any other regular name, it must be followed by an opening and closing parentheses. The section of the sub procedure code closes with End Sub as follows:

Sub ShowMeTheDough()

End Sub

We can declare variables in the procedure if you need to. These variables are declared and dealt with way, in the same way we learned in the regular script sections. Using declared variables, the above procedure can be written as follows:

Sub DisplayFullName()
Dim FirstName, LastName
Dim FullName

FullName = FirstName & " " & LastName
End Sub

Calling a Procedure

After creating a procedure, you can call it from another procedure, function, or control's event in the body section of an HTML file. To call a simple procedure such as the earlier DisplayFullName, you can just write the name of the sub procedure

In the following example, the above DisplayFullName sub procedure is called when the user clicks the Detail section of the form:

Sub Detailer()
DisplayFullName
End Sub

If you want the procedure to be accessed immediately as soon as the page displays, you can assign its name to the onLoad() event of the body tag

Arguments Passing

To carry an assignment, sometimes a procedure needs one or more values to work on. If a procedure needs a variable, such a variable is called an argument. Another procedure might need more that one argument, thus many arguments. The number and types of arguments of a procedure depends on various factors.

If you are writing your own procedure, then you will decide how many arguments your procedure would need. You also decide on the type of the argument(s). For a procedure that is taking one argument, in the parentheses of the procedure, write a name for the argument. Here is an example:

Sub CalculateArea(Radius)
Dim dblPI
Dim dblArea

dblPI = 3.14159
dblArea = Radius * Radius * dblPI
End Sub

A procedure can take more than one argument. If you are creating such a procedure, between the parentheses of the procedure, write the name of the first argument followed by a comma; add the second argument and subsequent arguments and close the parentheses. There is no relationship between the arguments; for example, they can be of the same type:

Sub CalculatePerimeter(Length, Height)
Dim dblPerimeter

dblPerimeter = 2 * (Length + Height)
End Sub
The arguments of your procedure can also be as varied as you need them to be. Here is an example:
Sub DisplayGreetings(strFullName, intAge)
Dim Sentence
Sentence 

Calling an Argumentative Procedure

We saw already how to call a procedure that doesn't take any argument. Actually, there are various ways you can call a sub procedure. As we saw already, if a sub procedure doesn't take an argument, to call it, you can just write its name. If a sub procedure is taking an argument, to call it, type the name of the sub procedure followed by the name of the argument. If the sub procedure is taking more than one argument, to call it, type the name of the procedure followed by the name of the argument, in the exact order they are passed to the sub procedure, separated by a comma. Here is an example:

Sub Result()
Dim dblHours, dblSalary

CalcAndShowSalary dblHours, dblSalary
End Sub
Sub CalcAndShowSalary(Hours, Salary)
Dim dblResult

dblResult = Hours * Salary
txtResult = dblResult
End Sub

Alternatively, you can use the keyword Call to call a sub procedure. In this case, when calling a procedure using Call, you must include the argument(s) between the parentheses. using Call, the above procedure could call the CalcAndShowSalary as follows:

Sub Result()
Dim dblHours As Double
Dim dblSalary As Double

dblHours = txtHours
dblSalary = txtSalary

Call CalcAndShowSalary(dblHours, dblSalary)
End Sub

Functions

Creating a Function

A function is an assignment that a piece of code can take care for the functionality of a database. The main difference between a sub procedure and a function procedure is that a function can return a value

A function is created like a sub procedure with a few more rules. The creation of function starts with the Function keyword and closes with End Function. Here is an example:

Function FindFullName()

End Function

The name of the function follows the same rules and suggestions we have reviewed for the sub procedures.

To implement a function, remember that it is supposed to return a value. In the body of the function, describe what it is supposed to do. to return the right value, assign the desired value to the name of the function. Here is an example:

Function CalculateArea(Radius)
CalculateArea = Radius * Radius * 3.14159
End Function

A function can also be as complex as performing many and various expressions in order to get a value that can be assigned to the name of the function.

Calling a Function

To call a function, you have two main alternatives. If you want to use the return value of a function in an event or another function, assign the name of the function to the appropriate local variable. Make sure you include the argument(s) of the function between parentheses.

Friday, 24 January 2014

The Null Coalescing Operator (??) in C#

C# provides a special data types, the nullable types, to which you can assign normal range of values as well as null values.
For example, you can store any value from -2,147,483,648 to 2,147,483,647 or null in a Nullable< Int32 > variable. Similarly, you can assign true, false or null in a Nullable< bool > variable. Syntax for declaring a nullable type is as follows:
< data_type> ? <variable_name> = null;
The following example demonstrates use of nullable data types:
using System;
namespace CalculatorApplication
{
   class NullablesAtShow
   {
      static void Main(string[] args)
      {
         int? num1 = null;
         int? num2 = 45;
         double? num3 = new double?();
         double? num4 = 3.14157;
         
         bool? boolval = new bool?();

         // display the values
         
         Console.WriteLine("Nullables at Show: {0}, {1}, {2}, {3}", 
                            num1, num2, num3, num4);
         Console.WriteLine("A Nullable boolean value: {0}", boolval);
         Console.ReadLine();

      }
   }
}
When the above code is compiled and executed, it produces the following result:
Nullables at Show: , 45,  , 3.14157
A Nullable boolean value:

The Null Coalescing Operator (??)

The null coalescing operator is used with the nullable value types and reference types. It is used for converting an operand to the type of another nullable (or not) value type operand, where an implicit conversion is possible.
If the value of the first operand is null, then the operator returns the value of the second operand, otherwise it returns the value of the first operand. The following example explains this:
using System;
namespace CalculatorApplication
{
   class NullablesAtShow
   {
         
      static void Main(string[] args)
      {
         
         double? num1 = null;
         double? num2 = 3.14157;
         double num3;
         num3 = num1 ?? 5.34;      
         Console.WriteLine(" Value of num3: {0}", num3);
         num3 = num2 ?? 5.34;
         Console.WriteLine(" Value of num3: {0}", num3);
         Console.ReadLine();

      }
   }
}
When the above code is compiled and executed, it produces the following result:
Value of num3: 5.34
Value of num3: 3.14157

Thursday, 23 January 2014

ASP Select Case Example

If Statements  is not the most efficient method for checking multiple conditions. ASP uses the Select Case statement to check for multiple Is Equal To "=" conditions of a single variable.

If you are an experienced programmer you realize the Select statement resembles a Switch statement that other programming languages use for an efficient way to check a large number of conditions at one time.

ASP Select Case Example

The variable that appears immediately after Select Case is what will be checked against the list of case statements. These case statements are contained within the Select Case block of code. Below is an ASP Select Case example that only checks for integer values. Later we will show how to check for strings.

ASP Code:

<%
Dim myNum
myNum = 5
Select Case myNum
 Case 2
  Response.Write("myNum is Two")
 Case 3
  Response.Write("myNum is Three")
 Case 5
  Response.Write("myNum is Five")
 Case Else
  Response.Write("myNum is " & myNum) 
End Select
%>

Display:

myNum is Five

ASP Select Case - Case Else

In the last example you might have noticed something strange, there was a case that was referred to as "Case Else". This case is actually a catch all option for every case that does not fit into the defined cases. In english it might be thought of as: If all these cases don't match then I'll use the "Case Else"!
It is a good programming practice to always include the catch all Else case. Below we have an example that always executes the Else case.

ASP Code:

<%
Dim myNum
myNum = 454
Select Case myNum
 Case 2
  Response.Write("myNum is Two")
 Case 3
  Response.Write("myNum is Three")
 Case 5
  Response.Write("myNum is Five")
 Case Else
  Response.Write("myNum is " & myNum) 
End Select
%>

Display:

myNum is 454

Select Case with String Variables

So far we have only used integers in our Select Case statements, but you can also use a string as the variable to be used in the statement. Below we Select against a string.

ASP Code:

<%
Dim myPet
myPet = "cat"
Select Case myPet
 Case "dog"
  Response.Write("I own a dog")
 Case "cat"
  Response.Write("I do not own a cat")
 Case Else
  Response.Write("I once had a cute goldfish")
End Select
%>

Display:

I do not own a cat

Declaring a Variable in ASP



ASP is not a language in itself. To program ASP you actually need to know the VBScript scripting language. This means that all VBScript variable rules can be applied to your ASP code.
Declaring a Variable in ASP
It is a good programming practice to declare all your variables before you use them, even though it is not required. Nearly all programming languages require you to declare variables and doing so also increases your program's readability.
In ASP you declare a variable with the use of the Dim keyword, which is short for Dimension. Dimension in english refers to the amount of space something takes up in the real world, but in computer terms it refers to space in computer memory.
Variables can be declared one at a time or all at once. Below is an example of both methods.
ASP Code:
<%
'Single Variable Declarations
Dim myVar1
Dim myVar2
'Multiple Variable Declarations
Dim myVar6, myVar7, myVar8
%>
ASP Variable Naming Conventions
Once again, ASP uses VBScript by default and so it also uses VBScripts variable naming conventions. These rules are:
  1. Variable name must start with an alphabetic character (A through Z or a through z)
  2. Variables cannot contain a period
  3. Variables cannot be longer than 255 characters (don't think that'll be a problem!)
  4. Variables must be unique in the scope in which it is declared (Basically, don't declare the same variable name in one script and you will be OK).
ASP - Assigning Values to ASP Variables
Assigning values in ASP is straightforward enough, just use the equals "=" operator. Below we have set a variable equal to a number and a separate variable equal to a string.
ASP Code:
<%
'Single Variable Declarations
Dim myString, myNum, myGarbage
myNum = 25
myString = "Hello"
myGarbage = 99
myGarbage = "I changed my variable"
Response.Write("myNum = " & myNum & "<br />")
Response.Write("myString = " & myString & "<br />")
Response.Write("myGarbage = " & myGarbage & "<br />")
%>
Display:
myNum = 25 myString = Hello
myGarbage = I changed my variable