ChangeCurrentElement
Moderator: Documentation Editors
ChangeCurrentElement
The example in the help for ChangeCurrentElement is not a running code. It's only a part.
I'm not sure this is a bug or it is by design, but other examples in help are full running snippets.
I'm not sure this is a bug or it is by design, but other examples in help are full running snippets.
sorry for my bad english
Re: ChangeCurrentElement
I've seen some that weren't full.
I guess it's expected that someone has already little knowledge working with lists.
Here's full version demonstrating the usage.
Edited: Included change mentioned by MrMat.
I guess it's expected that someone has already little knowledge working with lists.
Here's full version demonstrating the usage.
Code: Select all
Structure BasicStructure
Name.s
EndStructure
NewList mylist.BasicStructure()
AddElement(mylist())
mylist()\Name = "One"
AddElement(mylist())
mylist()\Name = "Two"
AddElement(mylist())
mylist()\Name = "Three"
AddElement(mylist())
mylist()\Name = "Four"
SelectElement(mylist(), 1) ; Changing from last element to second element
*Old_Element = @mylist() ; Get the address of the current element
Debug "At position "+ListIndex(mylist())+", the value is "+Str(mylist())
ResetList(mylist()) ; Perform a search for all elements named
While NextElement(mylist()); "Three" and change them to "3"
If mylist()\name = "Three"
mylist()\name = "3"
EndIf
Wend
ChangeCurrentElement(mylist(), *Old_Element) ; Change from last element to previously saved element position.
Debug "At position "+ListIndex(mylist())+", the value is "+Str(mylist())
Last edited by Thunder93 on Thu Mar 16, 2017 6:30 pm, edited 1 time in total.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Re: ChangeCurrentElement
Hi Thunder93,
Thanks for your example. This is incredibly minor, but in case it gets pasted verbatim into the docs:
becomes:
Cheers,
Mat
Thanks for your example. This is incredibly minor, but in case it gets pasted verbatim into the docs:
Code: Select all
While NextElement(mylist()); "John" and change them to "J"
Code: Select all
While NextElement(mylist()); "Three" and change them to "3"
Mat
Mat
Re: ChangeCurrentElement
Made the change in my previous post. Might be too long winded though, .. that example.
Maybe something like;
Maybe something like;
Code: Select all
Structure BasicStructure
Name.s
EndStructure
NewList mylist.BasicStructure()
AddElement(mylist()) : mylist()\Name = "HOME"
AddElement(mylist()) : mylist()\Name = "ONE"
*Old_Element = @mylist() ; Get the address of the current element
AddElement(mylist()) : mylist()\Name = "TWO"
AddElement(mylist()) : mylist()\Name = "THREE"
Debug "At position "+ListIndex(mylist())+", the value is "+mylist()\Name
ChangeCurrentElement(mylist(), *Old_Element) ; Change from last element to previously saved element position.
Debug "Now at position "+ListIndex(mylist())+", the value is "+mylist()\Name
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Re: ChangeCurrentElement
Not there wrote
Re: ChangeCurrentElement
In the help it is written that does not return anything then as it that that returns.Syntax
ChangeCurrentElement(List(), *NewElement)
Description
Changes the current element of the specified list to the given new element. This function is very useful if you want to "remember" an element, and restore it after performing other processing.
Parameters
List() The name of the list, created with the NewList function. You must include the brackets after the list name.
*NewElement The new element to set as the current element for the list. The element must be a pointer to another element which exists in this list. You should get this address by using the @ operator on the list name and not through any other method.
Return value
None.
Code: Select all
Macro IsChangeCurrentElement(List, Element)
Bool(Element And ChangeCurrentElement(List, Element))
EndMacro
Structure BasicStructure
Name.s
EndStructure
NewList mylist.BasicStructure()
AddElement(mylist()) : mylist()\Name = "HOME"
AddElement(mylist()) : mylist()\Name = "ONE"
*Old_Element = @mylist() ; Get the address of the current element
AddElement(mylist()) : mylist()\Name = "TWO"
AddElement(mylist()) : mylist()\Name = "THREE"
Debug "At position "+ListIndex(mylist())+", the value is "+mylist()\Name
; *Old_Element = ChangeCurrentElement(mylist(), *Old_Element) ; Uncomment to see
; *Old_Element = 0 ; Uncomment to see
If IsChangeCurrentElement(mylist(), *Old_Element) ; Change from last element to previously saved element position.
Debug "Now at position "+ListIndex(mylist())+", the value is "+mylist()\Name
EndIf
I really need to know if the pointer is valid if yes then do this and then that ...
Re: ChangeCurrentElement
If the return value is 'none' then there is no point in using it.Can someone explain this to me?
You should always know if your pointer is valid. null it as soon as the pointee becomes invalid. You could loop throught the list and compare each element address with your pointer, but there is no guarantee a matching address means it is still the object you assigned to the pointer:I really need to know if the pointer is valid if yes then do this and then that ...
Code: Select all
NewList l.i()
AddElement(l()) : l() = 111
AddElement(l()) : l() = 222
AddElement(l()) : l() = 333
LastElement(l())
*p.Integer = @l()
Debug *p\i
DeleteElement(l())
AddElement(l()) : l() = 444
Debug *p\i
Code: Select all
333
444
- Andre
- PureBasic Team
- Posts: 2071
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Re: ChangeCurrentElement
I would be happy to extend the PB docs with one or more of the posted examples.
But before of that, Fred should take a look according return value and related notes / examples...
But before of that, Fred should take a look according return value and related notes / examples...
Re: ChangeCurrentElement
IMO the example code should be concise and down to the actual command, not fiddling with structures or ListIndex()The current example code shows more or less what today would be done via PushListPosition() and PopListPosition(). These might be mentioned in 'see also'.
Code: Select all
NewList myList()
AddElement(myList())
myList() = 100
AddElement(myList())
myList() = 200
*element = @myList()
AddElement(myList())
myList() = 300
Debug myList() ; 300 (last element)
ChangeCurrentElement(myList(), *element) ; restore list position
Debug myList() ; 200
ForEach myList()
If @myList() = *element
Debug "element: " + myList() ; element: 200
EndIf
Next
- Andre
- PureBasic Team
- Posts: 2071
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Re: ChangeCurrentElement
Have added the last example of #NULL for now, also the Push/Pop references.
Some clarification about the return value is up to Fred. Else this topic can be closed.
Some clarification about the return value is up to Fred. Else this topic can be closed.
- Andre
- PureBasic Team
- Posts: 2071
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Re: ChangeCurrentElement
@Fred / all: Is still more/better decription needed about the ReturnValue?