DesktopX: Scripting Arrays Constants Functions

From WinCustomize Wiki

Jump to: navigation, search
Scripting Arrays Constants Functions
Original Author:sViz
Date Created:September 30, 2007
Application:DesktopX
Programs Used:DesktopX


Contents

[edit] DesktopX: Scripting Arrays, Constants, Functions

As we move on in this series, I want to introduce some more vbscript functions that we’ll be using: arrays, constants, and passing/returning values with functions.

[edit] Arrays

An array is a dynamic way of storing information. It is made up of variables. Their size is determined by how many variables are listed in the array, starting from 0. Let’s have a look.

Fixed Array

Dim Names(2)

Name(0) = “Jo”
Name(1) = “John”
Name(2) = “Jack”

Sub Object_OnScriptEnter
	Msgbox Name(1)
End Sub

A fixed array declares the name of the array and the size all at once. In this particular code, there are three variables in the array (starting from 0). I use fixed arrays when I know how many variables I need to store information; especially when there are only a few.

Dynamic Array

Dim Items()

Sub Object_OnScriptEnter
	For x = 0 to 5
		Redim Preserve Items(x)
		Items(x) = “Item ” & x
	Next
	Msgbox Items(3)
End Sub

A dynamic array is declared without the size. The parenthesis is left empty. That way you can later change the size as many times as you like with ‘Redim Preserve’. Redim Preserve will re-declare the array with a new size and preserve the information already stored. Without ‘preserve’, all your information is lost. I use dynamic arrays a lot, as they are the most useful for storing lots of info.

LBound UBound

When dealing with arrays lbound and ubound come in very handy. They are shortcuts, if you will, to retriving the first varible (lbound) and last variable (ubound) in your array.

Dim Items()

Sub Object_OnScriptEnter
	For x = 0 to 5
		Redim Preserve Items(x)
		Items(x) = “Item ” & x
	Next
	Msgbox Ubound(Items)
End Sub


[edit] Constants

Constants are fixed variables which are declared and defined in one line. They cannot be redefined later. They are used to replace numbers or strings with something more recognizable to you. Lets see how they make this code more understandable.

Without constants:

'Called when the script is executed
Sub Object_OnScriptEnter
	'Set 1-second timer
	object.SetTimer 1,1000
End Sub

Sub Object_OnTimer1
	curTime = FormatDateTime(Now(),1)
	Object.text = curTime
End Sub

With constants:

Const DateTime = 0, FullDate = 1, JustTime = 3

'Called when the script is executed
Sub Object_OnScriptEnter
	object.SetTimer 1,1000
End Sub

Sub Object_OnTimer1
	curTime = FormatDateTime(Now(),FullDate)
	Object.text = curTime
End Sub

In the first code, you’d have to remember what format goes with what number. In the second code, with constants, you can easily see what format is in use. It might seem like a small adjustment but in certain cases constants might provide some clarity; bringing things back to plain English.

[edit] Pass and Return values with Functions

Functions unlike Subs can return and pass values. By that, I mean you can take some information in one procedure pass it onto a function where it is analyzed and then return the result. Let’s see it in action. Apply the script below to an object.

'Called when the script is executed
Sub Object_OnScriptEnter
	sampleTxt = "My very educated mother just served us nine pizza-pies."
	wordCount = myFunction(sampleTxt)
	msgbox wordCount
End Sub

Function myFunction(info)
	spaceStr = 1
	wordStr = 1
	wCount = 0		
	While iend < len(info)
			x = Instr(spaceStr, info , " ")
			If x = 0 Then 
				y = Mid(info, wordStr, Len(info))
			Else
				y = Mid(info, wordStr, x-wordStr)
			End If
			spaceStr = x+1 
			wordStr = x+1
			iend = Instr(info, y)+ Len(y)
			If y <> "" Then wCount = wCount + 1
	Wend
	myFunction = wCount
End Function

Now this little script will count how many words are in a given string. Don’t mind the mumbo jumbo in the function if you don’t understand it yet. There are a few key parts I want to point out below:

Image:Dx scriptFunction2.png

To pass information to a function you would call the function with the information in the parenthesis. Call myFunction(“This is some information”) As it is in the code above, I’ve passed along the variable sampleTxt, which is the sample sentence in which we want to count how many words there are.

In the function itself, the passed information is represented as ‘info’. So in the function, wherever it says ‘info’ we are still dealing with that sample sentence from the beginning.

When we finally have the result of our process to return, we write the name of the function and the result myFunction = wCount

So when we write wordCount = myFunction(sampleTxt) in OnScriptEnter, we calling the procedure, passing the information, and assigning the result of the function to the variable.

Passing and returning values this way makes a function extremely reusable. You might have several different pieces of information that need to be processed in the same way. Passing them each through the one procedure allows for maximum functionality.


This concludes today's lesson.

Next Tutorial: DesktopX: Scripting Groups (Beginner)

[edit] Tips

None right now.

[edit] Further Reading