Changing this code to a function? (how do you make functions?)

K

Ken Fine

I have a menu system that has nodes that can be opened or closed. In an
effort to make my code more manageable, I programmed a little widget tonight
that keeps track of the open/active item and automatically builds
querystrings for my redirect URLS. The code for this follows. It defines an
ASP Dictionary object, and key/value pairs for each, and builds appropriate
querystrings based on comparison with a status variable.

The way it works for now is I manually declare and re-declare a variable
("CurrentContentClass") in places throughout the code where there is a new
menu item. I embed this same block of code repeatedly through my
application, one for each menu node. This is a really stupid approach, and
I'm sure there's a better way.

Better would be to create a single function into which you could feed
parameters and get output. I've used lots of functions in my auspicious
career in ASP, but I've never created one myself.

Can anyone suggest tutorials or approaches such that I can learn how to
create my own functions? Any guidance based on what's below?

Note that there's some (lots) of redundancy and cluelessness expressed the
code below. Some of this is newbieness; some is to accomodate future
functionality. While I'd appreciate any ideas for optimization, what I'm
really looking for here is a general "how-to" on making functions.

Thanks for any help.

-KF

<%Dim dicRedirect
Set dicRedirect=Server.CreateObject("Scripting.Dictionary")
dicRedirect.Add "Experts","uwelns"
dicRedirect.Add "UPhoto","uspns"
dicRedirect.Add "Uweek","uwkns"
dicRedirect.Add "Releases","uwnrns"
' The value element of the key/value pairs above is used to build the URL

%>

<%
Dim RedirectStringOpen
Dim counter
Dim SecondCounter
counter=0
SecondCounter=0
'set variables "counter" and "SecondCounter" to zero
Dim currentDictionaryCategory
'something to store the active value in the dictionary as we loop through
For each whatever in dicRedirect
' for every item in the dictionary I just defined

currentDictionaryCategory= Cstr(dicRedirect.item(whatever))
'set the currentDictionaryCategory to the value setting of the active
key/value pair
counter=counter+1
'Increment the counter by one. Counter is unused for now, may find
application later
if counter<>1 then
if Cstr(currentDictionaryCategory) = Cstr(CurrentContentClass)
then RedirectStringOpen = (RedirectStringOpen & "&" &
dicRedirect.item(whatever)&"=o")
' If the current dictionary item is the same as the variable
CurrentContentClass, then add the dictionary item name to the QueryString
with a value of "o", e.g. uwkns=o

if Cstr(currentDictionaryCategory) <> Cstr(CurrentContentClass)
then RedirectStringOpen = (RedirectStringOpen & "&" &
dicRedirect.item(whatever)&"=c")
' If the current dictionary item is not the same as the variable
CurrentContentClass, then add the dictionary item name to the QueryString
with a value of "c", e.g. uwkns=c

end if

if counter=1 then
if Cstr(currentDictionaryCategory) = Cstr(CurrentContentClass)
then RedirectStringOpen = (RedirectStringOpen &
dicRedirect.item(whatever)&"=o")
if Cstr(currentDictionaryCategory) <> Cstr(CurrentContentClass)
then RedirectStringOpen = (RedirectStringOpen &
dicRedirect.item(whatever)&"=c")
end if


If SecondCounter<>1 Then
redirectStringClosed = (redirectStringClosed & "&" &
dicRedirect.item(whatever)&"=c")
else
redirectStringClosed = (redirectStringClosed &
dicRedirect.item(whatever)&"=c")
end if

Next
%>
 
K

Ken Schaefer

Functions in VBScript are simple (relatively!).

<%
Function nameOfFunction( _
ByVal firstParameter, _
ByVal secondParameter, _
ByRef thirdParameter _
)

Dim anInternalVariable

nameOfFunction = "return this string"

End Function
%>

To declare a function, you use the Function keyword, followed by the name of
the function. In brackets you then has a list of parameters (bits of data
you want to pass into the function).

Passing a parameter ByVal is usual, and this makes a copy of the data which
you can manipulate inside the function. The original value (outside the
function) is not affected. You can also pass information ByRef, which passes
a reference to the original bit of data to your function. Typically this is
used for objects, since you don't necessary want to copy an existing object
(eg making a copy of an ADO Connection object that's already connected to a
database means the database needs to support two connections when one would
do). Any info that is passed ByRef, and changed in the function, is also
changed "outside" the function.

You can then DIM any internal variables. These variables do not exist
outside the function, and can't be refered to by code outside the function.

You then do your work. You can refer to the passed in data using the names
in the parameter list (eg firstParameter, secondParameter etc). To return a
result, you assign it to the function name.

Example function:

Function ArrayFromSQL( _
ByVal strSQLString, _
ByRef objConn _
)

Dim objRS ' ADODB.Recordset
Dim arrResults

Set objRS = objConn.Execute(strSQLString)

If not objRS.EOF then
arrResults = objRS.GetRows
End If

ArrayFromSQL = arrResults

' Here we call a subroutine
Call objDispose(objRS, True, True)

End Function

Sub objDispose( _
ByRef objToDispose, _
ByVal blnClose, _
ByVal blnNothing _
)

If blnClose then
objToDispose.Close
End If

If blnNothing then
Set objToDispose = Nothing
End If

End Sub

' Here we call the first function
myArray = ArrayFromSQL(objConn, "SELECT * FROM myTable"

HTH

Cheers
Ken


: I have a menu system that has nodes that can be opened or closed. In an
: effort to make my code more manageable, I programmed a little widget
tonight
: that keeps track of the open/active item and automatically builds
: querystrings for my redirect URLS. The code for this follows. It defines
an
: ASP Dictionary object, and key/value pairs for each, and builds
appropriate
: querystrings based on comparison with a status variable.
:
: The way it works for now is I manually declare and re-declare a variable
: ("CurrentContentClass") in places throughout the code where there is a new
: menu item. I embed this same block of code repeatedly through my
: application, one for each menu node. This is a really stupid approach, and
: I'm sure there's a better way.
:
: Better would be to create a single function into which you could feed
: parameters and get output. I've used lots of functions in my auspicious
: career in ASP, but I've never created one myself.
:
: Can anyone suggest tutorials or approaches such that I can learn how to
: create my own functions? Any guidance based on what's below?
:
: Note that there's some (lots) of redundancy and cluelessness expressed the
: code below. Some of this is newbieness; some is to accomodate future
: functionality. While I'd appreciate any ideas for optimization, what I'm
: really looking for here is a general "how-to" on making functions.
:
: Thanks for any help.
:
: -KF
:
: <%Dim dicRedirect
: Set dicRedirect=Server.CreateObject("Scripting.Dictionary")
: dicRedirect.Add "Experts","uwelns"
: dicRedirect.Add "UPhoto","uspns"
: dicRedirect.Add "Uweek","uwkns"
: dicRedirect.Add "Releases","uwnrns"
: ' The value element of the key/value pairs above is used to build the URL
:
: %>
:
: <%
: Dim RedirectStringOpen
: Dim counter
: Dim SecondCounter
: counter=0
: SecondCounter=0
: 'set variables "counter" and "SecondCounter" to zero
: Dim currentDictionaryCategory
: 'something to store the active value in the dictionary as we loop through
: For each whatever in dicRedirect
: ' for every item in the dictionary I just defined
:
: currentDictionaryCategory= Cstr(dicRedirect.item(whatever))
: 'set the currentDictionaryCategory to the value setting of the active
: key/value pair
: counter=counter+1
: 'Increment the counter by one. Counter is unused for now, may find
: application later
: if counter<>1 then
: if Cstr(currentDictionaryCategory) = Cstr(CurrentContentClass)
: then RedirectStringOpen = (RedirectStringOpen & "&" &
: dicRedirect.item(whatever)&"=o")
: ' If the current dictionary item is the same as the variable
: CurrentContentClass, then add the dictionary item name to the QueryString
: with a value of "o", e.g. uwkns=o
:
: if Cstr(currentDictionaryCategory) <> Cstr(CurrentContentClass)
: then RedirectStringOpen = (RedirectStringOpen & "&" &
: dicRedirect.item(whatever)&"=c")
: ' If the current dictionary item is not the same as the variable
: CurrentContentClass, then add the dictionary item name to the QueryString
: with a value of "c", e.g. uwkns=c
:
: end if
:
: if counter=1 then
: if Cstr(currentDictionaryCategory) = Cstr(CurrentContentClass)
: then RedirectStringOpen = (RedirectStringOpen &
: dicRedirect.item(whatever)&"=o")
: if Cstr(currentDictionaryCategory) <> Cstr(CurrentContentClass)
: then RedirectStringOpen = (RedirectStringOpen &
: dicRedirect.item(whatever)&"=c")
: end if
:
:
: If SecondCounter<>1 Then
: redirectStringClosed = (redirectStringClosed & "&" &
: dicRedirect.item(whatever)&"=c")
: else
: redirectStringClosed = (redirectStringClosed &
: dicRedirect.item(whatever)&"=c")
: end if
:
: Next
: %>
:
:
:
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top