473,699 Members | 2,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
("CurrentConten tClass") 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=Ser ver.CreateObjec t("Scripting.Di ctionary")
dicRedirect.Add "Experts","uwel ns"
dicRedirect.Add "UPhoto","uspns "
dicRedirect.Add "Uweek","uw kns"
dicRedirect.Add "Releases","uwn rns"
' The value element of the key/value pairs above is used to build the URL

%>

<%
Dim RedirectStringO pen
Dim counter
Dim SecondCounter
counter=0
SecondCounter=0
'set variables "counter" and "SecondCoun ter" to zero
Dim currentDictiona ryCategory
'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

currentDictiona ryCategory= Cstr(dicRedirec t.item(whatever ))
'set the currentDictiona ryCategory 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(currentDic tionaryCategory ) = Cstr(CurrentCon tentClass)
then RedirectStringO pen = (RedirectString Open & "&" &
dicRedirect.ite m(whatever)&"=o ")
' If the current dictionary item is the same as the variable
CurrentContentC lass, then add the dictionary item name to the QueryString
with a value of "o", e.g. uwkns=o

if Cstr(currentDic tionaryCategory ) <> Cstr(CurrentCon tentClass)
then RedirectStringO pen = (RedirectString Open & "&" &
dicRedirect.ite m(whatever)&"=c ")
' If the current dictionary item is not the same as the variable
CurrentContentC lass, 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(currentDic tionaryCategory ) = Cstr(CurrentCon tentClass)
then RedirectStringO pen = (RedirectString Open &
dicRedirect.ite m(whatever)&"=o ")
if Cstr(currentDic tionaryCategory ) <> Cstr(CurrentCon tentClass)
then RedirectStringO pen = (RedirectString Open &
dicRedirect.ite m(whatever)&"=c ")
end if
If SecondCounter<> 1 Then
redirectStringC losed = (redirectString Closed & "&" &
dicRedirect.ite m(whatever)&"=c ")
else
redirectStringC losed = (redirectString Closed &
dicRedirect.ite m(whatever)&"=c ")
end if

Next
%>

Jul 19 '05 #1
1 2109
Functions in VBScript are simple (relatively!).

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

Dim anInternalVaria ble

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(objR S, True, True)

End Function

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

If blnClose then
objToDispose.Cl ose
End If

If blnNothing then
Set objToDispose = Nothing
End If

End Sub

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

HTH

Cheers
Ken
"Ken Fine" <ke*****@u.wash ington.edu> wrote in message
news:bq******** **@nntp6.u.wash ington.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
: ("CurrentConten tClass") 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=Ser ver.CreateObjec t("Scripting.Di ctionary")
: dicRedirect.Add "Experts","uwel ns"
: dicRedirect.Add "UPhoto","uspns "
: dicRedirect.Add "Uweek","uw kns"
: dicRedirect.Add "Releases","uwn rns"
: ' The value element of the key/value pairs above is used to build the URL
:
: %>
:
: <%
: Dim RedirectStringO pen
: Dim counter
: Dim SecondCounter
: counter=0
: SecondCounter=0
: 'set variables "counter" and "SecondCoun ter" to zero
: Dim currentDictiona ryCategory
: '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
:
: currentDictiona ryCategory= Cstr(dicRedirec t.item(whatever ))
: 'set the currentDictiona ryCategory 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(currentDic tionaryCategory ) = Cstr(CurrentCon tentClass)
: then RedirectStringO pen = (RedirectString Open & "&" &
: dicRedirect.ite m(whatever)&"=o ")
: ' If the current dictionary item is the same as the variable
: CurrentContentC lass, then add the dictionary item name to the QueryString
: with a value of "o", e.g. uwkns=o
:
: if Cstr(currentDic tionaryCategory ) <> Cstr(CurrentCon tentClass)
: then RedirectStringO pen = (RedirectString Open & "&" &
: dicRedirect.ite m(whatever)&"=c ")
: ' If the current dictionary item is not the same as the variable
: CurrentContentC lass, 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(currentDic tionaryCategory ) = Cstr(CurrentCon tentClass)
: then RedirectStringO pen = (RedirectString Open &
: dicRedirect.ite m(whatever)&"=o ")
: if Cstr(currentDic tionaryCategory ) <> Cstr(CurrentCon tentClass)
: then RedirectStringO pen = (RedirectString Open &
: dicRedirect.ite m(whatever)&"=c ")
: end if
:
:
: If SecondCounter<> 1 Then
: redirectStringC losed = (redirectString Closed & "&" &
: dicRedirect.ite m(whatever)&"=c ")
: else
: redirectStringC losed = (redirectString Closed &
: dicRedirect.ite m(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
7222
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 function that is called from a button. I think I can use a <div id="t"></div> for this ? Something like
45
3601
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 themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
2
1783
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 plug-in DLLs so that I could early-bind to the plug-ins by declaring objects of the interface class type. This worked fine, until one day I found that I needed to add a new method to the interface class. Of course, everything broke immediately...
13
5157
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 description would be as clear as an example. ----------- class ParentA {};
13
2839
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
2018
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 of the group that will receive the e-mail. I pass this.form to a function to validate the other data before handing it off to a script to actually do the mailing. The mailer script looks at all of the form's fields and sends their names and...
16
2605
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 expects a boolean return value, which it gets, but the value changes! I walked throuhg the code, and the unmanged code is returning false, but when I look at the variable in the managed code it's return value it true. Even after I initialized the...
17
2387
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
3371
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 that has to deal with two different locales. Unfortunately, the code uses plain C functions for IO at many places. However, there is a single point that uses std::isprint with a C++ locale that is different from the standard locale. That's why I'm...
0
8623
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9185
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8935
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8893
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4389
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.