ARTICLE 8--DISSECTING STRINGS AND THE DATE OBJECT

Programming Using NSBasicCE

ARTICLE 8--DISSECTING STRINGS AND THE DATE OBJECT

All content (c)1999, 2002 Serg Koren.

All rights eserved.

Welcome to the eight installment of NSBasicCE.

In this article follow up on the last article and talk about how to break long strings into shorter strings, and we look at a new object in NSBasic, the Date Object.

Leftovers from Last Time

Last time we talked about concatenating strings and the ComboBox. I asked you to write a program that has:
  1. Five checkboxes.Each labeled with a number “1”, “2”, “3”,”4”, “5
  2. A ComboBox
  3. A CommandButton labeled “+”
  4. A CommandButton labeled “-“
  5. A CommandButton labeled “Total”
  6. A CommandButton labeled “Clear”
The program should:
  1. Each time the user checks a number checkbox put the number the user tapped at the end of the ComboBox list.
  2. If the user taps the + or –command button, put a “+” or a “-“ at the end of the ComboBox list.
  3. If the user taps the “Total” command button, display the result of the items in the ComboBox using the message:   “There are x items in the ComboBox.”
  4. If the user selects an item from the ComboBox, display the message: “The user chose: x.”(Note the period after the ‘x’.)
  5. If the user taps on the “Clear” command button, remove all entries from the ComboBox, and deselect (uncheck) all the checkboxes.
This seems complex, but you should take it a piece at a time and build it up. A good strategy would be to take steps 1-5 in the "has" section and get them working one at a time. Then work on the "should" section. Any such "break it up" strategy would work. Here's my version of the program for your reference:
'----------code starts here
'
' Article 7 assignment
'
'----------Routines to create the UI
' set up our 5 checkboxes
SUB CreateCheckbox1
ADDOBJECT "CheckBox","Ch1",10,10,30,20
Ch1.Caption = "1"
END SUB
SUB CreateCheckbox1
ADDOBJECT "CheckBox","Ch2",10,30,30,20
Ch2.Caption = "2"
END SUB
SUB CreateCheckbox2
ADDOBJECT "CheckBox","Ch3",10,50,30,20
Ch3.Caption = "3"
END SUB
SUB CreateCheckbox3
ADDOBJECT "CheckBox","Ch4",10,70,30,20
Ch4.Caption = "4"
END SUB
SUB CreateCheckbox4
ADDOBJECT "CheckBox","Ch5",10,90,30,20
Ch5.Caption = "5"
END SUB

'----------setup our combo box
SUB CreateComboBox
ADDOBJECT "ComboBox","Cmb",10,110,60,80
END SUB

'----------set up our command buttons
SUB CreatePlusButton 
ADDOBJECT "CommandButton","Plus",10,140,20,20
Plus.Caption = "+"
END SUB
SUB CreateMinusButton 
ADDOBJECT "CommandButton","Minus",10,170,20,20
Minus.Caption = "-"
END SUB
SUB CreateClearButton 
ADDOBJECT "CommandButton","Clear",10,200,50,20
Clear.Caption = "Clear"
END SUB
SUB CreateTotalButton 
ADDOBJECT "CommandButton","Total",10,230,50,20
Total.Caption = "Total"
END SUB
 
'----------event handler for checkboxes...
' just put the number (which is the caption) into combobox
SUB Ch1_Click
Cmb.AddItem Ch1.Caption ' just move the caption of this checkbox into the last slot in the Combobox
END SUB 
SUB Ch2_Click
Cmb.AddItem Ch2.Caption
END SUB 
SUB Ch3_Click
Cmb.AddItem Ch3.Caption
END SUB 
SUB Ch4_Click
Cmb.AddItem Ch4.Caption
END SUB 
SUB Ch5_Click
Cmb.AddItem Ch5.Caption
END SUB 

'----------event handlers for buttons
SUB Plus_Click
Cmb,AddItem Plus.Caption ' just move the caption of this button into the last slot of the Combobox
END SUB
SUB Minus_Click
Cmb,AddItem Minus.Caption 
END SUB
SUB Clear_Click
Cmb.Clear ' just clear all the items in the ComboBox out
END SUB
SUB Total_Click
MSGBOX "There are " & Cmb.ListCount & " items in the Combobox." ' see explanation below
END SUB
SUB Cmb_Change ' our ComboBox event handler...see explanation below
MSGBOX "You chose " & cmb.ListIndex & "."        ' note the period at the end
END SUB

'----------main routines
' set up our user interface widgets
SUB SetupUI
CreateCheckbox1
CreateCheckbox2
CreateCheckbox3
CreateCheckbox4
CreateCheckbox5
CreateComboBox
CreatePlustButton
CreateMinusButton
CreateClearButton
CreateTotalButton
END SUB
 
' our main routine that runs everything
SUB Main
SetupUI ' create the user interface
END SUB
Main ' our only IMMEDIATE command
'----------code ends here
 
That's it. It looks like a lot, but it's not bad! We'll show you how to make it simpler and shorter next time. This is also known as "brute force" code. You force everything to happen one by one. There are more efficient ways of doing the above! Now then, everything should make sense apart from maybe the event handlers. Most of the event handlers just copy the caption of the widget clicked into the ComboBox:

cmb.AddItem <widget name>.caption

This is a shortcut for something you probably did (if you're just starting out):

 
DIM X
X = <widget name>.caption
cmb.AddItem X

would do the same thing. But my version takes less typing and once you get used to it, is easier to understand. This is a programmatic "idiom" (also known as a "pattern".) The Total button event handler just displays a MSGBOX:

MSGBOX "There are " & Cmb.ListCount & " items in the Combobox."

Again, you may have done it by DIMensioning individual strings and then concatenating them (as described in the last article) and finally passing the concatenated string to MSGBOX. That's fine too! The tricky bit is the "Cmb.ListCount" which returns he number of items currently in the ComboBox. This is a number that gets coerced into a string which is then concatenated into the rest of it. Nothing too hard.

The ComboBox event handler uses the same technique to display the item number of the item in the list that the user chose: cmb.ListIndex

Note that this doesn't show the value of the item chosen, just the ordinal location in the list of the item chosen. We'll talk about how to get the actual number chosen next time!

If you don't realize it, the above sample can be used as a basis for a simple calculator. We'll come back to this example from time to time as we learn more!

Onward to dissecting strings!

Dissecting Strings

We described how to set up strings via a DIM statement and how to assign them to variable and how to concatenate strings using & into longer strings last time. This time we talk about how to chop a string into pieces!

Let's say you have a string that someone typed in. A first name, a space, and a last name, and you want to make your program friendlier and just use the person's first name when you display a message:

Serg<space>Koren

and you want to do a MSGBOX FirstName

You could have them enter just their first name in a field, and have a separate field for the last name. But sometimes this is too much work for the user. So lets say we get the full string somehow.

DIM FirstLast
FirstLast = "Serg Koren" 
We have a string! We know how to add stuff to it such as

Prompt = FirstLast & " how are you?"

But how do you just get the first name? Or just the last name? Or maybe the space in the middle? It's not very hard, since NSBasic provides a rich set of Functions that let you do just that. Let's start simply and assume you know the length of the substring you want to extract, and that the substring starts at the very left of our parent string. Just use the LEFT function. LEFT takes two parameters, the string you want to chop up, and the number of characters from the left you want to take. What you get is a substring with those restrictions. So if we know the first name is 4 characters long:

DIM First, FirstLast,FirstLength
FirstLength = 4
FirstLast = "Serg Koren"
First = LEFT(FirstLast,FirstLength)
MSGBOX First & ", how are you?"

Again, remember that a function returns a result and uses parentheses around its parameters. That's not hard is it? Now what about the last name? How do we get the rightmost part of a string if we know the length of the substring? Just use the RIGHT function the same way! Assuming the above snippet of code exists:

DIM Last,LastLength
LastLength = 5
Last = RIGHT(FirstLast,LastLength)
MSGBOX "The last name is: " & Last

You could even put it back together!

DIM NewFirstLast
NewFirstLast = First & " " & Last
MSGBOX NewFirstLast

Important note: RIGHT, LEFT, and the other string manipulation functions we will discuss don't destroy the original string! They just make copies of parts of them!

MSGBOX FirstLast

would still show "Serg Koren", it hasn't been destroyed by LEFT and RIGHT! Now the last function MID which returns a substring from the MIDdle. MID again takes the string to chop up, but its next parameter is the starting character (a number) and then an optional length parameter to extract. For example, if we wanted to extract the space from the name we could do:

DIM Space, StartPos, Length
StartPos = 5 ' start at the 5th character of FirstLast (the space)
Length = 1 ' extract only 1 character (just the space)
Space = MID(FirstLast,StartPos,Length) ' get the space
MSGBOX Space ' display the space

Not very exciting, but it works. How do we know? Well we can figure out how many characters "Space" has. We can get the lenght of a string (the number of characters in it) by using the LENgth function:

DIM SpaceLeng
SpaceLen = LEN(Space)
MSGBOX "Space has " & SpaceLen & "      characters in it."
MSGBOX "FirstLast has " & LEN(FirstLast)      & " characters in it."

Try the above samples of code on your PocketPC, changing the lengths or the starting position. I mentioned the third parameter in MID is optional. What happens if you leave it out in the above code? You should also read the manual pages on LEFT, RIGHT, MID, and LEN. You'll be using these a lot! Ignore LEFTB, RIGHTB, LENB, MIDB for the time being. We'll go over these in a future article.

The last string function I want to talk about is REPLACE. REPLACE replaces parts of a string with another string. It's like an editor's FIND/REPLACE function. For example, if we want to change all of the "e"s in my name to "x"s we could use REPLACE. If you check the manual, you see it has a bunch of parameters, but you'll only have to worry about the first three most of the time.

REPLACE(target,find,source)

'Target' is our main string we want to modify (FirstLast in my example), 'find' is the string we want to replace (e in my example) and 'source' is the character we want to replace find with (x).

FirstLast = REPLACE(FirstLast,"e","x")
MSGBOX FirstLast
You can also do things like
FirstLast = REPLACE(FirstLast,"er","eeeeeeeer")

There is no rule that says what you replace and what you replace it with has to be one character, or the lengths have to match, but you should know what you're doing.

That's it for strings; or at least the things you'll deal with most.

Date Object

Date Objects are a new feature introduced in the latest NSBasic release. It displays a calendar widget and lets the user select a date from the calendar. Normally, it displays a date in a field. When the user taps on the date, it displays the calendar. It's useful for getting a date from the user without him having to type a date in or having to pull up another type of calendar. Creating it is easy (you should have this down in your sleep).

ADDOBJECT "Date","MyDateObject",10,10,100,20

You should note that the width and height only apply to the date field itself. The calendar isn't resizable. The new properties are: formatLong, Max, and Min formatLong has a bug in it, so ignore it until it's fixed.

Max and Min allow you to specify a range to limit your calendar to a date range. This is useful if say you want to limit the calendar selection to the current fiscal quarter, or this:

MyDateObject.Min = "07/01/1999"
MyDateObject.Max = "12/01/1999"

There are no new Methods to discuss for the Date Object, and the only new Event we can handle is something called "dropDown" which gets triggered (calls our "MyDateObject_dropDown" handler) when the user taps the the field to display the calendar.

For Next Time

This is a simple exercize to give you a break from the last one. Write an app that has:

  1. a date object
  2. a command button

It should:

  1. save the date the user taps on in a variable called TheDate
  2. Display the message: "This is the i-th day of the j-th month." whenever the user taps the command button.

Note that i and j above are numbers you can get from TheDate.

Next time I'll tackle a powerful and simplifying concept known as arrays as well as the INPUTBOX object! See you then...

If you get stuck or have a question about anything we've gone over, feel free to e-mail me at Serg@VisualNewt.com
Cheers!
nsblogo2

©2007 Serg Koren & VisualNewt Software.  All rights reserved.