Small Basic >
Curriculum > Online
> Lesson 2.4: Sound, Program, and Text Objects
In this lesson, you will learn how to:
When you create programs and work with various objects and operations that Small Basic offers, you can manipulate how your program runs if you use the
To better understand the Program object, let’s look at an example.
TextWindow.WriteLine("Program Directory: " + Program.Directory)
TextWindow.WriteLine("This program will run for 5 seconds")
This is the output you will see:
In this example, you use the Delay operation to set a fixed time delay for the program to run, and you cause the program to stop running by using the
You can control how a program runs by using the Program object in Small Basic.
To verify the output of your program, click Run on the toolbar, or press F5 on the keyboard. The text window displays the program directory and then ends after five seconds.
By using the Program object, you can also retrieve information about the arguments that are passed to your program.
Let’s look at an example to understand how you can use the
Program object to determine the count and value of arguments that are passed to a program as it runs.
If Program.ArgumentCount = 2 Then
TextWindow.WriteLine("First Number = " + Program.GetArgument(1) )
TextWindow.WriteLine("Second Number = " + Program.GetArgument(2) )
Multiplication=Program.GetArgument(1) * Program.GetArgument(2)
TextWindow.WriteLine("The product of the two number is: " + Multiplication)
TextWindow.WriteLine("PLease enter two numbers to be multiplied.")
You can also use operations in a program to determine information about the arguments that are passed to that program. For example, you can use the
ArgumentCount operation to find out how many command-line arguments are passed to the program. Similarly, you can use the
GetArgument operation that returns the specified argument. You must provide the index value of the required argument as a parameter to the operation.
After we work with arguments and use the Program object, let’s explore some other interesting aspects of Small Basic.
You can work with sounds in your program by using the Sound object with operations such as
Play, Pause, and Stop.
Did you know that you could include sounds in your Small Basic programs? In fact, you can choose from sample sounds provided in the Small Basic library.
filepath = Program.Directory+"\Windows XP Startup.wav"
The output of this example plays, pauses, and stops the specified sound file at regular intervals.
You can play a sound in your program by using the Play operation of the
Sound object. You specify the path of the local file or the URL of the file on the network as a parameter to the
Play operation. Similarly, you use the Pause operation with the path of the file as the parameter. You stop playing the sound by using the
In addition, observe the use of the Delay operation of the
Program object in the example. The Delay operation helps you delay the execution of commands.
You can use sound files in various formats (such as .mp3, .wav, or .wma) in your Small Basic programs.
Let’s look at an example to understand how you can play specific sounds (such as a bell ring, a chime, or a click) by using certain operations of the
Sound object in your Small Basic program.
filepath = "C:\Windows\Media\chimes.wav"
In this example, the PlayAndWait operation plays an audio file and then waits until it finishes playing.
This program will play the sound of a bell ringing, a chime, and a click, at regular intervals.
In the previous example, you saw how to play a sound in your program by specifying the path of a sound file. The example here explains how to include built-in sounds such as a ringing bell, a chime, and a click.
After an interval of one second, the program runs the operation PlayBellRing and plays the bell sound. After another second, the
PlayChime operation runs, and the chime sound plays. After one more second, the
PlayClick operation runs and plays the click sound.
The PlayAndWait operation plays an audio file and waits until it finishes playing. If the particular file is paused, the operation resumes from the position where the audio playback was paused.
Small Basic also offers useful operations that you can use to work with text.
For example, you may want to convert all names to uppercase letters, or you might want to search for specific information within some text.
You can use the Text object and its various functions and methods to perform operations on text in Small Basic.
For example, you can determine the length of a text string by using the GetLength operation.
Let’s learn about more operations of the Text object by reviewing an example...
TextWindow.Write("Enter you first name: ")
FirstName = TextWindow.Read()
TextWindow.Write("Enter your last name: ")
LastName = TextWindow.Read()
CompleteName = Text.Append(FirstName, "" + LastName)
NameInUpperCase = Text.ConvertToUpperCase(CompleteName)
TextWindow.WriteLine("Hello " + NameInUpperCase + "!")
When you work in Small Basic, you can perform operations on text by typing a statement that contains the
In this example:
To verify the output of your program, click Run on the toolbar, or press F5 on the keyboard.
Let’s look at another example to understand more operations of the
TextWindow.Write("Enter a valid e-mail address: ")
EmailID = TextWindow.Read()
m = Text.IsSubText(EmailID, ".")
n = Text.IsSubText(EmailID, "@")
If m = "True" And n = "True" Then
TextWindow.WriteLine("You have entered a valid e-mail address.")
TextWindow.WriteLine("Please enter a valid e-mail address.")
In this example, you ask the user to specify an e-mail address. Then you use the
IsSubText operation of the Text object to determine whether the address is valid.
You can use operations of the Text object to verify whether specific text or a character is a subset of the larger text. To demonstrate this technique, we use the
Write and Read operations of the TextWindow object to obtain an e-mail address from the user. Then we use the
IsSubText operation of the Text object to determine whether the user has specified a valid address.
Another operation of the Text object is the GetSubText operation. This operation takes three parameters¾the text from which you want to derive the subtext, the location from where you want to derive the subtext, and the
length up to which you want to derive the subtext.
Here are some more uses of the Text object…
To determine whether given text starts with specified subtext, you can use the
StartWith or EndWith operations.
To get the character code for a specific Unicode character, you can use the
To determine the index position of specific subtext, you can use the GetIndexOf operation.
Now you know how to:
Write a program that displays a text window and then performs the following steps:
To see the answers to these questions, go to the
Answer Key page.