I'm building a treeview page that is generated and expanded as the user
opens links from it, and am using an array stored in a session variable
to maintain the tree.
The problem I've hit is that of how to corectly dim the array variable.
the variables are dimed after option explicit, but
having
dim arrtree
there throws a type mismatch at line 116 when the array is empty
but not when its populated
having
dim arrtree()
throws same at line 87 when the array is populated, but is fine when its
empty
obviously trying to have two dim statements in the if-else throws a
'redefined' error.
other than removing the option line, how to get round this?


this is the part of the code that is doing the work:

if intmenuid > 0 then
if session("arrtreeflag") > "" then

arrtree = session("arrtree")'########line 87
thisid = Request.QueryString("thisid")

x = ubound(arrtree,2)+1
arrtree(9,thisid) = 1
sql = "select tagid,rtrim(m1.menuname) from navigate_tags t
left join navigate_menus m1 on tagid = m1.menuid where
parentid = "&intmenuid&" and menuname is not null order by
node"
else
sql = "select tagid,rtrim(menuname) from navigate_tags,
navigate_menus where parentid = 0 and node = 0 and tagid =
menuid"
x = 0
level = 0
parentnode = 0
strmessage = "reset"
end if
else

sql = "select tagid,rtrim(menuname) from navigate_tags,
navigate_menus where parentid = 0 and node = 0 and tagid = menuid"
x = 0
level = 0
parentnode = 0
intmenuid = 0
end if
if strmessage > "" then Response.Write "timeout - tree reset to top<br/>"
'sql = "cn_viewtree_count "&intmenuid&" "

Response.Write "<!-- sql["&sql&"] -->"
set rs = conn.execute(sql)
if not rs.eof then
do while not rs.eof
Response.Write "<!-- x["&x&"] -->"
redim preserve arrtree(10,x) ''''##########line 116
arrtree(0,x) = rs(0) ' node id
arrtree(1,x) = 0 ' 0 = menu 1 = item
if intmenuid > 0 then arrtree(2,x) = intmenuid ' parentid
if intmenuid = 0 then arrtree(2,x) = 0 ' parentid
arrtree(3,x) = rs(1) ' name
Response.Write "<!-- sql[cn_viewtree_child "&rs(0)&" ] -->"
set rs_c = conn.execute("cn_viewtree_child "&rs(0)&" ")
arrtree(4,x) = rs_c(0) ' has child 0 false 1 true
arrtree(5,x) = 0 ' has been used in tree
arrtree(6,x) = level ' tree level
arrtree(7,x) = x ' array node
arrtree(8,x) = intmenuid'parentnode 'parent array node
arrtree(9,x) = 0 ' has been opened


Response.Write "adding index["&x&"] id["&arrtree(0,x)&"]par
["&arrtree(2,x)&"]name["&arrtree(3,x)&"]child["&arrtree(4,x)&"]level
["&arrtree(6,x)&"]parentnode["&arrtree(8,x)&"] <br />"
x=x+ 1
rs.movenext
loop
end if
'arrtree(4,x) = 0 ' has child 0 false 1 true
set session("arrtree") = nothing
session("arrtree") = arrtree
set session("arrtreeflag") = nothing
session("arrtreeflag") = "current"

Re: getting type mismatch with array by Roland

Roland
Thu Jan 06 03:32:19 CST 2005

"s_m_b" wrote in message
news:Xns95D596A4CF4AEsmb2000nshotrmailcom@207.46.248.16...
: I'm building a treeview page that is generated and expanded as the user
: opens links from it, and am using an array stored in a session variable
: to maintain the tree.
: The problem I've hit is that of how to corectly dim the array variable.
: the variables are dimed after option explicit, but
: having
: dim arrtree
: there throws a type mismatch at line 116 when the array is empty
: but not when its populated
: having
: dim arrtree()
: throws same at line 87 when the array is populated, but is fine when its
: empty
: obviously trying to have two dim statements in the if-else throws a
: 'redefined' error.
: other than removing the option line, how to get round this?

When you:

dim arrtree

... this is not an array. You can test is with:

Response.Write(typename(arrtree))

When you:

dim arrtree()

This is an array and you need to:

redim preserve arrtree(next array element number)

... to add additional elements to the array.

So, on line 87, when you dim arrtree() and your session value is populated,
you need to first redimension the array for additional elements.

If I am adding elements, I put mine in a loop:

If I have an array with 10 elements called arr, I might have added those
elements with:

dim arr
arr = split("0,1,2,3,4,5,6,7,8,9",",")

If I want to split them up, odd in one array, even in another, one way would
be:

dim i, j, k, arrOdd(), arrEven(), o, e
arr = split("0,1,2,3,4,5,6,7,8,9",",")
j = 0 : k = 0
for i = 0 to ubound(arr)
if i mod 2 = 0 then
redim preserve arrEven(j)
arrEven(j) = arr(i)
j = j + 1
else
redim preserve arrOdd(k)
arrOdd(k) = arr(i)
k = k + 1
end if
next

Then to see what is in my arrays...

sub lprt(str)
Response.Write(str & "<br />" & vbCrLf)
end sub

o = join(arrOdd," ")
e = join(arrEven," ")
lprt("Odd numbers: " & o)
lprt("Even numbers: " & e)

It appears you will have to test if the session is populated before you
redimension the array. And if you're going to be redimensioning the array,
you should dimension it as arrtree(), not arrtree initially.

Code: http://kiddanger.com/lab/arraysplit.asp
Source: http://kiddanger.com/lab/ss_arraysplit.asp

HTH...

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp