Android Question Casting String to number (Int)

moster67

Expert
Licensed User
Longtime User
Just some thoughts of mine and wondering about your opinions...

B4A casts automatically a numerical String to a number so the following code-samples work fine:

B4X:
If IsNumber(txtNumInput.Text) Then
        If txtNumInput.Text > "9" Or txtNumInput.Text = "0" 'the first values are both casted to Double and then compared, the last check is using ".equals" in java-sources
            MsgboxAsync("Sorry but your number must be between 1 and 9. Please try again","Result")
            txtNumInput.Text = ""
            txtNumInput.RequestFocus
            Return
        End If
    End If

also, this variant works:

B4X:
    If IsNumber(txtNumInput.Text) Then
        If txtNumInput.Text > 9 Or txtNumInput.Text = 0 Then 'the first value is casted to Double and then compared to 9, the last check is using ".equals" in java-sources
            MsgboxAsync("Sorry but your number must be between 1 and 9. Please try again","Result")
            txtNumInput.Text = ""
            txtNumInput.RequestFocus
            Return
        End If
    End If

However, I feel more comfortable by explicitly casting a numerical String to an Int and then checking its Int-value to other numbers (Int), like this:

B4X:
    If IsNumber(txtNumInput.Text) Then
        Dim InNum As Int = txtNumInput.Text 'explicit casting to Int in generated Java-sources
        If InNum > 9 Or InNum = 0 Then
            MsgboxAsync("Sorry but your number must be between 1 and 9. Please try again","Result")
            txtNumInput.Text = ""
            txtNumInput.RequestFocus
            Return
        End If
    End If


I just feel that the first and second approaches are not natural while at the same it is great that B4A does the casting for us automatically.
Don't get me wrong - it works.
I don't know what to think and which approach to use from now on... ?
 

Star-Dust

Expert
Licensed User
Longtime User
The third method i consider it the cleanest and the safest. Casting sometimes gives unexpected results.

In fact now I would do so at the most;
B4X:
If 9<txtNumInput.Text Or 0=txtNumInput.Text Then

But the third method I think is cleaner
 
Last edited:
Upvote 0

moster67

Expert
Licensed User
Longtime User
OK, thanks. Then my instincts were correct :)

Actually, the question arose in a small education course I am doing with some elderly people where some of the participants already had some programming experience in other languages. While still being convinced of using explicit casting in this case, I still got hesitant since B4X does so many things for us automatically, such as casting, and one might not think of any negative side-effects.

Perhaps some comments about automatic casting should be mentioned in the Code Smells thread or shown as a warning in the log??
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
...I am doing with some elderly people where some of the participants already had some programming experience in other languages..
I am also an elder person with some programming experience now ... I'm not stunned though :oops::oops::oops::eek::eek::eek:
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
I am also an elder person with some programming experience now ... I'm not stunned though :oops::oops::oops::eek::eek::eek:
Lol - however, some of these elderly people programmed 25-30 years ago and have not programmed since then!
You on the other hand, just like me (I am also an elder person), are still going strong with programming! :)
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
I don't think that the code you posted can be considered a common mistake
Well, that's why I was baffled :)
Someone wrote that code and it worked but then another person with coding experience said that should not be possible because in other languages you would need to cast explicitly (or convert) unless implicit casting can take place.

Anyway, as I said, I was just wondering about your opinions...
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
I'm worried that I might make a hard-to-find mistake with the automatic conversion. One part of me appreciate the ease of it, but another part of me would like to be forced to do it like your C# example:
B4X:
int i = int.Parse(EditText.Text)

I think I might appreciate having something like this:
B4X:
#AutomaticTypeConversion: false
to disable it and force me to do things stricter.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
think I might appreciate having something like this:
B4X:
#AutomaticTypeConversion: false
to disable it and force me to do things stricter.
Such flags cause more problems than solutions.
A better way to solve it is with a warning, however I don't remember a single forum discussion where someone had problems related to this.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
I don't remember a single forum discussion where someone had problems related to this
Yeah, me neither. I just worry that I will make a mistake that will cause big problems down the line. A warning is probably a great and very good enough solution.
 
Upvote 0
Top