About user typed structure

doraneko

Member
Licensed User
Longtime User
For example,

Type TPtData(Name As String,ID As Int, Male As Boolean)
Dim PtData(100) As TPtData
Dim PtTemp As TPtData

PtData(0).Name="Mr. Zero"
PtData(0).ID=0
PtData(0).Male=True

PtTemp=PtData(0)

May I substitute PtData(0) to PtTemp ?

In my program, these method loses member's values.

Please teach me.:sign0085:
 

JMB

Active Member
Licensed User
Longtime User
Hi there.

I think the problem is that you need to initialize your variables. It's not sufficient just to Dim them.

Try PtData.Initialize and PtTemp.Initialize after your Dim lines and before you assign values.

JMB
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
No, they don't need to be initialised. Basic4android initialises all variables and arrays with a default value, including the fields of a Type.

What is the problem you see. If I take your code and add

Msgbox(ptTemp.Name,"Name")

at the end I get "Mr. Zero" displayed as I would expect.
 
Upvote 0

doraneko

Member
Licensed User
Longtime User
This is an example program.
---------------------------------------------
Sub Process_Globals
Type TPtData(Name As String, ID As Int, Male As Boolean)
End Sub

Sub Globals
Dim Button1 As Button
Dim Button2 As Button
Dim Button3 As Button
Dim ListView1 As ListView
Dim PtTemp As TPtData
Dim PtData(10) As TPtData
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("ObjectTest")
Initialize
End Sub

Sub Initialize
Dim i As Int
For i=0 To 9
PtData(i).Initialize
Next
PtTemp.Initialize
ListView1.Clear
End Sub

Sub Button3_Click
Activity.Finish
End Sub

Sub Button1_Click
Dim i As Int
For i=0 To 9
PtData(i).ID=i
PtData(i).Name="Name #"&i
Next
ListNew
End Sub

Sub Button2_Click
PtTemp=PtData(0)
PtData(5)=PtTemp
PtTemp.Name="Temp"
ListNew
End Sub

Sub ListNew
ListView1.Clear
Dim i As Int
Dim s As String
For i=0 To 9
s=PtData(i).Name & " ID:" & PtData(i).ID
ListView1.AddSingleLine(s)
Next
End Sub
-----------------------------------------
Please push Button1
And When I push Button2, I expect to change from "Name #5 ID:5" to "Name #0 ID:0". But in fact, "Name #0 ID:0" and "Name #5 ID:5" change to "Temp ID:0" !
I think, in structure's assignment, one's value substitute to another by reference.
But, I want to substitute by value.
Please help me.:sign0085:
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
It's better if you use the code tags to enclose code and better still if you post the exported program so it can be run.

Types are passed by reference so if you want to copy one you need to Dim a new one and assign the individual fields from the old to the new one.
 
Upvote 0
Top