Android Question Converting object to string

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I can do this with something like this:

B4X:
Sub CStr(oVal As String) As String
    Return oVal
End Sub
[\CODE]

This is fine if oVal has a string assigned to it, eg: oVal = "abc"
However, this won't work if oVal has nil assigned to it, so for example:

[CODE]
Dim o As Object
Log(CStr(o))
[\CODE]

This will give something like: |java.lang.Object@fc3ece5|
Which I take is perhaps a memory location.
What I would like is for it to return "" so an empty string.

Any suggestions how to do this?

RBS
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I can do this with something like this:

B4X:
Sub CStr(oVal As String) As String
    Return oVal
End Sub

This is fine if oVal has a string assigned to it, eg: oVal = "abc"
However, this won't work if oVal has nil assigned to it, so for example:

B4X:
Dim o As Object
Log(CStr(o))

This will give something like: |java.lang.Object@fc3ece5|
Which I take is perhaps a memory location.
What I would like is for it to return "" so an empty string.

Any suggestions how to do this?

RBS
Should be:

I can do this with something like this:

B4X:
Sub CStr(oVal As String) As String
    Return oVal
End Sub

This is fine if oVal has a string assigned to it, eg: oVal = "abc"
However, this won't work if oVal has nil assigned to it, so for example:

B4X:
Dim o As Object
Log(CStr(o))

This will give something like: |java.lang.Object@fc3ece5|
Which I take is perhaps a memory location.
What I would like is for it to return "" so an empty string.

Any suggestions how to do this?

RBS
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
How about?

B4X:
Private Sub Cstr(obj As Object) As String
    If obj = Null Then Return "" Else Return obj
End Sub
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
However, this won't work if oVal has nil assigned to it, so for example:
I am afraid you are suffering from a misconception The above is not correct. Your value of your object o is not a nil or Null. It is a reference to an area of memory where the actual object resides. When the object is converted to a string you get the Java object reference, though you can't do much with an object, which is the building block for all other reference types as it has very few methods. Assigning Null to o will change the value of the object reference to Null and free the area of memory occupied by the object to be garbage collected.

In practice you will never Dim a variable as an Object as it is pretty useless, though declaring Sub parameters as Object lets you pass instances of various different reference types to a Sub where you can test for their actual type with 'Is', so your example is not valid in the real world. I think you need to explain a bit more about what you require.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I am afraid you are suffering from a misconception The above is not correct. Your value of your object o is not a nil or Null. It is a reference to an area of memory where the actual object resides. When the object is converted to a string you get the Java object reference, though you can't do much with an object, which is the building block for all other reference types as it has very few methods. Assigning Null to o will change the value of the object reference to Null and free the area of memory occupied by the object to be garbage collected.

In practice you will never Dim a variable as an Object as it is pretty useless, though declaring Sub parameters as Object lets you pass instances of various different reference types to a Sub where you can test for their actual type with 'Is', so your example is not valid in the real world. I think you need to explain a bit more about what you require.
> if oVal has nil assigned to it,

By that I meant, there is no code doing oVal = x
So, not sure where the misconception is.

In the real app I won't do Dim o As Object as I can see that servers little purpose, but instead we might be dealing with an array of objects or perhaps a Map where the keys
for the values are dealt with as objects.

RBS
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
By that I meant, there is no code doing oVal = x
So, not sure where the misconception is.
You have declared an Object o. An object is a reference type and the value of the variable o is a pointer to a region of memory where the declared Object resides. There is no nil or Null involved. Declaring the Object with Dim creates an instance of it. You don't need to assign anything to its variable o because doing so will overwrite the pointer to the original object instance which will then be freed by the garbage collector and the variable o will now point to whatever x is.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
You have declared an Object o. An object is a reference type and the value of the variable o is a pointer to a region of memory where the declared Object resides. There is no nil or Null involved. Declaring the Object with Dim creates an instance of it. You don't need to assign anything to its variable o because doing so will overwrite the pointer to the original object instance which will then be freed by the garbage collector and the variable o will now point to whatever x is.
OK, I understand, but what if we have for example an array of objects, so:

B4X:
Dim arrObjects(10) As Object

and I am iterating through that array and need to deal with an empty array element as if it was a null string, so "" ?

RBS
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
There won't be an empty array element as B4X avoids dealing with Nulls by filling an array with a default value for each element. For an arrray of Objects this is the same as Dim arr(x) as Object for each element. Erel has tried to ensure you will never find a Null in official B4X language elements and libraries. If you enocunter a Null it will probably have come from an additional library method.

Java:
// Dim oa(10) As Object
_oa = new Object[(int) (10)];
int d0 = _oa.length;
for (int i0 = 0;i0 < d0;i0++) {
_oa[i0] = new Object();
}
 
Upvote 0

cklester

Well-Known Member
Licensed User
OK, I understand, but what if we have for example an array of objects, so:

B4X:
Dim arrObjects(10) As Object

and I am iterating through that array and need to deal with an empty array element as if it was a null string, so "" ?

RBS

Are you in control of building and using the array? If so, go with another data structure. An array of "any-possible values" seems to be a terrible way to manage data.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
There won't be an empty array element as B4X avoids dealing with Nulls by filling an array with a default value for each element. For an arrray of Objects this is the same as Dim arr(x) as Object for each element. Erel has tried to ensure you will never find a Null in official B4X language elements and libraries. If you enocunter a Null it will probably have come from an additional library method.

Java:
// Dim oa(10) As Object
_oa = new Object[(int) (10)];
int d0 = _oa.length;
for (int i0 = 0;i0 < d0;i0++) {
_oa[i0] = new Object();
}
OK, that is nice to hear and I have no actual problem to deal with but was just thinking I might come across Null elements, but it seems this can't really happen.
Thanks for clarifying that.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Are you in control of building and using the array? If so, go with another data structure. An array of "any-possible values" seems to be a terrible way to manage data.
I am in control of all this, and I don't think I have array of objects, but was just looking for possible problems.
As I understand it now from agraham's post these problems are very unlikely to occur.

RBS
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
@agraham has explained it well.
For completeness, see my test results below.

B4X:
'    Dim o As Object
'    Log(o = Null)            'False
'    Log(CStr(o).length)      '0
'    o = Null
'    Log(o = Null)            'True
'    Log(CStr(o).length)      '0

Private Sub CStr(obj As Object) As String
    Dim s As String = obj
    If obj = Null Then                                    'will be converted to the string "null" which is not what you want
        Return ""
    Else if s.startsWith("java.lang.Object@") Then        'unassigned    - probably an error but could possibly be handled
        Return ""
    Else
        Return s
    End If
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
For completeness, see my test results below.
Don't forget how B4X handles Null strings

B4X:
Private Sub CStr(obj As Object) As String
    Dim s As String = obj
    If obj = Null Then                                    'will be converted to the string "null" which is not what you want
        Return ""
    Else If s.startsWith("java.lang.Object@") Then        'unassigned    - probably an error but could possibly be handled
        Return ""
    Else if s.EqualsIgnoreCase("null") Then                'B4X Null String test
        Return ""
    Else
        Return s
    End If
End Sub
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
@agraham has explained it well.
For completeness, see my test results below.

B4X:
'    Dim o As Object
'    Log(o = Null)            'False
'    Log(CStr(o).length)      '0
'    o = Null
'    Log(o = Null)            'True
'    Log(CStr(o).length)      '0

Private Sub CStr(obj As Object) As String
    Dim s As String = obj
    If obj = Null Then                                    'will be converted to the string "null" which is not what you want
        Return ""
    Else if s.startsWith("java.lang.Object@") Then        'unassigned    - probably an error but could possibly be handled
        Return ""
    Else
        Return s
    End If
End Sub
Yes, had done some testing and found the same.
Although it looks I don't need to worry about this it would be easy to handle.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Yes, had done some testing and found the same, including:

1D string array:
starting with: [Ljava.lang.String;

2D string array:
starting with: [[Ljava.lang.String;

List:
(List) Not initialized

B4XOrderedMap:
starting with: [b4xcollections=null,

So, again, it looks I don't need to worry about this, but it would be easy to handle.

RBS
 
Upvote 0
Top