Android Question Issue on passing types

LucianDesign

Member
Licensed User
Longtime User
Hello,
I have some issues on understanding how custom types are working.
In my code bellow I'm passing to a sub processTempObj(aObject As MyType) a MyType object.
Inside I create a new instance tmpProcessObj and assign the aObject. From what I see the aObject is assigned by reference because when I change tmpProcessObj.Name="Something is changed" then in reality I also change the initial aObject.
Is this normal? All I want is to pass the value into the new instance and not to chance my intial Object2 from the code.

Can someone help me?
P.S. Also the project is attached

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   
    'This is MyType
    Type MyType (Name As String)
    'A simple list
    Dim MyList As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    'Create 2 objects with MyType
    Dim Object1 As MyType
    Object1.Initialize
    Object1.Name = "Object one"
    Dim Object2 As MyType
    Object2.Initialize
    Object2.Name = "Object two"
    'Initialize the list and add Object1 and Object2
    MyList.Initialize
    MyList.Add(Object1)
    MyList.Add(Object2)
   
    'We log the list to see its content
    Log(MyList)
   
    doSomething 'something to do
   
    'We log the list again
    Log("---- The list again ----")
    Log(MyList)
   
End Sub

Private Sub doSomething
    For Each tmpObject As MyType In MyList
        If tmpObject.Name = "Object two" Then
            processTempObj(tmpObject)
        End If
    Next
End Sub


Private Sub processTempObj(aObject As MyType)
    'We create a new instance and pass the aObject
    Private tmpProcessObj As MyType
    tmpProcessObj.Initialize
    tmpProcessObj = aObject
    tmpProcessObj.Name="Something is changed" 'Changing the name in the new instance
End Sub
 

Attachments

  • test_mytype_list.zip
    7 KB · Views: 110

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top