introduction as visual basic (vb) components become more and more pervasive on the web application development landscape, we are forced to code with more structure in this environment than ever before. no longer is the slick code for our webs restricted to our webs; business objects have made short work of distributing these solutions. we have to plan for this use.
realizing all shops and projects are different, i don抰 believe that i have made a database call from within asp since summer ?998, so understand that i am coming from a component-driven web perspective. furthermore, i can抰 remember the last time a client has asked me to create a static html page. the majority of what we do is dynamic. sometimes, i find myself calling 411 and asking for number where last name = 憇mith?and first name like 慾o%?
one strategy for improving the structure of our web development involves the way we pass data between asp and components. i will usually try to group items (such as rights) together with the user they apply to and pass them in a variant array between components and/or back to asp. in this way i can pass data from record sets (using the getrows method of adodb.recordset) and database updates to com as well as my grouped rights all in the same type (structure) of variable. this is a question of 揺ase of use.? you may prefer to return record sets to asp as themselves. my rationale is that i will definitely use arrays to pass and manipulate data within asp and vb, so i抎 rather pass my record sets that way as well. chalk it up to personal preference. |
the problem my recent discovery of a bug or a design limitation between vb and asp relates to how vb and asp interpret arrays. i rely heavily on arrays, therefore a utility component is one of the first i create. one of the most essential functions in that component is my converttoarray function. this is an asset when writing web apps using activex components.
to quickly build components and save myself from breaking interfaces*, i like to use arrays to pass data back and forth between components. at times i also use this technique to pass data from asp to com. below are two code snippets. looking at snippet 1, i am constructing a simple two-dimensional array (in this case it didn抰 need to be two-dimensional).
snippet 1: create an asp called testcase.asp, and run it.
<% @language="vbscript" %>
<% option explicit %>
<%
dim tcs
dim rc
dim vntinput(0,4)
dim i
vntinput(0,0) = request.querystring("struser")
vntinput(0,1) = request.querystring("intcreate")
vntinput(0,2) = request.querystring("intdelete")
vntinput(0,3) = request.querystring("intmodify")
vntinput(0,4) = request.querystring("intview")
if len(vntinput(0,0)) = 0 then
response.redirect("testcase.asp?struser=mydomain\arichman&intcreate=1&intdelete=1&intmodify=0&intview=1")
end if
vntinputstring = "string input value"
' first make sure we have a valid array
for i = 0 to ubound(vntinput,2)
response.write "loop count " & i & " " & vntinput(0,i) & "<br>"
next
response.write "<hr>"
set tcs = server.createobject("testcases.arrayfailure")
rc = tcs.acceptarray(vntinput)
' now see what we have left
for i = 0 to ubound(vntinput,2)
response.write "loop count " & i & " " & vntinput(0,i) & "<br>"
next
%>
if you construct and parse this variant array in vb script, it will work as intended. if you construct and parse this variant array in vb, it will work as intended as well. however, if you wish to pass your array "by value" (byval) from asp to vb, you will see some messy results. whether by design or mistake, when you pass a variant array from asp to com byval, vb will throw an automation exception, and that is the problem. also, don抰 run snippet 2 (below) in any vb component you have created before saving your data or you will have to kill it via the task manager.
create an activex component called testcases.dll and a class called arrayfailure.
snippet 2:
public function acceptarray(byval vntarray as variant) as integer
doevents
end function
now, of course, you could pass the variant array "by reference" (byref), but there抯 a catch.** it wouldn抰 matter much if these weren抰 shared business objects. yes, i could take the responsibility of always preserving the data in an object passed byref which was intended to be passed and not manipulated, but this seems overly involved and it is not my preference. |
the solution there抯 a com solution for a com problem. create a function that will parse a string and return an array from vb. asp preserves the returned variant array and it will not cause an automation exception when sent into a component byval. to do this, replace the asp-built array in snippet 1 with the sample code in snippet 3.
add this snippet to the above asp sample in place of the asp-built array.
snippet 3
<% @language="vbscript" %>
<% option explicit %>
<%
dim tcs
dim rc
dim vntinput
dim vntinputstring
dim i
dim cta
set cta = server.createobject("testcases.arrayfailure")
rc = cta.converttoarray("a,b,c,d,e,f,g,h,i,j,k,l", ",", 3, vntinput)
note: you will not need to dimension the bounds of this array (see above code appearing in bold) in asp. vb will do that inside your converttoarray function.
create an activex component and class to house useful utilities.
snippet 4:
本文关键:Handling Arrays Between ASP and COM
本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)
| |