Delphi has this incredible handy Inc() and Dec() functions. Allows you to write Inc(MyClass.Acceleration.Unit) or Inc(MyClass.Acceleration.Unit,15) instead of MyClass.Acceleration.Unit = MyClass.Acceleration.Unit + 1 or MyClass.Acceleration.Unit = MyClass.Acceleration.Unit + 15.
Writing a function/method that increases a Variable is easy. But in Xojo you have to use it as MyVar = Inc(MyVar) Is it possible to write this function in such a way that it can be used as in Delphi?
Interesting, I too am frustrated that we canât just use x+1 or x-1 like in c++ and JavaScript, but your example from delphi gives me an idea. I see no reason that you cannot write a method that takes 2 variables and returns the results. Something like INC(value, increment_value) then In the method just add the increment_value to the value and return the result. I will try it later. Thanks for the idea.
Thatâs not what inc() and dec() does in Pascal, thatâs what Succ() and Pred() does (return the next/previous value, donât modify the target).
And those functions donât act on âIntegersâ, they act on ANY ORDINAL type (Byte, Integer, int32, int64⊠even a Char, any ordinal).
And they are inline, it means, insert very fast instructions in the code right in the place they were invoked not needing stacking parameters and a call to a function.
Yes, sorting it out in my mind during coffee on the couch and actually doing it has shown me that it is more complicated that I first thought.
I did create some spaghetti to get done what works in my situation. I know there is a much better solution but I donât know how to get there yet.
anyhow here it is for everyone to make fun of.
Sub INC(vname as String, val as Integer)
select case vname
case "RowNo"
RowNo = RowNo + val
end Select
End Sub
and to call it
app.INC("RowNo",1)
and to use it
dvTransItems.cell(lri,1) = App.RowNo
it may write nice but it is a resource hog, I imagine. I put it in the app level and the variable is a public variable at the app level.
I see the heads spinning now. Maybe someone will fix it.
If you create a new project, and drag this IncDec (with the globe) to the navigator of the new project, the new project now gets those 2 new functions that can be used everywhere (Scope Global).
Absolutely perfect. Thanks again. Now that I know how to make global methods I can fix all of the around about ways I have done it in the past.
I am going global.
Use Global with care. Operate the most local as possible, global is just for âtoolsâ really used and necessary being visible everywhere. Global for too many things may be seen as bad design.