We help IT Professionals succeed at work.

classic asp dim variable not working

dpicco
dpicco asked
on
Hello experts,
 
Is there anything wrong with the way I'm declaring and assigning asp variables here? The first variable (lvl_drill) always gets assigned correctly but the second variable (lvl_drill_tla) only gets assigned when lvl_entry = "company". Otherwise it stays at "ope"

dim lvl_drill
dim lvl_drill_tla
if lvl_entry="company" then
lvl_drill="operation"
lvl_drill_tla="ope"
elseif lvl_entry="operation" then
lvl_drill="division"
lvl_drill_tla="div"
elseif lvl_entry="division" then
lvl_drill="business_unit"
lvl_drill_tla="bu"
elseif lvl_entry="business_unit" then
lvl_drill="territory"
lvl_drill_tla="ter"
elseif lvl_entry="territory" then
lvl_drill="market"
lvl_drill_tla="mkt"
elseif lvl_entry="market" then
lvl_drill="location"
lvl_drill_tla="xyz"
end if

Open in new window

Thanks.
Comment
Watch Question

CERTIFIED EXPERT
Awarded 2010
Top Expert 2013
Commented:
Your ifs are messed up.
You need if lvl_entry=="company"

What's happening is you are actually setting lvl_entry to "company" each time and it's always running that part of the if.
CERTIFIED EXPERT
Awarded 2010
Top Expert 2013
Commented:
The = operator means "set equal to"
The == operator means "compare if they are equal"
G_H
Commented:
I have tested this locally, on IIS6, and you code is working fine. I have included the full test code below, so you can see how I did this.

The only remaining question is does any of this happen inside a function or Sub?

 
<%


lvl_entry = "operation"

dim lvl_drill
dim lvl_drill_tla
if lvl_entry="company" then
lvl_drill="operation"
lvl_drill_tla="ope"
elseif lvl_entry="operation" then
lvl_drill="division"
lvl_drill_tla="div"
elseif lvl_entry="division" then
lvl_drill="business_unit"
lvl_drill_tla="bu"
elseif lvl_entry="business_unit" then
lvl_drill="territory"
lvl_drill_tla="ter"
elseif lvl_entry="territory" then
lvl_drill="market"
lvl_drill_tla="mkt"
elseif lvl_entry="market" then
lvl_drill="location"
lvl_drill_tla="xyz"
end if

response.write lvl_drill
response.write lvl_drill_tla

%>

Open in new window


GH
G_H
Commented:
@Tommy,
I think this is VBScript...

GH
CERTIFIED EXPERT
Awarded 2010
Top Expert 2013
Commented:
Yeah, the if then stuff makes me think you are correct. ASP is weird that way. I always use the C++/C# style ASP, so I get stuck in that mode.

The symptoms of the problem are identical to what you would expect if the first if was doing an assignment.

Author

Commented:
GH - this is not inside a function or sub. Thanks.
why dont you use a select case instead of elseif? It would be more efficient and manageable.
CERTIFIED EXPERT
Awarded 2010
Top Expert 2013
Commented:
I agree. Select case is better than a bunch of if elseifs.

Also, if you add Option Explicit at the top of your code file, it will let you know if your problem is due to spelling a variable incorrectly.
G_H
Commented:
@dpicco, OK, can you put my code in a file and try to run it.

@Tommy, LOL, gets me everytime!

@viralypatel, Agreed, if formatted properly would also be easier to read.

GH
G_H
Commented:
Updated code:

 
<%
lvl_entry = "operation"

dim lvl_drill
dim lvl_drill_tla

Select Case lvl_entry
	Case "company"
		lvl_drill="operation"
		lvl_drill_tla="ope"
	Case "operation"
		lvl_drill="division"
		lvl_drill_tla="div"
	Case "division"
		lvl_drill="business_unit"
		lvl_drill_tla="bu"
	Case "business_unit"
		lvl_drill="territory"
		lvl_drill_tla="ter"
	Case "territory"
		lvl_drill="market"
		lvl_drill_tla="mkt"
	Case "market"
		lvl_drill="location"
		lvl_drill_tla="xyz"
	Case Else
		'' ## A Swing and a miss!
End Select

response.write lvl_drill
response.write lvl_drill_tla
%>

Open in new window


... To include Select Case, as agreed by three experts!

Author

Commented:
looks like lvl_drill was getting set elsewhere in the code.
lvl_drill_tla was always set to "ope" because of some sql that was running  and setting lvl_entry to "company" each time the page loaded. Talk about spagetti code! Sorry about that. Thank you very much for your suggestions.
G_H
Commented:
Can you split these points, as the other experts made valid points, which I bundled into my answer...

GH

Author

Commented:
GH - I don't know how to split points after points have been assigned. Can you tell me how to do this? Thanks again for your help.
CERTIFIED EXPERT
Awarded 2010
Top Expert 2013
Commented:
You just hit 'request attention' and ask the admins to reopen the question.
I'll go ahead and do it for you since I'm bored.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.