Velocity Reviews > Odd function problem

# Odd function problem

Mike
Guest
Posts: n/a

 01-15-2006
I have a function as follows:

function docolors(Rstart,Gstart,Bstart,color,incrementer)
r=Rstart
g=Gstart
b=Bstart
response.write "<div><table><tr>"
for color = 0 to 255
response.write "<td style=""background-color:rgb"
response.write "(" & r & "," & g & "," & b & ");"">"
response.write "<a href=""#"" title=""R"
response.write r & ":G" & g & ":B" & b & """>"
response.write "&nbsp;&nbsp;&nbsp;</a></td>"
color=color+incrementer
next
response.write "</tr></table></div>"
end function

When its called thus:

docolors 0,0,0,r,9

it increments the value of red by 9 and writes 26 table cells to the
page gradually going from black to red (which is what I want).

I have a form to collect user provided values for the function's
arguments and dynamically build a call to docolors() using the form
field values:
<%
sub dropdown()
for i = 0 to 255
response.write "<option value=""" & i & """>" & i & "</option>" &
vbcrlf
next
end sub
%>
<form action="" method="post">
<strong>Start Values</strong><br />
Red:<select name="r">
<%
dropdown
%>
</select>
Green:<select name="g">
<%
dropdown
%>
</select>
Blue:<select name="b">
<%
dropdown
%>
</select><br /><br />
<strong>Colour to increment value of:</strong><br />
<input type="radio" name="c" value="r" />Red<br />
<input type="radio" name="c" value="g" />Green<br />
<input type="radio" name="c" value="b" />Blue<br /><br />
<strong>Incrementer:</strong><br />
<select name="i">
<%
dropdown
%>
</select><br />
<input type="submit" name="submit" value="Submit" />
</form>

<%
x=cint(request.form("r"))
y=cint(request.form("g"))
z=cint(request.form("b"))
h=trim(cstr(request.form("c")))
i=cint(request.form("i"))

docolors x,y,z,h,i
%>

and it seems that all the values translate correctly if I
response.write the whole lot:

response.write "call docolors(" & x & "," & y & "," & z & "," & h & ","
& i & ")<br />"

but the thing fails to increment the value of h - the paramenter that
goes where color is in the function. x,y,z,and i all work as expected.

My logic is clearly faulty somewhere. Can anyone see where?

TIA

Mike

Paxton
Guest
Posts: n/a

 01-17-2006

Dave Anderson wrote:
> Mike wrote:
> > function docolors(Rstart,Gstart,Bstart,color,incrementer)
> > r=Rstart
> > g=Gstart
> > b=Bstart
> > response.write "<div><table><tr>"
> > for color = 0 to 255
> > response.write "<td style=""background-color:rgb"
> > response.write "(" & r & "," & g & "," & b & ");"">"
> > response.write "<a href=""#"" title=""R"
> > response.write r & ":G" & g & ":B" & b & """>"
> > response.write "&nbsp;&nbsp;&nbsp;</a></td>"
> > color=color+incrementer
> > next
> > response.write "</tr></table></div>"
> > end function

>
> ...
>
> > but the thing fails to increment the value of h - the
> > paramenter that goes where color is in the function.
> > x,y,z,and i all work as expected.

>
>
> You are ignoring the parameter [color] and initializing it to 0 in your
> loop.
>
>
> --
> Dave Anderson
>

Thank, Dave. I've rewritten it and it works as expected now:

function docolors(Rstart,Gstart,Bstart,color,incrementer)
dim r,g,b,i
r=Rstart
g=Gstart
b=Bstart
response.write "<div><table><tr>"
select case color
case "r"
for i = r to 255
response.write "<td style=""background-color:rgb"
response.write "(" & i & "," & g & "," & b & ");"">"
response.write "<a href=""#"" title=""R"
response.write i & ":G" & g & ":B" & b & """>"
response.write "&nbsp;&nbsp;&nbsp;</a></td>"
i=i+incrementer
next
case "g"
for i = g to 255
response.write "<td style=""background-color:rgb"
response.write "(" & r & "," & i & "," & b & ");"">"
response.write "<a href=""#"" title=""R"
response.write r & ":G" & i & ":B" & b & """>"
response.write "&nbsp;&nbsp;&nbsp;</a></td>"
i=i+incrementer
next
case "b"
for i = b to 255
response.write "<td style=""background-color:rgb"
response.write "(" & r & "," & g & "," & i & ");"">"
response.write "<a href=""#"" title=""R"
response.write r & ":G" & g & ":B" & i & """>"
response.write "&nbsp;&nbsp;&nbsp;</a></td>"
i=i+incrementer
next
end select
response.write "</tr></table></div>"
end function

Mike