Objects as parameters in functions..

BlitzMax Forums/BlitzMax Programming/Objects as parameters in functions..

Czar Flavius(Posted 2009) [#1]
OK I know why this doesn't work now, as intended. (I'm too mentally exhausted to explain it myself) But not until after MANY hours of hair ripping and keyboard smashing anger. So could anyone tell me a different way to do what I'm trying to? (This is just example code, I need to do stuff with the link before I progress to the next one, and want to put the change in the function so I don't "forget" it in the main code)

Local list:TList = New TList
list.AddLast("1")
list.AddLast("2")
list.AddLast("3")

Local link:TLink = list.FirstLink()

For Local i = 0 Until 3
	Print String(link.Value())
	NextLink link
Next

Function NextLink(link:TLink)
	link = link.NextLink()
End Function

Desired output: 1 2 3
Actual output: 1 1 1


Gabriel(Posted 2009) [#2]
You would need to return the adjusted link from the NextLink function, and call it with Link=NextLink(Link).

I'm assuming there's a good reaon for not using EachIn, such as something you're going to do in that function with the Link.


Czar Flavius(Posted 2009) [#3]
OF COURSE! I am so stupid :D

Though I was hoping to avoid link=, as I couldn't fit what I want to do on a single line then. Such as Select popup(link), where pop returns the link's value but progresses to the next link, ready for the next operation. I'll have to store the link in a temp variable before Select, and then test that.

The reason for no EachIn is, I have popup and popdown functions, and will need to go up and down the list in arbitrary directions.


Gabriel(Posted 2009) [#4]
Well don't say you're stupid, because the only thing that attracted me to this question was the knowledge that I spent a long time wondering the exact same thing one night ;)

Indeed, I also concluded that it would be nice to find a way to do it which didn't involve link= but I don't think BlitzMax offers any way to pass objects by value.

You could try adding Var to the function definition like so:


Function NextLink(link:TLink Var)
link = link.NextLink()
End Function



But I think that only works with strings, which are treated slightly differently from all other objects. I might be wrong.

EDIT: Hmm, I guess I was wrong. It does seem to pass the object by value if you add Var as I suggested.