how to update variables in SUB

B

Bill Patel

Variable ta, tb, tc, td and te are not updated in the SUB WriteTableDetail.
As a result, Response write of the ta, tb, tc td and te displays 0.

Sub Page_load(Src AS Object, e AS EventArgs)
IF Request.QueryString("lnk_a") = "1" OR Request.QueryString("lnk_a") =
"3" OR Request.QueryString("lnk_a") = "4" THEN
Main_Program
END IF
END SUB

Sub Main_Program
Dim a AS Integer = 0
Dim b AS Integer = 0
Dim c AS Integer = 0
Dim d AS Integer = 0
Dim e AS Integer = 0
Dim ta AS Integer = 0
Dim tb AS Integer = 0
Dim tc AS Integer = 0
Dim td AS Integer = 0
Dim te AS Integer = 0
Dim I1 AS Integer = 0
Response.Write("<TABLE WIDTH=1300 BORDER=1 CELLSPACING=0 CELLPADDING=0
style=font-family: Arial; font-size: 8pt>")
DO While I1 <= DS1.Tables(0).Rows.Count - 1
IF DS1.Tables(0).Rows(I1).item(4) = "CN" THEN
a = a + 1
ELSE
IF DS1.Tables(0).Rows(I1).item(4) = "IF" THEN
b = b + 1
ELSE
c = c + 1
END IF
END IF
IF DS1.Tables(0).Rows(I1).item(5) = "Y" THEN
d = d + 1
ELSE
e = e + 1
END IF
IF Request.QueryString("lnk_a") = "1" THEN
IF a < 1 AND b > 0 THEN
WriteTableDetail(a, b, c, d, e, ta, tb, tc, td, te)
END IF
ELSE
IF Request.QueryString("lnk_a") = "2" THEN
IF a - b> 1 THEN
WriteTableDetail(a, b, c, d, e, ta, tb, tc, td, te)
END IF
END IF
END IF
I1 = I1 + 1
Loop
Response.Write("<TR >")
Response.Write("<td ALIGN=RIGHT>" & ta & "</td>")
Response.Write("<td ALIGN=RIGHT>" & tb & "</td>")
Response.Write("<td ALIGN=RIGHT>" & tc & "</td>")
Response.Write("<td ALIGN=RIGHT>" & td & "</td>")
Response.Write("<td ALIGN=RIGHT>" & te & "</td>")
Response.Write("</TR >")
Response.Write("</TABLE>")
END SUB

SUB WriteTableDetail
ta = ta + a
tb = tb + b
tc = tc + c
td = td + d
te = te + e
Response.Write("<TR >")
Response.Write("<td ALIGN=RIGHT>" & a & "</td>")
Response.Write("<td ALIGN=RIGHT>" & b & "</td>")
Response.Write("<td ALIGN=RIGHT>" & c & "</td>")
Response.Write("<td ALIGN=RIGHT>" & d & "</td>")
Response.Write("<td ALIGN=RIGHT>" & e & "</td>")
Response.Write("</TR >")
END SUB
 
L

Lars-Erik Aabech

Hi!

First of all, always use Option Explicit in your work. It makes the compiler
tell you when you do mistakes like this one. ;) For information about how to
do this, see these pages:
http://msdn.microsoft.com/library/e...explicitdeclarationofvariables.asp?frame=true
http://msdn.microsoft.com/library/en-us/vblr7/html/vastmoptionexplicit.asp?frame=true

Your a-e and ta-td variables is local to the Main_Program method. The
variables you reference in the WriteTableDetail method are created each time
you call it. If you'd used Option Explicit, the application wouldn't compile
at all.

Anyway, to solve the problem, move the variables out of Main_Program and
make them member variables in the page. I'm not 100% sure of the VB syntax,
but it should look something like this:

public class YourPage
implements....

' Declare your variables outside the methods
public a As Integer = 0
public b As Integer = 0
... ' the rest
public td As Integer = 0

Sub Page_load(Src As Object, e As EventArgs)
' don't dim the variables here
Response.Write...

HTH,
Lars-Erik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top