Android Question How to cast an object to string?

Midimaster

Active Member
Licensed User
The MAP.GET() returns my string as an object. Now I want to trim the string, but how to cast an object to string?
B4X:
'this is easy:
Dim word as String = MyPropery.Get("Name")
word = word.Trim

'but how to cast in one line?
'this will not work:
Dim oneLine as String = MyPropery.Get("Name").Trim
Dim anotherLine as String = String(MyPropery.Get("Name")).Trim

is there a way to do this in B4X?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This cannot work: Dim oneLine as String = MyPropery.Get("Name").Trim
There is no relation between the value returned from Map.Get and the left side type.

You will need to use a variable or a sub like this:
B4X:
Sub GetProperty(Key As String) As String
Return MyProperty.Get(Key)
End Sub

B4X:
Dim OneLine As String = GetProperty("Name").Trim
 
Upvote 0

emexes

Expert
Licensed User
Or you could try a sub like this:
B4X:
Sub CastToString(S As String) As String
    Return S
End Sub
and then doing casts to String like this:
B4X:
Dim OneLine As String = CastToString( MyProperty.Get("Name") ).Trim
 
Upvote 0
Top