Page 1 of 1

ChangeCurrentElement

Posted: Mon Mar 13, 2017 11:40 pm
by Josh
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.

Re: ChangeCurrentElement

Posted: Tue Mar 14, 2017 1:24 am
by Thunder93
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.

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())
Edited: Included change mentioned by MrMat.

Re: ChangeCurrentElement

Posted: Thu Mar 16, 2017 6:22 pm
by MrMat
Hi Thunder93,

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" 
becomes:

Code: Select all

While NextElement(mylist()); "Three" and change them to "3" 
Cheers,
Mat

Re: ChangeCurrentElement

Posted: Thu Mar 16, 2017 6:51 pm
by Thunder93
Made the change in my previous post. Might be too long winded though, .. that example.

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

Re: ChangeCurrentElement

Posted: Mon Dec 18, 2017 8:33 pm
by mestnyi
Not there wrote :oops:

Re: ChangeCurrentElement

Posted: Sun Jan 21, 2018 4:30 pm
by mestnyi
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.
In the help it is written that does not return anything then as it that that returns.

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
Can someone explain this to me?
I really need to know if the pointer is valid if yes then do this and then that ...

Re: ChangeCurrentElement

Posted: Sun Jan 21, 2018 6:21 pm
by #NULL
Can someone explain this to me?
If the return value is 'none' then there is no point in using it.
I really need to know if the pointer is valid if yes then do this and then that ...
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:

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

Re: ChangeCurrentElement

Posted: Tue Feb 06, 2018 10:29 pm
by Andre
I would be happy to extend the PB docs with one or more of the posted examples. :D
But before of that, Fred should take a look according return value and related notes / examples...

Re: ChangeCurrentElement

Posted: Wed Feb 07, 2018 12:14 pm
by #NULL
IMO the example code should be concise and down to the actual command, not fiddling with structures or ListIndex()

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
The current example code shows more or less what today would be done via PushListPosition() and PopListPosition(). These might be mentioned in 'see also'.

Re: ChangeCurrentElement

Posted: Wed Feb 07, 2018 10:33 pm
by Andre
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.

Re: ChangeCurrentElement

Posted: Fri Mar 06, 2020 10:23 pm
by Andre
@Fred / all: Is still more/better decription needed about the ReturnValue?