Home   Archive   Permalink



string conversion isn't working.

Hi
I'm sure there must be something easy that I am missing here but I can't get a test for to-string conversion to work when all the other conversion from integer to time are working.
Here is the text:R E B O L []
fldblk:[vi to-integer vn to-string vdc to-decimal vdt to-date vtm to-time vmn to-money]
    
errchk: func [fldblk errblk ][
    i: 0
    foreach [fld tp] fldblk [
        ;print errblk
        ;print i
        f: get fld
        print rejoin [tp " " f/text]
        if error? try [do rejoin [tp " " f/text]] [i: i + 1 errblk/:i/1: fld errblk/:i/2: "Failed to convert"]
    ]
]
view gui: layout[
    vi: field
    vn: field
    vdc: field
    vdt: field
    vtm: field
    vmn: field
    
    button "val" [
        errblk: array [6 2] copy []
        errchk fldblk errblk
        either errblk/1/1 == none [
            alert "Good Entry"
        ][
            alert reform ["Errors " errblk/1/2]
            print to-string errblk/1/1
            focus get errblk/1/1
            print errblk
            return
        ]
    ]
]

posted by:   John     29-Apr-2014/2:41:03-7:00



The vn field has a to-string conversion but when a string is entered the test gives an error. Why is that?

posted by:   John     29-Apr-2014/2:42:56-7:00



In fact the conversion works when I enter a number into the vn field.

posted by:   John     29-Apr-2014/2:48:12-7:00



After looking at it further, it seems that if i put something like "sss" in the vn field that when it goes to "do rejoin [tp " " fld]" that it gets a script error . If i literally try this in console mode do rejoin [to-string " " "sss"] it produces the script error saying sss has no value whereas it doesn't say this for to-integer " " "10".


posted by:   john     29-Apr-2014/4:03:39-7:00



In fact to continue the above example if i say
do rejoin [to-string " " "10"] that works but put "sss" in place of the 10 and it doesn't work in converting a string. apparently it thinks "sss" is a variable.

posted by:   John     29-Apr-2014/5:09:20-7:00



When you 'do an integer value, the result evaluates just as if you've typed the integer into the console. Likewise, when you 'do a random string of characters, it's just as if you'd typed those characters into the console. So, if the 'sss word isn't defined, you get the expected error:
    
>> 10
== 10
>> sss
** Script Error: sss has no value
** Near: sss
    
>> do 10
== 10
>> do "sss"
** Script Error: sss has no value
** Near: sss
    
By default, any set of characters typed into a field can be treated as a string. What case are you intending to test for by validating a string data type?

posted by:   Nick     29-Apr-2014/8:14:52-7:00



That is a good point Nick. I don't have any real case that I was intending to test for by converting a string into a string. But I was curious why it was so darn difficult to do it. :). Maybe I should just skip doing an empty validation since anything goes in that field.


posted by:   john     29-Apr-2014/16:05-7:00



However, an expedient solution might be to put some null action in the position of the to-conversion value. vi to-integer vn magicnullaction vdc etc.. Got a suggestion?


posted by:   john     29-Apr-2014/18:36:25-7:00



You should really take a look at 'parse. All the validation possibilities are handled more effectively with it.

posted by:   Nick     29-Apr-2014/22:17:28-7:00



As it stands, you can just leave "vn to-string" out of your fldblk.

posted by:   Nick     30-Apr-2014/17:31:18-7:00