Android Question map assignment question

Discussion in 'Android Questions' started by Multiverse app, Mar 19, 2019.

Tags:
  1. Multiverse app

    Multiverse app Active Member Licensed User

    I have a simple question.

    Code:
    Dim mapGlobal As Map
        mapGlobal.Initialize
        mapGlobal.Put(
    "Key1""Value1")
        map2=mapGlobal
    Does the "=" the pass the reference of the mapGlobal to map2 or creates an object at new memory location?
     
  2. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    Can't test it now, but how about altering mapGlobal and see if that change shows in map2?

    RBS
     
  3. Multiverse app

    Multiverse app Active Member Licensed User

    Yes, it does

    Code:
    Dim mapGlobal As Map
        mapGlobal.Initialize
        mapGlobal.Put(
    "Key1""Value1")
        mapGlobal.Put(
    "Key2""Value2")
        mapGlobal.Put(
    "Key3""Value3")
        mapGlobal.Put(
    "Key4""Value4")
       
       
        
    Dim map2 As Map=mapGlobal
        
    Log(map2.Remove("Key1"))
       
        
    Log("mapGlobal: ")
        
    For i=0 To mapGlobal.Size-1
            
    Log(mapGlobal.GetKeyAt(i)&", "&mapGlobal.GetValueAt(i))
        
    Next
       
        
    Log("map2: ")
        
    For i=0 To map2.Size-1
            
    Log(map2.GetKeyAt(i)&", "&map2.GetValueAt(i))
        
    Next
    log:
    Code:
    Value1
    mapGlobal: 
    Key2, Value2
    Key3, Value3
    Key4, Value4
    map2: 
    Key2, Value2
    Key3, Value3
    Key4, Value4
     
    José J. Aguilar likes this.
  4. Erel

    Erel Administrator Staff Member Licensed User

    1. You should never use GetKeyAt / GetValueAt.
    Code:
    For Each Key As String In map2.Keys
     
    Log($"${Key}: ${map2.Get(Key)}"$)
    Next
    2. You can use CreateMap:
    Code:
    Dim MyGlobal As Map = CreateMap("Key1""Value1""Key2""Value2")
     
    Multiverse app likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice