No implicit cast as string?

ikidd

Member
Licensed User
Longtime User
I use this and the compare doesn't work (1,2,3,4... is in the tag of some of the views):

B4X:
Dim j As Int
For j = 1 To labels.Size
   For i = 0 To pnlAdd.NumberOfViews-1
      If pnlAdd.GetView(i).tag=j Then  '<----Won't compare as string
         pnlAdd.GetView(i).Visible= True
         Dim b As Label
         b= pnlAdd.GetView(i)
         b.Text=labels.GetKeyAt(j-1)
      End If
   Next   
Next

But if I punt j into an implicit string variable a la:

B4X:
Dim j As Int
For j = 1 To labels.Size
   For i = 0 To pnlAdd.NumberOfViews-1
      Dim tagtest As String
      tagtest=j
      If pnlAdd.GetView(i).tag=tagtest Then  'Works, praise be to Murphy
         pnlAdd.GetView(i).Visible= True
         Dim b As Label
         b= pnlAdd.GetView(i)
         b.Text=labels.GetKeyAt(j-1)
      End If
   Next   
Next

then no problem. I'm not really understanding why, and I can't find a CAST or CSTRING function that I would figure would be integral to situations like this.

What is happening that I'm missing about implicit casts here?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
The problem in this case is that the Tag type is Object. The compiler cannot implicitly cast the value to string in this case.

In this case it is actually better to cast the tag to int:
B4X:
Dim t As Int = pnlAdd.GetView(i).tag
If t = j Then ...

Explicit cast functions are almost always not required. In this case you can add such a method:
B4X:
Sub CString(o As Object) As String
 Return o
End Sub
 
Upvote 0

ikidd

Member
Licensed User
Longtime User
If I use:

B4X:
Dim tagtest As Int = pnlAdd.GetView(i).tag
if tagtest=j then...

throws a Java.lang.NumberFormatException on the first view it iterates if I do it that way.

If I do it the way I showed earlier, it seems to work OK.

Also: it seems to think a Label and and Editbox are the same ie:

B4X:
If pnlAdd.GetView(i) Is Label Then
            pnlAdd.GetView(i).Visible= True
            Dim b As Label
            b= pnlAdd.GetView(i)
            b.Text=labels.GetvalueAt(j-1)
         Else If pnlAdd.GetView(i) Is EditText Then
            pnlAdd.GetView(i).Visible = True
         End If

Puts the value in both the label and the editbox that are tagged at j-1.

I think I read that they both are inherited from the same java object in the back end, so I can see this happening. Any way of differentiating them other than parsing more data out of the tag?
 
Upvote 0
Top