6 |
The LA Fox Developer Newsletter
|
February 1996
|
Simple Stuff
by Ariela Pomerance
As CIXen know, a week or so after each magazine arrives we post a message in FoxUserGroup asking for your opinions on the articles. One reason for this is to help us get an idea of what members, in general, want
-
as opposed to what your Council may think they want.
In recent issues our emphasis has been on Visual FoxPro. It’s new, it’s very different, lots to learn and new problems to solve and work round and Members using it look to us for help. However the feedback from CIX after our last issue also pointed firmly to a need for more on general, pre-VFP basics. As one member put it
" .
SO
simple that a total novice could actually do
it,
and understand the code”
Clearly some regular “simple stuff’ pages in Fox User would be be very welcome, and there
is
a list of subjects produced by that discussion at the end of this article. Contributions are invited, and will be paid for at the usual rate. The list included some questions that I’m often asked
-
so I’ll start off by trying to answer one
“Why and when do you go for a character, numeric or date field? and most importantly the reasons, in English, no jargon allowed.”
A field (or variable) will only contain numbers. Should its data type be numeric or character?
The answer depends on what you plan to do with the numbers. If they are mainly to be used in calculations
-
for example a unit price
-
then the type should be Numeric
-
Why? because you can only calculate with numeric data. Where no calculation is likely
-
for example a telephone number
-
then the type should be Character; treating the digits as text. Why? Using character type fields is simpler for combining and displaying data or if the field is to be part of a compound index
-
i.e. an index which is made up of more than one field.
Of course Fox can be made to calculate using digits stored as words (character), not numbers (numeric). The function VALO turns a character string of digits into a something that looks identical but is now a numeric and can be calculated with. Try
-
x='12345' && x is a word
12345
?x/10
and you get an error message
|
data type mismatch
You
need-:
? val(x)
/
10
and you’ll get the expected
-
1234.50.
If you want to display numeric data combined with other data types using ‘+‘s
,
the numbers must be converted to character strings by using the function STRO For example-:
Item
•
‘Christmas
Puddings’ && character
data Unit_Price=
2.22 &&
numeric
data
? ‘the
cost of 10
'=
Item
+
'is
£‘
+Unit_Price*10
and you get an error message
operator/operand type mismatch
meaning, that you tried to do something mathematical
(addition) to a character string. You need
-:
?
‘the
cost
of 10 '+item
+‘
Is £‘+str(Unlt_PricelO,5,2)
and you get
the cost of 10 Christmas Puddings is £22.20
The STR(the_number,5,2) formats the converted numbers to a 5 character string with 2 decimal places.
However
?
‘the cost
of 10’, Item, 'is £
,UnltPrlce*1O
(i.e fields separated with commas, not ‘+‘s) is also legal and accepted
-
the result just looks untidy.
When it comes to dates, there is rarely a good reason not to use the Date type. Even if a date will not be used in a date calculation, using the Date type means that the field being filled in is automatically formatted as nn/nn/nW (or /nnnn, depending on how SET CENTURY is set) and that Fox will automatically check that the digits entered do make up a valid date. And Fox has an excellent selection of functions for working with dates, as long as the dates are stored as type Date. For example
If you want the date (26/06/95) to appear as
Monday 26th June
(Con’t, page 8)
|
Page 6
|
6 |