Search
Sponsors

Posts Tagged ‘tutorials’

variables in VBScript

Tuesday, February 26th, 2008

variables in VBScript

Variables

Before you use a variable in VBScript you should declare its name . To do this in VBScript you use the dim keyword like the following examples.

You should follow a few rules when you declare variable names and these are

  1. must begin with a letter
  2. cannot contain a period (.)
  3. cannot have more than 255 characters

dim x
dim y
dim myAge
dim myWage

You could have put these all on the same line like this

dim x , y , myAge , myWage

The initial value of the variable is empty in VBScript

To prevent mistakes when declaring variables you can use Option Explicit before declaring them and this means you must declare all your variables with dim , public or private , if you dont you will get an error . Anyone who has created scripts in JavaScript will see the advantage here where you can declare a variable like this

myvar

but if you make a spelling mistake like myyar later on this will not be flagged and can lead to some strange and hard to find errors.

Types of Variables

In VBScript a variable can hold any a value of any data type and this data type does not have to be set when the variable is declared . This means you can change the data type of your variable in your program .

Datatype Description
Empty Variant is uninitialized . Value is 0 for number variables and a zero length string “” for empty strings
Null Variant which contains no valid data
Boolean either True or False
Byte integer in the range 0 to 255
Integer integer in the range -32768 to 32767
Currency -922,337,203,685,477.5808 to 922,337,203,685,477,5807
Long an integer value in the range -2,147,483,648 to 2,147,483,647
Single single precision floating point number in the range of 3.402823E38 - 45 to 1.401298E - 45 for negative values and 1.401298E - 45 to 3.402823 E 38 for positive values
Double a double precision floating point number
Date/Time a number that represents the date between Jan 1 100 to Dec 31 9999
String a variable length string that can contain 2 billion characters
Object contains an object
Error contains an error number

procedures in VBScript

Tuesday, February 26th, 2008

procedures in VBScript

Procedures

There are two types of procedures in VBScript , Sub procedures or Function procedure . If you do not need to return a value you can use the Sub procedure but if your statements need to return a value then use a Function.

Sub Procedures

a sub procedure is a statement / series of statements that are enclosed by the Sub and End Sub statements
a sub procedure cannot return a value but it can perform certain actions
If you do not have any arguments you use ( )
Can take arguments that are passed to it by a calling procedure

Sub samplesub ( )
collection of statements here
End Sub

Example:

sub message() document.write(”this is a VBScript Sub procedure”) end sub call message()Here is the code for the above example above

This code would go in the <HEAD> section of your page ideally

<script language=”VBScript”>
sub message()
document.write(”this is a VBScript Sub procedure”)
end sub

And to call this sub procedure we would use the following script

<script language=”VBScript”>
call message()
</script>

Function Procedures

a function procedure is a statement / series of statements that are enclosed by the Function and End Function statements
a function procedure can return a value and it can perform certain actions
If you do not have any arguments you use ( ) on their own
Can take arguments that are passed to it by a calling procedure
it can return a value by assigning a value to its name

Function samplefunction(  )
collection of statements here
samplefunction = somevalue
End Function

Example

function message1() message1 = “a sample VBScript function” end function document.write “here is ” & message1() Here is the code for the example aboveThis would go in the <HEAD> section

<script language=”VBScript”>
function message1() message1 = “a sample VBScript function”
end function
</script>

This code would call the function

<script language=”VBScript”> document.write “here is ” & message1() </script>

Pointers and Dynamic Allocation of Memory

Thursday, February 21st, 2008

Pointers and Dynamic Allocation of Memory There are times when it is convenient to allocate memory at
run time using malloc(), calloc(), or other allocation functions.
Using this approach permits postponing the decision on the size
of the memory block need to store an array, for example, until
run time. Or it permits using a section of memory for the
storage of an array of integers at one point in time, and then
when that memory is no longer needed it can be freed up for other
uses, such as the storage of an array of structures.

When memory is allocated, the allocating function (such as
malloc(), calloc(), etc.) returns a pointer. The type of this
pointer depends on whether you are using an older K&R compiler or
the newer ANSI type compiler. With the older compiler the type
of the returned pointer is char, with the ANSI compiler it is
void.

If you are using an older compiler, and you want to allocate
memory for an array of integers you will have to cast the char
pointer returned to an integer pointer. For example, to allocate
space for 10 integers we might write:

int *iptr;
iptr = (int *)malloc(10 * sizeof(int));
if(iptr == NULL)
{ .. ERROR ROUTINE GOES HERE .. }

If you are using an ANSI compliant compiler, malloc() returns
a void pointer and since a void pointer can be assigned to a
pointer variable of any object type, the (int *) cast shown above
is not needed. The array dimension can be determined at run time
and is not needed at compile time. That is, the “10″ above could
be a variable read in from a data file or keyboard, or calculated
based on some need, at run time.

Because of the equivalence between array and pointer
notation, once iptr has been assigned as above, one can use the
array notation. For example, one could write:

int k;
for(k = 0; k < 10; k++
iptr[k] = 2;

to set the values of all elements to 2.

Even with a reasonably good understanding of pointers and
arrays, one place the newcomer to C is likely to stumble at first
is in the dynamic allocation of multi-dimensional arrays. In
general, we would like to be able to access elements of such
arrays using array notation, not pointer notation, wherever
possible. Depending on the application we may or may not know
both dimensions at compile time. This leads to a variety of ways
to go about our task.

As we have seen, when dynamically allocating a one
dimensional array the dimension can be determined at run time.
Now, when using dynamic allocation of higher order arrays, we
never need to know the first dimension at compile time. Whether
we need to know the higher dimensions depends on how we go about
writing the code. Here I will discuss various methods of
dynamically allocating room for 2 dimensional arrays of integers.

First we will consider cases where the 2nd dimension is known
at compile time.

METHOD 1:

One way of dealing with the problem is through the use of the
“typedef” keyword. To allocate a 2 dimensional array of integers
recall that the following two notations result in the same object
code being generated:

multi[row][col] = 1; *(*(multi + row) + col) = 1;

It is also true that the following two notations generate the
same code:

multi[row] *(multi + row)

Since the one on the right must evaluate to a pointer, the
array notation on the left must also evaluate to a pointer. In
fact multi[0] will return a pointer to the first integer in the
first row, multi[1] a pointer to the first integer of the second
row, etc. Actually, multi[n] evaluates to a pointer to that
array of integers which makes up the n-th row of our 2
dimensional array. That is, multi can be thought of as an array
of arrays and multi[n] as a pointer to the n-th array of this
array of arrays. Here the word “pointer” is being used
to represent an address value. While such usage is common in the
literature, when reading such statements one must be careful to
distinguish between the constant address of an array and a
variable pointer which is a data object in itself.

Consider now:
———————————————–
#include <stdio.h>
#define COLS 5

typedef int RowArray[COLS];
RowArray *rptr;

int main(void)
{
int nrows = 10;
int row, col;
rptr = malloc(nrows * COLS * sizeof(int))
for(row = 0; row < nrows; row++)
for(col = 0; col < COLS; col++)
{
rptr[row][col] = 17;
}
}
————————————————-
Here I have assumed an ANSI compiler so a cast on the void
pointer returned by malloc() is not required. If you are using
an older K&R compiler you will have to cast using:

rptr = (RowArray *)malloc(…. etc.

Using this approach, “rptr” has all the characteristics of an
array name and array notation may be used throughout the rest of
the program. That also means that if you intend to write a
function to modify the array contents, you must use COLS as a
part of the formal parameter in that function, just as we did
when discussing the passing of two dimensional arrays to a
function.

METHOD 2:

In the METHOD 1 above, rptr turned out to be a pointer to
type “one dimensional array of COLS integers”. It turns out that
there is syntax which can be used for this type without the need
of typedef. If we write:

int (char *xptr)[COLS];

the variable xptr will have all the same characteristics as the
variable rptr in METHOD 1 above, and we need not use the
“typedef” keyword. Here xptr is a pointer to an array of
integers and the size of that array is given by the #defined
COLS. The parenthesis placement makes the pointer notation
predominate, even though the array notation has higher
precedence. i.e. had we written

int char *xptr[COLS];

we would have defined xptr as an array of pointers holding the
number of pointers equal to that #defined by COLS. Which is not
the same thing at all. However, arrays of pointers have their
use in the dynamic allocation of two dimensional arrays, as will
be seen in the next 2 methods.

(more…)

Translate