Sometimes I struggle with the documentation. Since I am starting a new project I want to use the Random class introduced with 2019r2. The documentation says only create one instance of this class per app. So I try to create an app property of class Random which I will use many times. I can’t figure how to do this.
I could just use the old Rnd function but would like to use the new stuff.
New desktop project
Add a module named “Globals” (I do this instead of putting them on App)
Add a property to this module
gRandom as Random
Add a method to the module, Init, with the following code
gRandom = new Random
Then in app open you call Globals.Init
Thats the simplest
Now any where you need a random number you can just use gRandom.InRange or whatever
A better version might be
New desktop project
Add a module named “Globals” (I do this instead of putting them on App)
Add a property to this module
protected mGRandom as Random
Add a COMPUTED property to this module, gRandom, that has the GET portion implemented as
Init
return mGRandom
Add a private property
mInited as boolean
Add a method to the module, Init, with the following code
if minited then
return
end if
mGRandom = new Random
minited = true
Now the global property is basically read only everywhere
And things get initialized without you having to call anything etc
The only thing you then have to maintain is the properties that are global and setting them up initially when init is called
Why add it to a module? It‘s already in the System module - so you can just use it:
Var i As Integer = System.Random.InRange(0, 1000)
MessageBox("The random number is: " + i.ToString)
You don‘t:
Use System.Random to get an instance of the class:
So System.Random returns to you an instance. For all intents and purposes, System.Random IS the instance and you can use it directly without creating another variable.
Because I never bothered to look at the docs for 2019r2 (or later) since I mostly dont use those
And because if you DO that your code is no longer backwards compatible IF that matters
Mine IS
I know - but he specifically asked about the new class and is familiar with the old one.
Problem is: with new Apple silicone coming soon everyone will need to update if they want their apps to run natively on those Macs (a nice shot in the arm for Xojo). But I do wonder if they will use that as an excuse to remove API 1 (at least for the new Mac target) …
It could be the final shove some need to abandon Xojo
Or to update if their apps don’t work well enough in the new Rosetta
Will be interesting to see
I expect the new Apple Silicon target should be about like recompiling for Linux on Pi which is ARM
Nothing more
That would be a big issue for more than few I suspect… I don’t think they will do that… yet.
But I am pretty sure they will do nothing that makes writing API 1 code and maintaining backward compatibility in the IDE any easier. Which means the new versions will effectively keep being compile only for API 1 code…until they start breaking API 1 stuff.
-Karen
well there’s the fun part
IF they actually REMOVED the API 1 code then a person could write a module that put them back and they couldnt remove that
I should have known that the issue was iOS. I didn’t mention that it was an iOS project since the documentation on the new class says “Supported for all project types and targets.”
Doesn’t seem to be the case for Xojo 2019r3.1. I created a desktop project and the class works perfectly there. In the iOS this line of code does not autocomplete:
r=system.random.lessthan(10000)
and the error message is:
“This item does not exist.”