Android Question Correct way to deal with string / number cast

mmanso

Active Member
Licensed User
Longtime User
Hi there,

I'm making an API call which returns this:

B4X:
(MyMap) {success=1}

Then, I've this code to validate a success return code:

B4X:
If res.GetDefault("success", "") = "1" Then

To my surprice, this "If" isn't True... If I change it to:

B4X:
If res.GetDefault("success", "") = 1 Then

It works but the IDE complains:



What's the correct way to deal with this?

Thanks.
 

Attachments

  • screenshot_2021-02-18 10_43_27.png
    8.6 KB · Views: 107

Sandman

Expert
Licensed User
Longtime User
Did you try to put the object on the right side of the comparison?

Like so:
B4X:
If 1 = res.GetDefault("success", "") Then
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
B4X:
If 1 = res.GetDefault("success", FailValue)  Then

Where FailValue is whatever the fail value should be, presumably an int.

Edit: I was too slow
 
Upvote 0

mmanso

Active Member
Licensed User
Longtime User
I ended up doing the

B4X:
Dim wasSuccess As String = res.GetDefault("success", "")
If wasSuccess = "1" Then
End If

To avoid IDE complains...

I'm just wondering how will I avoid future bugs based on the... I was hoping that:

B4X:
If res.GetDefault("success", "") = "1" Then

would be evalutated in "String" context...
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
The IDE is giving you the actual solution, I don't know why you're not taking the advice.

Anyway, here's a post that might explain things:
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…