B4J Question Current declaration does not match previous one - But in Separate Namespaces (?)

cklester

Well-Known Member
Licensed User
Getting this error message.
VarMap - 57: Current declaration does not match previous one.<br />Previous: {Type=Map,Rank=0, RemoteObject=True}<br />Current: {Type=List,Rank=0, RemoteObject=True}

Searched the forum, and it has come up several times, but I'm wondering why (in my case) it is considered an error, since they are being defined in separate namespaces.

I tried defining them as "Private" and it still gives me the error.

This doesn't seem like it should be a problem unless I've misunderstood how namespaces work in B4J.

Same Variable Name But Two Different Namespaces:
        If value Is Map Then
            Dim copy As Map = CopyMap(value)
            newMap.SetValue(key, copy)
        Else If value Is List Then
            Dim copy As List = CopyList(value)
            newMap.SetValue(key, copy)
        Else
            newMap.SetValue(key, value) ' Direct copy for simple types
        End If
 

cklester

Well-Known Member
Licensed User
Does Dim copy As Object = ... work?

I don't know. I ended up changing my code to this:

Fixed Code:
        If value Is Map Then
            Dim mapCopy As Map = CopyMap(value)
            newMap.SetValue(key, mapCopy)
        Else If value Is List Then
            Dim listCopy As List = CopyList(value)
            newMap.SetValue(key, listCopy)
        Else
            newMap.SetValue(key, value) ' Direct copy for simple types
        End If
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
The 2 declarations are in the same namespace. If they are in different subs or in different modules you can reuse the names.
Doesn't CopyMap(value) already return a map? and CopyList(value) a List?
Note that the name 'result' in the two subs are defined differently, ok, since they are in different namespaces.
Also note that the value object in the argument is auto caste to the declared type in the sub

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
 
    Dim newMap As Map = CreateMap()
    Dim key As String = "aKey"
'    Dim value As Object = CreateMap("aaa": 1, "bbb": 2)
'    Dim value As Object = Array As String("aaa", "bbb")
    Dim value As Object = "aaa"
 
    If value Is Map Then
        newMap.Put(key, CopyMap(value))
    Else If value Is List Then
        newMap.Put(key, CopyList(value))
    Else
        newMap.Put(key, value) ' Direct copy for simple types
    End If
 
    Log(newMap.Get("aKey"))  '{aaa=1, bbb=2} or '[aaa, bbb] or 'aaa  respectively
End Sub

Private Sub CopyMap(mp As Map) As Map  'a simplistic stand-in for the OP's Sub - assumes that elements of the map are basic types
    Dim result As Map
    result.Initialize
    For Each kw As String In mp.keys
        result.Put(kw, mp.Get(kw))
    Next
    Return result
End Sub

Private Sub CopyList(lst As List) As List   'a simplistic stand-in for the OP's Sub - assumes that elements of the list are basic types
    Dim result As List
    result.Initialize
    For Each obj As Object In lst
        result.Add(obj)
    Next
    Return result
End Sub
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Longtime User
B4X:
Private Sub CopyList(lst As List) As List
    Dim result As List
    result.Initialize
    For Each obj As Object In lst
        result.Add(obj)
    Next
    Return result
End Sub

If the original List contains a List, then when that original List is copied to new List, isn't there then still data in the new List that, when you modify it, that modification then shows up in the original List too?

I thought the idea of copying was to make the two lists (original and new copy) separate and distrinct, so that changes to one didn't leak through to change the other.

Ditto for Maps.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
True. I just wanted to show the namespace and argument passing. These subs are not what the OP is using (I hope).
The real ones would have some recursive copying of sublists and submaps until a simple type is reached.
I have added some notes to clarify.
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
how namespaces work in B4J

It feels like you've got some C in your background, where the scoping done by the if-then-else brace blocks would mean that the Dim copy As statements would be creating entirely separate variables that could be of different Types (and also not exist outside of the brace blocks).

Whereas in B4J etc, the scope of variables is the entire Sub, and there are no nested scopes within program flow structures ie for and do loops, ifs, selects.

So you can't have the same variable declared to be of different Types within the same Sub.

But you can use the Object Type to have a variable capable of holding (or more usually: pointing to) different Types.
 
Upvote 0
Top