Programming Using NSBasicCE
ARTICLE 9 -- ARRAYED THINGS AND INPUTBOX
All content (c)1999 Serg Koren.
All rights reserved.
Welcome to the ninth installment of NSBasicCE.
Here we discuss arrays, and the INPUTBOX widget.
Leftovers from Last Time
In our last article we discussed how to take a string and extract substrings from it. I then asked you to write a simple app that has:
- a date object
- a command button
It should:
- Save the date the user taps on in a variable called TheDate
- 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.
Here is my solution:
'--------begin code here-------------- ' ' Aricle 8 exercize ' ' '---------- Create our objects routines OPTION EXPLICIT ' force DIMensions DIM FullDate ' global variable to store the date string returned by Date object DIM Day ' the part of the date string containing the Day (i-th day) DIM Month ' the part of the date string containing the Month (j-th month)
' Create a date object SUB CreateMyDateObject ADDOBJECT "Date","MyDateObject",10,10,100,30 END SUB
' Create our command button SUB CreateMyCommandButton ADDOBJECT "CommandButton","MyCommandButton",10,50,100,30 END SUB
'----------- Set up our event handlers ' Date object event handler SUB MyDateObject_Change FullDate = MyDateObject.Text ' get the selected date and store it in our variable END SUB
' CommandButton event handler SUB MyCommandButton_click Day = MID(FullDate,4,2) ' extract the dd portion of mm/dd/yyyy Month = LEFT(FullDate,2) ' extract the mm portion of mm/dd/yyyy MSGBOX "This is the " & Day & "th day of the " & Month & "th month." END SUB
'------------- Our setup routines SUB SetupUI CreateMyDateObject CreateMyCommandButton END SUB
SUB Main SetupUI END SUB
Main ' our IMMEDIATE command to get things running '--------- end code here
That's basically it. However, you'll notice that if you choose the 1st day or month, your message is grammatically incorrect! You don't know enough at this point to fix it, but you will next time!
ARRAYS
Simply put, arrays are collections of things. In programming, arrays are a powerful tool that lets you simplify your code by grouping collections of variables together under one name. An example would be the best way to understand this. For instance, assume you have to program a game having ten boxes and you need to put something into the third. You could set up variables for each box thusly:
DIM Box1 DIM Box2 DIM Box3 ... DIM Box10 Box3 = Something
This would work fine, but it's a lot of typing isn't it? Wouldn't it be simpler saying "here is a bunch of boxes, and I want the 3rd one."? In NSBasic you can do this by creating an array of variables and giving them a single name. In this example lets call our collection of boxes "Box". We specify the maximum number of things (boxes) in our collection by putting the number inside parentheses after the name in our DIMension statement! So if we want to have 10 boxes we would have a DIM statement:
DIM Box(10)
Now if you're just getting into programming and have been following these articles you may be thinking..."doesn't the parentheses mean that this is a function?" Well not in this case. You don't have the FUNCTION keyword. But actually in some languages (Smalltalk, etc.) This indeed would be a function-like object. But in NSBasic, this is just how you specify an array of variables. Ok, so back to our example...how do we put stuff into our third box?
Box(3) = Something
Easy! So how do we see what is in the third box instead? Just as you would expect...
X = Box(3)
And likewise you can do things like...
Box(3) = Box(3) * 2
...which would multiply the value in array element 3 by 2. No, the number isn't 6. It depends on what is stored in Box(3). If we did:
Box(3) = 12 Box(3) = Box(3) * 2
...then Box(3) = 24! Can we do...
Box = Box(3) + 2
...no! You can't do something to the entire array. You have to specify which element in the array you want to operate on (again this is a limitation of NSBasic, since other languages allow you to operate on the array as a whole).
So far we've been assuming arrays hold numbers, but arrays can hold any data type!
Box(4) = "Hello" Box(5) = "There" Box(6) = 12 * 2
MSGBOX Box(4) & Box(5) & " array element 6 has " & Box(6) & " in it."
You can't however put objects into arrays!
Box(7) = MyDateObject ' a Date object you create elsewhere
will give you a runtime error (an error that you get when you run the program) of "Object doesn't support this property or method." However, there are ways of creating arrays of objects (which we'll talk about in a future article.)
A couple of important things to note about arrays. The first element in the array is actually element 0! That means that in our box example our boxes are numbered 0 through 10, an d not 1 through 10! So what happens when we try:
Box(11) = 1
Try it!
Can we get the 3rd through the 5th box in our array? No, again you need to deal with each element in an array singly.
Arrays are very straightforward once you get past knowing how to DIM and reference (use) them. However, we've only been discussing one-dimensional arrays. That is arrays of things we can line up in a single line. But lets say you need to program a Bingo game board for your Auntie Ethel who is addicted to bingo! A bingo board consists of a two-dimensional array (a grid) Or a spreadsheet? In this case we use a two-dimensional array set up by a DIM statement that specifies the maximum number of elements in each axis (vertically and horizontally). So if we are building a mini-spreadsheet that has 10 rows and 30 columns we would use the DIMension statement:
DIM Sheet(10,30)
or
DIM Sheet(30,10)
Huh??? Well programming languages don't know anything about the concepts of "vertical" and "horizontal", so the order in which you declare them in the DIM statement is totally arbitrary. By convention, however we tend to think of row-column instead of column-row, so the first is more accepted. However, you can use the second and it will work fine, but you personally as the programmer have to remember which is the row and which is the column. So it's a good idea to put in a comment to remind you...
DIM Sheet (30,10) ' column, row
or better yet....
DIM MaxRow
DIM MaxColumn
MaxRow = 10
MaxColumn = 30
DIM Sheet (MaxColumn,MaxRow)
We explained before why this version is better programming style, although the single DIM statement is faster to type.
Also, note that in the above example we didn't DIM Sheet(29,9) (because of the 0th element in each dimension). This wastes a bit of space, but usually isn't critical, and it simplifies our programs so that we don't have to subtract one each time. You may be scratching your head here again...say we want to load cell 5,3 with a 2 if we had DIMmed the array as
DIM Sheet(29,9) ' because our sheet starts at Sheet(0,0) and not Sheet(1,1) Sheet(4,2) = 2 ' would actually be cell (5,3) because our array starts at Sheet(0,0) and goes to (29,9)
but because we dimensioned
DIM Sheet(30,10) ' and not use cell (0,0) for anything Sheet(5,3) = 2 ' is easier to deal with although it's actually (6,4), but we just ignore the (0,0) element to simplify our lives
Read that through a couple of times so you understand it. Draw it out on some graph paper too if you need to. It's not hard to understand, just a bit clumsy to explain.
That's basically it for arrays. Oh, how many dimensions can an array in NSBasic have? We've only discussed two. NSBasic arrays can have up to 60! dimensions. So you could have a DIM statement:
DIM Wow(20,340,1,40,...bunch more...,3) ' for 60 dimensions separated by commas
Don't ask why you would ever want to deal with this...
INPUTBOX
So far we've been dealing with programs that don't let the user type something in. The INPUTBOX is a widget that lets you do just that! It's similar to MSGBOX in that it displays a separate mini-window and display a message. It's different in that the window it displays also has a single text field that lets the user type something in. Refer to the manual page on INPUTBOX.
INPUTBOX (prompt[,title[,default[,xpox,ypos]]])
As you can see INPUTBOX is a function and it returns whatever string the user typed in. As you can also see from the manual, the only required parameter is the prompt. The prompt is the message to display to the user. The INPUTBOX is very easy to use. You don't have to create it or set up event handlers, you just use it.
A = INPUTBOX ("Type something please","My Input Box Title","Gee I can preload an answer",10,10)
Try that. The "default" parameter (the 3rd one), lets you set a default string in the input field to save the user some typing.
B = INPUTBOX ("What is your name?","My Name Input","enter your name here",10,10)
or it can be used to tell your user where to input stuff. (Yes I've seen this done, and I assume its for the computer-challenged among us, but I think this is bad design. Just leave it out!)
There is one really important thing you'll soon discover if you try to use INPUTBOX on a CasioE-105 and other PPC handhelds. INPUTBOX doesn't scale properly to the size of the screen. It doesn't fit! This is a Microsoft bug! To get around this problem, you can use the replacement INPUBOX routine I've written in NSBasic, and can be found at http://www.VisualNewt.com/CE/NSB/ Make sure you read the accompanying documentation, since it works a little differently from the normal INPUTBOX.
For Next Time
Write a simple program that uses arrays. The program should have:
- 3 CommandButtons labeled "A","B", and "C"l
- A CommandButton labeled "Do it!"
The program should:
- Tally the numbers of times each of the three CommandButtons has been tapped. (Hint: This is what the array is for).
- Prompt the user for his name when he taps the "Do it!" button, and then display a message in the format:
"Hi, you've tapped button A x times, B y times, and button C z times."
That's it for this week. Next week we'll get into a very powerful programming concept. We'll discuss how your programs can make decisions and repeat things over and over and over and over and... And our widgets of the week will be the Label, and the ListBox. We're doing two because there's not a lot to the label. 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!

