Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Effect of undimmed variables?

Status
Not open for further replies.

Coder7

Programmer
Oct 29, 2002
224
US
Good afternoon.

I have a general question regarding what types of problems can stem from undimmed vb script variables in an asp page. I'm having INTERMITTENT problems with an application in production and I currently don't have a way to view variable values in production.

In the course of looking for causes of the problem, I noticed that, on the page where I'm having the issues, there are some variables that I didn't dim. I cloned some code from another asp page in our application and I neglected to remove some variables that I don't need and I'm wondering if this could be a possible cause of my current issues, especially the fact that the problem is intermittent. The only thing thing that I do with these unnecessary variables is assign a value to them from the value that's returned from the database in a recordset.

Thanks for any thoughts regarding this.



 
are u gettin an error message?
when u turn on option explicit then you generate an error regarding undeclared variables.
as for other types of problems: generally i dont know of any if you dont declare your variables. its just good programming practice to declare them to allow you and others who look at your code to know why they were used and what they were used for.

Steve

everyday something new is learned, hmmm...
 
There is no requirement to Dim your variables unless you declare Option Explicit at the head of your page. I have seen multitude of problems stemming from not using Option Explicit and explicitlyDimming variables though. For example, say you mispell a variable. With Option Explicit you get an error message due to an undimmed variable and you can fix it, without Optin Explicit the server just assumes you want another variable and suddenly you start getting the wrong results (or no results at all), or you start getting wierd error messages (like object not existing, etc) that are not obvious indicators of a misspelled variable.
I believe their is also a performance issue related to using Option Explicit an explicitly declaring your variables. I think the quoted numbe from the last article I read on this is tat using Option Explicit can increase the performance of your pages by up to something like 10%. So not only are you protected from misspelled variables your also getting beter performance. Thereasoning behind this is due to thefact that since all of te variables are declared before use, the system can internally reference them by their ordinal rather than by name. This means that instead of the serverhaving to match a name to an ordinal with every single call it can instead match the name to an ordinal one time, then reuse that same ordinal thoughout the script. Faster access to variable values means faster execution, etc.

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
and there we have it... true reason of why to declare variables that isn't learned in any text book

good post tarwn, i never even thought of it that way!

Steve

everyday something new is learned, hmmm...
 
Yeah, sorry about all the typos, I think I still haven't gotten used to this keyboard. Used my old one for 8-10 years, only had this one for about 6 months tops. I used to typo a lot but now it seems like every other word...maybe I should go pressure wash the old one and put it back into service :p

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
8-10 years? sounds like that's seen windows workgroups... go get one of those quiet keys with wireless mouse, they're awesome!

Steve

everyday something new is learned, hmmm...
 
Tarwn, I think that would make a good FAQ. ;-)

------------------------------------------------------------------------------------------------------------------------
"The major difference between a thing that might go wrong and a thing that cannot possibly go wrong is that when a thing that cannot possibly go wrong goes wrong it usually turns out to be impossible to get at or repair."
--Dou
 
I do use Option Explicit and I do not get any errors. The problem seems to be a fall through in the logic but there is no consistency. I just wondered if these undimmed variables could be the culprit but based on all of your responses, that seems to not be the case. Thanks VERY MUCH for your valuable input.
 
yea my logic seems to fail no and then... best bet is to manually walk through the code...
or post some of it and see what everyone else can come up with

Steve

everyday something new is learned, hmmm...
 
Coder7
If your using Option Explicit then you should be getting an error message for every single one of those un-Dimmed variables. The only thing that would allow the un-Dimmed variables to exist in the script without errors bein throw would be if they are in blocks of code that do not get executed. Example:
Code:
<%
Option Explicit

If Day(Now) = 16 Then
	a = a + 1
	Response.Write a
End If
%>
That example will only throw out error messages on the 16th of every month becauser the inner portion of the If statement will only be executed on the 16th.

it could be that you have soemthing similar in your code, where a block of code with undimmed variables is only getting called occasionally, then outputting an error instad of processing because you never Dimmed the variables.

Chopstik Nah, that was a little one. Ask onpnt about me posting FAQs as posts :). Every FAQ I have posted so far was originally a post that was about the same length or longer and generally written on the fly. There's also a bunch I never posted as FAQs that have since disappeared.

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
that would pose a problem...

Steve

everyday something new is learned, hmmm...
 
Tarwn,

Here's the entire page. There are commented out debugging statements interspersed throughout the code. I commented out all of the on error resume next and still got no errors.

--------------------------------------
<%@ Language=VBScript %>

<%
' ***********************************************
' FILE NAME : Default.asp
'
' LAYOUT DEVELOPMENT : Unknown
'
' CODE DEVELOPMENT : Unknown
'
' FUNCTIONAL PURPOSE : Navigate logged in person
' to correct asp page depending on who he/she is.
'
' CREATE DATE : 08/19/2002
'
' MODIFICATIONS / CHANGE HISTORY :
' 1. 03/18/04 in ASSOCIATE table.
'**************************************************

Option Explicit

Dim strCurrPage
strCurrPage = "default.asp"
%>
<!-- #include file="Includes/AuthUser.asp" -->

<%
Dim strPage, strPage1, strPage2, strPage3

on error resume next

strPage = "Scripts/MainMenu.asp"
strPage1 = "Scripts/Menu_Eval.asp"
strPage2 = "Scripts/Eval_List.asp"
strPage3 = "Scripts/Eval_Search.asp"

Response.Cookies("glblAKAName") = sAKAName
Response.Cookies("glUserID") = sTSOID 'sUserID
Response.Cookies("glAuthCode") = sAuthCode

'sAuthCode = 2
'sAuthCode = 3
'sAuthCode = 4

'Response.Write("sAuthCode = " & sAuthCode & "<br>")

Dim glStrConn
Dim objBepmDbSp
Dim strCompanyInd, strTsoId

strTsoId = sTsoId
'strTsoId = "kcs3764"
'strTsoId = "zzzzzzzzz"
'strTsoId = "kcsacs1"
'Response.Write("strTsoID = " & strTsoId & "<br>")

glStrConn = Application("sDBConn")

Call createBEPMObject
Call getAssocRecord
Call destroyBEPMObject

'Response.Write("ready to check auth levels")
'Response.Write("strTsoId = " & strTsoId & "<br>")
'Response.Write("sAuthCode = " & sAuthCode & "<br>")
'Response.Write("strCompanyInd = " & strCompanyInd & "<br>")

Dim authNav

'If IsNull(strCompanyInd) Then
If CStr(strCompanyInd) = "" Then
If sAuthCode = 1 Then
authNav = "H1"
ElseIf sAuthCode = 2 Then
authNav = "H2"
ElseIf sAuthCode = 3 Then
authNav = "H3"
ElseIf sAuthCode = 4 Then
authNav = "H4"
End If
Else
If sAuthCode = 1 Then
authNav = "E1"
ElseIf sAuthCode = 2 Then
authNav = "E2"
ElseIf sAuthCode = 3 Then
authNav = "E3"
ElseIf sAuthCode = 4 Then
authNav = "E4"
End If
End If

'Response.Write("authNav = " & authNav & "<br>")

Select Case authNav
Case "E1"
Call Display_Error_Page(100, "External associates are not authorized for this access level. Please contact administrator ...", "<B><Font color='Red'>Un-Authorized Access :</Font></B>")
Case "E2"
Response.Cookies("glCompanyInd") = strCompanyInd
Response.Redirect strPage3
Case "E3"
Call Display_Error_Page(100, "External associates are not authorized for this access level. Please contact administrator ...", "<B><Font color='Red'>Un-Authorized Access :</Font></B>")
Case "E4"
Response.Redirect strPage2
Case "H1"
Response.Redirect strPage
Case "H2"
Response.Redirect strPage1
Case "H3"
Response.Redirect strPage1
Case "H4"
Response.Redirect strPage2
End Select

'start obsolete code 03/18/04
'If CInt(sAuthCode) = 1 then
' Response.Redirect strPage
'Elseif CInt(sAuthCode) = 2 or CInt(sAuthCode) = 3 then
' Response.Redirect strPage1
'Elseif CInt(sAuthCode) = 4 then
' Response.Redirect strPage2
'Else
' Call Display_Error_Page(100, "You are not Authorized to access this System. Please contact administrator if you think you have access and are not able to get in the system ...", "<B><Font color='Red'>Un-Authorized Access :</Font></B>")
'End if
'end obsolete code 03/18/04
%>

<%' Include Files %>
<!-- #include file="Includes/adovbs.inc" -->

<%
Sub getAssocRecord
On Error Resume Next
Dim objRs, strError
Dim inPars(2)
inPars(0) = strTsoID
inPars(1) = ""
inPars(2) = 1

'Dim i
'For i = 1 To UBound(inPars)
' Response.Write("inPars(" & i & ") = " & inPars(i) & "<BR>" )
'Next

Set objRs = objBepmDbSp.getRecords(CStr(glStrConn), "sp_Assoc_get", inPars)
If NOT ( objRs.BOF And objRs.EOF ) Then
strAssocName = objRs.Fields("assoc_desc").Value
strEvalId = objRs.Fields("Eval_Id").Value
strReportsTo = objRs.Fields("reports_to_level").Value
strReportsTo_TSOID = objRs.Fields("reports_to_tso_id").Value
strCompanyInd = objRs.Fields("Company_Ind").Value
strStatus = objRs.Fields("Status").Value
strCreatedBy = objRs.Fields("Created_By").Value
dtCreatedDate = objRs.Fields("Created_Date").Value
strActionBy = objRs.Fields("Action_By").Value
dtEndDate = objRs.Fields("End_Date").Value
'Response.Write("in get assoc record")
'Response.Write("strCompanyInd =" & strCompanyInd & "<br>")
Else
Response.Write("No records found in ASSOCIATE table for logon tso id")
End If
objRs.Close

If Not objRs is Nothing Then
Set objRs = Nothing
End If
End Sub

Sub createBEPMObject
If NOT ( isObject(objBepmDbSp) ) Then
Set objBepmDbSp = Server.CreateObject("BEPerformEval.clsBEPMDB")
End If
End Sub

Sub destroyBEPMObject
If isObject(objBepmDbSp) Then
If Not objBepmDbSp is Nothing Then
Set objBepmDbSp = Nothing
End If
End If
End Sub
%>
 
I'm suprised that the position of your option explicit hasn't thrown an error. I thought it was manditory(if used) to position option explicit as the immediate next line after the page declaration. Might want to look into that.

Code:
<%@ LANGUAGE="VBSCRIPT" CODEPAGE="1252" %>
<% option explicit %>
<%
the rest........
%>

Also, and I'm not trying to sound snide, but do you even have script debugging running? Like everyone has said, you SHOULD be getting errors for every one of those undimmed vars. and the option explicit. Not sure what server model you are using, but IIS5 uses a virtual directory IISHelp to serve error pages. Also in IIS, go to the properties of the main directory the file is in, on the directories tab click the configuration button, click the debugging tab, check -> Enable ASP client-side script debugging, radio -> Send detailed ASP messages to client. If you don't have the IISHelp Virtual directory, let me know, I'll help you try to get it set up.

I hope this helps.
ryandoah
 
Comment out the On Error Resume Next. Generally you don't want to have tat active until your ready for the script to go to production and want to catch internal errors that arise from things like database connection errors, etc. Right now it is probably catching errors in your code and just ignoring them, causing problems later in the code.

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top