variable initialization

donkeykong
05-15-2002, 06:03 AM
hi guys

is it possible to intialize variables to 0 as soon as they r 'dim'med
in vbscript /ASP ??

istead of the irritating
dim i,j,k,l
i=0
j=0
k=0
l=0

Thinker
05-15-2002, 08:08 AM
Since variables default to variant, if you treat them as a numeric
type when you use them, they should default to 0 without any
explicit initialization. There isn't any code construct that allows for
declaration and initialization in the same statement.

Rezner
05-15-2002, 10:22 AM
If you do not initialize a variable, nothing will be displayed on the screen when the script runs. For example:
Dim intYear
Response.Write "The current year is: " & intYear
Will output "The current year is:" and nothing else. But...
Dim intYear
intYear = Year(Now)
Response.Write "The current year is: " & intYear
Will output "The current year is: 2002"

You have to remember that, when you use ASP, you are not programming... you are scripting (server side.) The difference is that the objects and variables are being carved out as universal blanks on the server and not explicitly on the local machine. With this in mind, you will notice that "Dim x As Integer" causes an error.

In ASP the "Dim" statement really doesn't do all that much. The below two chunks of code are the exact same thing:x = 0

'accomplishes the same thing as:
Dim x
x = 0I'm sure that, in the inner depths of ASP, there is a great reason for the Dim statement (program readability) but, for most purposes, you can live without it.

PS - If you don't want to use the Dim statement, make sure that you do NOT use the "Option Explicit" line at the top of your scripts.

Thinker
05-15-2002, 02:43 PM
Response.Write "The current year is: " & intYear
This prints nothing because it is using intYear as a string (because
of the concatenator operator).

This will show 0.
Response.Write "The current year is: " & CInt(intYear)

donkeykong
05-15-2002, 10:47 PM
Thanks guys

guess i'll have to thrash it out the hard way .....:(

there shld have been some construct like
dim x,y,z,a = 0 or something .....
ah well , c'est la vie !!!!!!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum