When a structure is born, set automatic values?

Just starting out? Need help? Post your questions and find answers here.
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

When a structure is born, set automatic values?

Post by Seymour Clufley »

Code: Select all

Structure demostructure
  token.s
EndStructure


Procedure.b InitializeDemoStructure(*struc.demostructure)
  ; this procedure fills the token field in a demostructure with some random letters
  ; I'm looking for a way to automatically call this procedure whenever an instance of demostructure is first made
  
  *struc\token = ""
  For a = 1 To 8
      *struc\token+Chr(Random(25)+65)
  Next a
  
EndProcedure


Procedure DoSomething(*ds.demostructure)
  
  ; this procedure uses an instance of the demostructure
  
EndProcedure


test1.demostructure
Debug "test1's token is "+test1\token

DoSomething(@test2.demostructure)
Debug "test2's token is "+test2\token
Is that just pie in the sky?
User avatar
greyhoundcode
Enthusiast
Enthusiast
Posts: 112
Joined: Sun Dec 30, 2007 7:24 pm

Post by greyhoundcode »

Looks like a job for a constructor method :wink:
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

I didn't find a solution for procedures parameters but for local variables:

Code: Select all

Procedure.s RandomHash()
	For c = 1 To 8
	   temp$ + Chr(Random(25)+65)
	Next
	ProcedureReturn temp$
EndProcedure 

Structure _demostructure
  token.s
EndStructure

Macro demostructure
	_demostructure\token = RandomHash()
EndMacro

test1.demostructure
test2.demostructure
test3.demostructure

Debug test1\token
Debug test2\token
Debug test3\token
Maybe it can get you started.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Post Reply