Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Changing this code to a function? (how do you make functions?)

Reply
Thread Tools

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

 
 
Ken Fine
Guest
Posts: n/a
 
      11-30-2003
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.Diction ary")
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
%>



 
Reply With Quote
 
 
 
 
Ken Schaefer
Guest
Posts: n/a
 
      11-30-2003
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


"Ken Fine" <(E-Mail Removed)> wrote in message
news:bqbjo6$ehu$(E-Mail Removed)...
: 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.Diction ary")
: 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
: %>
:
:
:


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What life changing moments,do you wish you had photographed muchbetter and why ? Brian Digital Photography 14 01-08-2009 11:07 PM
What life changing moments,do you wish you had photographed muchbetter ? Colin Digital Photography 13 12-20-2008 06:53 PM
make pls don't work , i help you make web ,help you do business onthe internet xiaobb03@gmail.com Digital Photography 0 03-13-2008 11:28 AM
What do you use java for ? AndDoes Java make you feel happy? zelzel.zsu@gmail.com Java 15 11-12-2005 03:54 AM
OT: you couldn't make this up if you tried Vigo Breadcrumbs MCSE 31 04-27-2004 09:29 PM



Advertisments