Android Question How to add a primitive variable to a map (not its value)? [Solved]

Gaxapu

Member
Licensed User
Hello all.
I´m trying to add a primitive variable to the value side of a map, but, anyhow i do it, only its value is added.
What I want is to relate the value side of a map to already defined vbles.

a small code snippet for clarify
B4X:
    Dim Vble1    As Int = 0
    Dim Mapa    As Map
    Mapa.Initialize
    Mapa.Put("Vble1", Vble1)
    Vble1 = 34
    Log(Vble1)
    Log(Mapa.Get("Vble1"))
this gives a log:
34
0

What I want is:
34
34

Any help is appreciated.
 

angel_

Well-Known Member
Licensed User
Longtime User
Maybe I did not understand the question well, but try this:

B4X:
    Dim Vble1    As Int
    Dim Mapa    As Map
   
    Vble1 = 34
   
    Mapa.Initialize
    Mapa.Put("Vble1", Vble1)
   
    Log(Vble1)
    Log(Mapa.Get("Vble1"))
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
As the int added to the map is a primitive it is stored inside the map. It is not a reference to a primitive variable.
Changing the var afterwards does not change the map-content.

If you add a List to the Map only a reference to this list is stored. So, when you change the list content after adding it to the map the content of the map is changed too.

Example
B4X:
    Dim vars As List
    vars.Initialize
    vars.Add(0) ' Add the value 0 to the list.

    Dim Mapa    As Map
    Mapa.Initialize
    Mapa.Put("VbleList", vars) ' Add a reference to the List into the Map
    Log(vars.Get(0)) ' Get the first item from list

    vars.Set(0,34)  ' Change VbleList and set a new value to item 0
    Log(vars.Get(0))' Get the first item from list
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
would it not just be simpler to do

Intead of
B4X:
Vble1 = 34
put
B4X:
Mapa.Put("Vble1", 34)

This will overwrite the old value.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I´m trying to add a primitive variable to the value side of a map, but, anyhow i do it, only its value is added.
Because that is how primitives work in B4X (and in Java). Primitives are not accessed via references, objects are.
 
Upvote 0

Gaxapu

Member
Licensed User
Thank you all.

I was afraid that the answer given by DonManfred and OliverA was THE answer.
That solved the question. It can´t be done (in a easy/clean way).

My intention was to access a bunch of deeply indented variables indistinctly from the variables or from the map.
For example:
X = t1Var.t2Var.t3Var.t4Var
equals
X = Mapa.get("t1Var.t2Var.t3Var.t4Var")

I´m sure you get the point.

The solution given by DonManfred, been correct, is no adequate for me in this use case, because it forces me to declare plus initialize a number of variables.

Just in case you want to know more about it:
I wanted to store/retrieve a file with pairs of VbleName/Value. The content values are sometimes consts, sometimes formulas.
On loading the file I could calculate the formula result and store it in the appropriate map member; accessing it through the vble or through the map as needed.

I´ll put the "Solved" in the title as soon as I notice there are no more interventions.

Note: Just to be sure. There is no way to reference a primitive variable?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The way to "reference a primitive" value is with a single element array.
B4X:
    Dim Vble1(1)    As Int
    Dim Mapa  As Map
    Mapa.Initialize
    Mapa.Put("Vble1", Vble1)
    Vble1(0) = 34
    Log(Vble1(0))
    Dim v() As Int = Mapa.Get("Vble1"))
    Log(v(0))

Or with a custom type:
B4X:
Type MyValue (Value As Int)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Or with a custom type:
Last night :confused: I was about to write that the problem could be solved by changing the type of the variable Vble1 to object (also thinking to a custom type).

Well, I did not do it because, before writing nonsense, I wanted to try and, surprisingly, it (object) does not work.
So now I have tried with a custom type and this works instead.

Just a little question: why? ?

Using an object:
B4X:
Sub TestObject
    Dim Vble1 As Object = 0
 
    Dim Mapa  As Map
    Mapa.Initialize
    Mapa.Put("Vble1", Vble1)
    Vble1 = 34

    Log(Mapa.Get("Vble1"))
   'log:  0
End Sub

Using a custom type:
B4X:
Type tMyVar(Value As Int)

Sub TestType
    Dim Vble1 As tMyVar
    Vble1.Initialize
 
    Dim Mapa  As Map
    Mapa.Initialize
    Mapa.Put("Vble1", Vble1)
    Vble1.Value = 34

    Dim V2 As tMyVar = Mapa.Get("Vble1")
    Log(V2.Value)
   ' log:  34
End Sub
 
Upvote 0

Gaxapu

Member
Licensed User
Last night :confused: I was about to write that the problem could be solved by changing the type of the variable Vble1 to object (also thinking to a custom type).

Well, I did not do it because, before writing nonsense, I wanted to try and, surprisingly, it (object) does not work.
So now I have tried with a custom type and this works instead.

Just a little question: why? ?

Using an object:
B4X:
Sub TestObject
    Dim Vble1 As Object = 0

    Dim Mapa  As Map
    Mapa.Initialize
    Mapa.Put("Vble1", Vble1)
    Vble1 = 34

    Log(Mapa.Get("Vble1"))
   'log:  0
End Sub

Using a custom type:
B4X:
Type tMyVar(Value As Int)

Sub TestType
    Dim Vble1 As tMyVar
    Vble1.Initialize

    Dim Mapa  As Map
    Mapa.Initialize
    Mapa.Put("Vble1", Vble1)
    Vble1.Value = 34

    Dim V2 As tMyVar = Mapa.Get("Vble1")
    Log(V2.Value)
   ' log:  34
End Sub

Hello Lucas.
The first thing I tried too. Was one of the many things I´ve tried.
Thinking it twice it happens that "Object" is also a primitive type and so it´s not passed as a reference to the map.

The second attempt with a custom type is what i want to elude. It means to define a custom type, declare a variable and initialize it. Three steps instead of just one.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The second attempt with a custom type is what i want to elude. It means to define a custom type, declare a variable and initialize it. Three steps instead of just one.
Not really. You define the type once. You also use the nice "generate create sub" and then all you need is to write:
B4X:
Dim v As MyValue = CreateMyValue(4)
Map.Put("v", v)
v.Value = 34
 
Upvote 0
Top