Inc() and Dec() functions

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?

Use extends?

Myvar.inc

1 Like

Extend to what
 the variable? No, sometimes I have dozens of vars that need in-or decrease.

You’d use ByRef to do that

Sub Inc(byref value as integer)
Value = value + 1
End Sub
3 Likes

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.

1 Like

As a matter of fact that crossed my mind. Didn’t look any further. Thanks.

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.

What you want to achieve? Your code seems weird to my eyes without context.

2 Likes

Hi, I have not yet used the byref case. Can you explain in a little more detail where and how to write that?

I am just trying to accomplish what the OP is looking for, a simple function to increment or decrement a variable without having to write x = x + 1.

How sweet it is, I think I have it figured out, for me anyhow

a method at the app level

Function INC(byref value as integer) As Integer
  value = value + 1
  return value
End Function

In my app window level
dvTransItems.cell(lri,1) = app.INC(RowNo)
RowNo is a local var at the window level.

Thanks so much for that.

Now I can delete the spaghetti and pretend it never happened.
I have to understand more about byref, very powerful.

Analyze this (run, check, read the code):

inc_dec.zip (6.6 KB)

1 Like

Yes, I see now even easier, Thank you for that.

1 Like

I know this is a very ‘newby’ question, but what is IncDec with the globe icon next to it?

Got it, a module. Thanks again. So much to learn, such little time.

1 Like

Correct.

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).

1 Like

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.

2 Likes

I understand, thanks again.

1 Like