Can a mod fix this post please?

Miscellaneous Forums/General Discussion/Can a mod fix this post please?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Brucey:

I don't even understand what your example is doing.

You have a function which returns an int.

You have a global variable in that function, accessable only to that function, which retains its value when the function exits.

But you set that global variable to the value of some other global variable the minute you enter the function, overwriting any value you saved.

Unless I don't understand how globals in functions work and an assignment like that, which would set a local variable every time you enter the function, is only used to set the global variable's initial state and is never executed again after that, so it does indeed retain the value from the previous run. I don't use globals in functions, because I have never found a situation where they're useful. I almost always find I need to be able to access any value I might store like that in other functions, so it must be a global in the type instead.

Anyway.. Let's say the global isn't overwritten every time in your example. So the first time you enter the function the event is set to wxEVT_USER_FIRST. And a local ID is set to whatever was in that global when you entered the function. You then increment the global variable by 1, and then exit returning the value which the global had when you entered the function...

So... All this does is return a number one higher than the last, every time it's called? So it'll go 0,1,2,3... etc?

And you needed this difficult to understand function to do this simple task... why?

I mean when you think about it, in your code, you're just gonna be saying "Okay, I need a new event type to assign to this variable."

Which you will then code like this:

ThisEvent = wxNewEventType()

Calling your complicated function.

But you could have just created a global NextEvent (in whatever type uses these events), and then written the code:

ThisEvent = NextEvent
NextEvent = NextEvent + 1

And it would have been clear to anyone what it does immediately, and would have required near zero thinking to implement.

That is not to say I would do that in a library I intended other people to use, because asking people to increment a global every time they get a value just isn't good design, but if it was my own program, and I was trying to do things the fast way, and the way that would be easiest for me to go back to later to understand what the hell I was doing... I might go for the global. :-)