473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
%>

Jul 19 '05 #1
1 2090
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" <ke*****@u.washington.edu> wrote in message
news:bq**********@nntp6.u.washington.edu...
: 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
: %>
:
:
:
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

55
by: Ton den Hartog | last post by:
Stupid basic question but I find it horribly imposible to find the answer elsewhere... :-( I want to have a piece of text in my HTML page and want to be able to change it in a Javascript...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
2
by: Oenone | last post by:
I could use a little advice to help prevent me making a possible mess of a project. :) In VB6, I once created a project that exposed a public interface class. I then Implemented this in various...
13
by: ctor | last post by:
Hi, I'm experiencing an annoying issue. Here is a simplified idea of what I am trying to do. Inclusion guards aren't shown for readability. I hope this isn't too confusing; I don't think a...
13
by: dragoncoder | last post by:
Consider the following code #include <iostream> class Base { public: virtual void say() { std::cout << "Base" << std::endl; } }; class Derived: public base {
2
by: Gary Dale | last post by:
I have a form with a pull-down list with six options, each of which has a value set. The value is the e-mail account name (without the domain) of a group while the displayed value is the full name...
16
by: DaTurk | last post by:
Hi, I have a c# application that needs to access c++ libraries, so it does this by using a managed layer of c++ CLI. Anyway, in the CLI function call, that calls the unmanaged function it...
17
by: blufox | last post by:
Hi All, Can i change the execution path of methods in my process at runtime? e.g a()->b()->c()->d()->e() Now, i want execution to be altered at runtime as -
4
by: dertopper | last post by:
Hello newsgroup, is it possible to create a std::locale object without changing the global C locale? Is this implementation defined? My problem is that I work in a multi-threaded environment...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.