Assigning a pointer to a structure passed as a parameter to the current value of a list?

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Assigning a pointer to a structure passed as a parameter to the current value of a list?

Post by Quin »

I have this test code:

Code: Select all

EnableExplicit

Structure Thing
	Str.s
	Num.i
EndStructure

Global NewList Things.Thing()

Macro AddThing(_Str_, _Num_)
	AddElement(Things())
	Things()\Str = _Str_
	Things()\Num = _Num_
EndMacro

AddThing("Test1", 12)
AddThing("Test2", 14)

Procedure.i FindThingByStr(Str$, *Out.Thing)
	If Str$ = "" : ProcedureReturn #False : EndIf
	Str$ = LCase(Str$)
	ForEach Things()
		If LCase(Things()\Str) = Str$
			*Out = Things()
			ProcedureReturn #True
		EndIf
	Next
	ProcedureReturn #False
EndProcedure

OpenConsole()
Print("Type a name: ")
Define Name$ = Input()
Define T.Thing
If FindThingByStr(Name$, @T)
	PrintN(T\Str + T\Num)
Else
	PrintN(Name$ + " not found.")
EndIf
Input()
However, if I type Test1 or Test2 in any case, it always shows 0 and doesn't have the correct values assigned to the fields, but it does successfully find it. If I type an invalid name, the invalid name handling triggers. This is utterly baffling.
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Assigning a pointer to a structure passed as a parameter to the current value of a list?

Post by mk-soft »

Code: Select all

EnableExplicit

Structure Thing
	Str.s
	Num.i
EndStructure

Global NewList Things.Thing()

Macro AddThing(_Str_, _Num_)
	AddElement(Things())
	Things()\Str = _Str_
	Things()\Num = _Num_
EndMacro

AddThing("Test1", 12)
AddThing("Test2", 14)

Procedure.i FindThingByStr(Str$, *Out.Thing)
	If Str$ = "" : ProcedureReturn #False : EndIf
	Str$ = LCase(Str$)
	ForEach Things()
		If LCase(Things()\Str) = Str$
		  ;*Out\Num = Things()\Num
		  ;*Out\Str = Things()\Str
		  CopyStructure(@Things(), *Out, Thing)
		  ProcedureReturn #True
		EndIf
	Next
	ProcedureReturn #False
EndProcedure

OpenConsole()
PrintN("Type a name: ")
Define Name$ = Input()
Define T.Thing
If FindThingByStr(Name$, @T)
	PrintN(T\Str + "," + T\Num)
Else
	PrintN(Name$ + " not found.")
EndIf
Input()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
skywalk
Addict
Addict
Posts: 4241
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Assigning a pointer to a structure passed as a parameter to the current value of a list?

Post by skywalk »

You need to do a deep copy.
*Out\Num = Things()\Num
*Out\Str = Things()\Str
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Assigning a pointer to a structure passed as a parameter to the current value of a list?

Post by Quin »

mk-soft wrote: Sat Apr 12, 2025 8:46 pm

Code: Select all

EnableExplicit

Structure Thing
	Str.s
	Num.i
EndStructure

Global NewList Things.Thing()

Macro AddThing(_Str_, _Num_)
	AddElement(Things())
	Things()\Str = _Str_
	Things()\Num = _Num_
EndMacro

AddThing("Test1", 12)
AddThing("Test2", 14)

Procedure.i FindThingByStr(Str$, *Out.Thing)
	If Str$ = "" : ProcedureReturn #False : EndIf
	Str$ = LCase(Str$)
	ForEach Things()
		If LCase(Things()\Str) = Str$
		  ;*Out\Num = Things()\Num
		  ;*Out\Str = Things()\Str
		  CopyStructure(@Things(), *Out, Thing)
		  ProcedureReturn #True
		EndIf
	Next
	ProcedureReturn #False
EndProcedure

OpenConsole()
PrintN("Type a name: ")
Define Name$ = Input()
Define T.Thing
If FindThingByStr(Name$, @T)
	PrintN(T\Str + "," + T\Num)
Else
	PrintN(Name$ + " not found.")
EndIf
Input()
Thanks a ton, this works! I could've sworn there was a CopyStructure() function, but I couldn't find it under the Memory section of the documentation and didn't search that well.
Post Reply