Does Xojo have an equivalent to FileMakers "Global Calculations"?

Before using Xojo, my most recent programming tool of choice was FileMaker Developer 6 (which is now almost 15 years old!!).

When creating a field you could assign it a type ‘calculation’. Then within the code window you would use the usual logical expressions. The calculations were ‘global’. They were persistent and the value could be called anywhere within the program. They also re-calculated depending on other 'calculations"

What this meant was I could use them in a sort of ‘cascading’ effect, where it was easy enough to pull out values (that were dependent on others) and write simple truth tables based on many scenarios. Perhaps lazy programming, but it all worked very well.

The reason I ask is that I’m creating a simple XY graph - It’s all working perfect but the line of code is becoming too long - which makes it hard to understand and I would like to break it up a bit. Here’s the code:

[code]
Dim totalSamples as Integer
totalSamples = recordedGramsArray.Ubound

Dim forceY as Integer

For timeX as Integer = 0 to totalSamples-1

forceY = timeX

[b]GraphPicture.Graphics.DrawLine(timeX, (kGraphHeight-recordedGramsArray(forceY)), timeX+1, (kGraphHeight-recordedGramsArray(forceY+1)))[/b]

Next timeX

//Update the Canvas

cnvGRAPH.Backdrop = GraphPicture[/code]

The bold line is where I’d like to make a change. In essence all this is, is Graphics.DrawLine(X1,Y1,X2,Y2) but the code is getting hard to read and I need to add more to that line:

eg. (kGraphHeight-recordedGramsArray(forceY)*scaleFactorY). I’d like to be able to calculate that elsewhere first.

Hope it’s understood what I’m getting at. Your thoughts appreciated.

[EDIT] Shame about the bold text within the code not being bold. What the…?

@Steve Kelepouris: To make that one line of code easier to read, you could break out your X1, Y1, X2, and Y2 values.

For example:

[code]Dim totalSamples as Integer

totalSamples = recordedGramsArray.Ubound

Dim forceY as Integer
Dim X1,Y1,X2,Y2 as Integer

For timeX as Integer = 0 to totalSamples-1

forceY = timeX
X1 = timeX
Y1 = kGraphHeight - recordedGramsArray(forceY)
X2 = timeX + 1
Y2 = kGraphHeight - recordedGramsArray(forceY+1)

GraphPicture.Graphics.DrawLine(X1, Y1, X2, Y2)

Next timeX

//Update the Canvas

cnvGRAPH.Backdrop = GraphPicture[/code]

In Xojo you don’t put calculations in database fields.
You write code where you need it.

So you can write a method where you collect all the values in an array, do some calculations and than draw a graph.

And you can take a look on our ChartDirector plugin:
http://www.monkeybreadsoftware.de/xojo/plugin-chartdirector.shtml

That is the same chart library as used in FileMaker.

I don’t know if Steve asked about database-related questions so much.
Seems to me more like a call for methods or computed properties. You could insert a method like

Function GraphHeight(Force As Integer) As Double Return kGraphHeight-recordedGramsArray(Force) End Function

(or Integer instead of double of your calculation uses integers)

and call your code with

GraphPicture.Graphics.DrawLine(timeX, (GraphHeight(forceY)), timeX+1, (GraphHeight(forceY+1))

The method can be part of your window, of a module or a class. It should have access to RecordedGramsArray() of course, so that might indicate where it fits best – should be within the scope of the array.

I think between a [Code] tag some html tags are ignored.

EDIT: Not really lazy programming but one of the features of object oriented programming: Just like you normalize a database into its different tables in database programming, it is a wise idea to break down complicated tasks into simple steps, using methods or computed properties (which can basically be the same as methods except for they don’t take any additional input parameters). Much easier to debug, and if you err, you only got one piece of code to change, not all the occurrences where you duplicated the code.

You need to be careful when you use the term ‘global’ when talking about Filemaker, because they use that word in a way that’s quite different from typical programming languages.

Version 6 was the last of the ancient versions of Filemaker. It changed radically when version 7 came out.

As others have mentioned, calculations are not stored in database fields in Xojo. In Filemaker, a ‘calculated field’ is a database field whose value is calculated from other fields in the same record. Hence, the user cannot enter/edit the value. The calculated value is actually stored in the database along with other field values (though in versions 7 and later you can choose whether the value is stored or unstored), so that it doesn’t need to be recalculated when the record is called up, thus speeding up the display, and also allowing the table to be sorted and searched using the calculated field as a key field (a neat feature BTW). If any of the dependent field values are changed, then the calculated field value is automatically recalculated and stored.

Filemaker also has so called ‘global’ fields which are not associated with any particular record. A global field is really a simple variable which contains only one value for the entire database table.

You can calculate values as part of your SELECT statement as well. But from the example you gave, it’s best to do it in code.

[quote=292802:@Tim Dietrich]@Steve Kelepouris: To make that one line of code easier to read, you could break out your X1, Y1, X2, and Y2 values.

For example:

[code]Dim totalSamples as Integer

totalSamples = recordedGramsArray.Ubound

Dim forceY as Integer
Dim X1,Y1,X2,Y2 as Integer

For timeX as Integer = 0 to totalSamples-1

forceY = timeX
X1 = timeX
Y1 = kGraphHeight - recordedGramsArray(forceY)
X2 = timeX + 1
Y2 = kGraphHeight - recordedGramsArray(forceY+1)

GraphPicture.Graphics.DrawLine(X1, Y1, X2, Y2)

Next timeX

//Update the Canvas

cnvGRAPH.Backdrop = GraphPicture[/code][/quote]

Thanks all, much appreciated.

Tim, this is the approach I’ve taken. I already knew that I could do it this way, but over-complicated it (as usual). Simple, but does the job and works.

Functions would and may well be needed in the long run, but I’ll have have to get my mind around using them correctly.

Fortunately I don’t do programming for a living. I consider it a challenge and therefore would rather try to create it myself rather than use others already worked out solutions - free or otherwise. Well, at this stage anyway :slight_smile: