Modify a object in list
BlitzMax Forums/BlitzMax Beginners Area/Modify a object in list
| ||
| Hi everione. I created a list with 8 typeobject and every typeobject have 3 field, the question is it posible edit one field of 5th typeobect in list, por example?
SuperStrict
Global list:TList= CreateList()
Type mytype
Field x%=0
Field y%=1
Field z%=2
End Type
For Local n%=1 To 8
Local newtype:mytype
newtype = New mytype
Print newtype.x
Print newtype.y
Print newtype.z
list.addlast(newtype)
Next
I want to modify z in 5th newtype in list, is it possible? I hope you understand, i cant explein better 'cause i began two day ago in blitmax and programing, so i mega noob. |
| ||
SuperStrict Global list:TList = CreateList() Type mytype Field x%=0 Field y%=1 Field z%=2 End Type For Local n% = 1 To 8 Local newtype:mytype = New mytype Print newtype.x Print newtype.y Print newtype.z list.AddLast(newtype) Next Local temp:mytype 'modify z in 5th newtype in list temp = mytype(list.ValueAtIndex(5)) temp.z = 99 'modify z in all newtype in list For temp = EachIn list temp.z = 43 Next 'modify z only if y = 1 For temp = EachIn list If temp.y = 1 temp.z = 43 End If Next Something to note and remember - when you use ValueAtIndex, you need to surround this with the name of your type in brackets. You don't need to do that for EachIn. If you often need to modify only a single object in a list, and not all objects in the list, an array may be better for you. SuperStrict Global array:mytype[] Type mytype Field x%=0 Field y%=1 Field z%=2 End Type 'give array size of 8 array = New mytype[8] 'an array starts at number 0 not 1, so we use positions 0-7 (still 8 in total) not 1-8 For Local n% = 0 To 7 Local newtype:mytype = New mytype Print newtype.x Print newtype.y Print newtype.z 'place newtype into position n of the array array[n] = newtype Next Local temp:mytype 'modify z in 5th newtype in array. notice the difference temp = array[5] temp.z = 99 'modify z in all newtype in array. no difference to code For temp = EachIn array temp.z = 43 Next 'modify z only if y = 1. no difference to code For temp = EachIn array If temp.y = 1 temp.z = 43 End If Next The downside is that an array is of fixed size. You must know in advance the number of objects to add. You can resize the array later, but it will lose its old contents unless you save it in another array first. |
| ||
| You can resize the array later, but it will lose its old contents unless you save it in another array first. resizing one dimension array with slices will not loose its contents unless you are shrinking it. Be careful what you tell the noob unless your intention is to confuse him. ;). I am not saying he should be using slices either or not. |
| ||
| You should care about the cases, when you delete one element of the list. After this, Element no. 5 isn't longer at the 5th position (but on 4th). A better solution is to add a Field Index% to the Type and to fill this with a counter value every time you create a new one. After deleting one element, the position of all elements has changed, but not this index value: SuperStrict
Global List:TList= CreateList()
Type MyType
Global Last%
Field x%=1234
Field y%=1
Field z%=2
Field Index%
Function CreateNew()
Local locType:MyType
locType = New MyType
locType.Index=Last
Last=Last+1
List.Addlast(locType)
End Function
End Type
; how to create some new elements:
For Local I%= 1 to 10
MyType.CreateNew()
Next
; how to find a certain emelemt:
For locType:MyType= EachIn List
If locType.Index=5 then
Print locType.X
Endif
Next
A much more elegant way to do this is to integrate the search fuction into the TYPE: SuperStrict
Global List:TList= CreateList()
Type MyType
Global Last%
Field x%=1234
Field y%=1
Field z%=2
Field Index%
Function CreateNew()
Local locType:MyType
locType = New MyType
locType.Index=Last
Last=Last+1
List.Addlast(locType)
End Function
Function Element:MyType(Nr%)
For Local locType:MyType= EachIn List
If locType.Index=Nr Then
Return locType
Endif
Next
End Function
End Type
' how to create some new elements:
For Local I%=1 to 10
MyType.CreateNew()
Next
' how to find a certain emelemt:
Print MyType.Element(5).X
|
| ||
| Oh dear that will probably confuse him.. I wouldn't encourage use of lists in that way just to find one element. |
| ||
| Thanx for your time. I tried every example and I understand. Very useful for me. Great community !!! |