Jump to content
Server Maintenance This Week. ×

How to use a custom function to name a variable


This topic is 5961 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I have not used custom functions before, and I need to use one to name a global variable. I have found several, the easiest to understand for me is this one I found on Matt Petrowsky's web site called SetGlobalVariable. The problem I have,is I don't know how to implement it in a script. In other words, what script step do I use to get it to name the variable ? It is supposed to replace the SetVariable" step, but with what script step?

I am baffled, and I feel like it is right in front of me but I have been staring at it too long.

Any clues ?

Here's Matt's text:

Hi Guys,

I recently had the need (or more likely the desire) to set global variables dynamically..

As most of you will know, Set variable allows you to specify a dynamic value and repetition, but not a dynamic name for your variable. This often leads you to store info in a delimited list within one repetition when you need to store multiple peices of information per one subject.

This will soon make your scripts become messy and the logic harder to follow

Hence two small custom functions:

Custom Function: SetGlobalVariable( name ; value ; repetition)

Evaluate("Let( $$" & name & "[" & repetition & "] = "" & value & "" ; "" )" )

Example Usage: SetGlobalVariable( "record_" & table::primaryKey ; table::aField ; 1 )

If your primaryKey field contained "991" and your aField field contained "Alex!!" -

[color:red]This would be the equivalent of using the script step: Set Variable( $$record_991[1] ; Value: "Alex!!" ) ..

or otherwise writing: Let( $$record_991[1] = "Alex!!" ; "" )

Which means that if you were to now retrieve the value of $$record_991 - you would get "Alex!!" as the value returned.

Link to comment
Share on other sites

Any script step that performs a calculation can be used for this. The variable is created during the course of evaluating the calculation's formula. The actual result of the calculation is insignificant (it is set to return empty in your example), and so is the entire script step (unless you find an additional use for it). For example:

...

Set Variable [ $dummy ; Value: SetGlobalVariable ( Table::Field ; Table::AnotherField ; 1 ) ]

...

sets the $dummy variable to empty - but creates an additional, dynamically named, variable in this otherwise redundant process. Or:

...

If [ SetGlobalVariable ( Table::Field ; Table::AnotherField ; 1 ) ]

# nothing happens here

End If

...

However, I must say I am having a hard time imagining a situation where this would be needed. After all, one puts a value into a variable in order to retrieve it later. If the variable's name must be computed again, that only makes the entire process unduly complicated - like keeping a needle in a haystack.

Link to comment
Share on other sites

global variables are a flawed design

Whoa, before you agree with me - that is NOT what I said. Global variables are a tool. They can produce a flawed design, if not used appropriately - but there's nothing inherently wrong with them. Same as repeating fields, really.

Link to comment
Share on other sites

Correct! ... but here were the inclusion of a repeating notation in the CF indicating a substantial use of such.

As long as you can't use Extend( when assigning values to a $ or $$ variable, isn't the entire bracket notation not particular convenient except when two loops interacts:

http://blinkerfish.blogs.com/__fmcollective_old/2007/04/creating_multip.html

But this example hardly require globals then :)

--sd

Link to comment
Share on other sites

However, I must say I am having a hard time imagining a situation where this would be needed. f the variable's name must be computed again, that only makes the entire process unduly complicated - like keeping a needle in a haystack.

This was me being lazy at some point - I created the function specifically to be used in dynamic loop scripts which required record memory, and in another situation in combination with a plugin for an experimental audit script which turned out quite well... and I could be wrong, but I believe there is a corresponding GetGlobalVariable function.

Link to comment
Share on other sites

There is the corresonding GetGlobalVariable function. My use for this is to record the layout name and active tabs in the layout so I can return to the last state of that layout. I have tried another solution to it and it bogs down terribly. My layouts have a consistent tab scheme, 3 top tabs, and five bottom tabs. I have found a custom function which captures the topmost tabs quite nicely. My strategy is to use the SetGlobalVariable function to name the variable the same as the layout that is being navigated out of (using a button), and use the second and third repetitions to store the topmost two tabs. Then be able to recall those values when navigating back to that layout. By giving the tabs common object names (1,2,3, a,b,c) I can use the same script for all of my navigation buttons. Does this make sense ? Is there a better way ?

Link to comment
Share on other sites

Thank you Comment. I was doing the right thing the wrong way. Once I saw your example, I was able to get my syntax right, and smooth sailing!

Since my last response, I put this thing together and so far it works perfectly. Here is my script;

Go_To Layout Scriipt

Set Variable [ $dummy; Value:SetGlobalVariable(Get(LayoutName);1;

Case(

isFrontMostTabPanel ( "1" )=1;"1";

isFrontMostTabPanel ( "2" )=1;"2";

isFrontMostTabPanel ( "3" )=1;"3";

"")

) ]

Set Variable [ $dummy; Value:SetGlobalVariable(Get(LayoutName);2;

Case(

isFrontMostTabPanel ( "a" )=1;"a";

isFrontMostTabPanel ( "b" )=1;"b";

isFrontMostTabPanel ( "c" )=1;"c";

isFrontMostTabPanel ( "d" )=1;"d";

isFrontMostTabPanel ( "e" )=1;"e";

"")

) ]

Go to Layout [ Get(ScriptParameter) ]

Go to Object [ Object Name: GetGlobalVariable ( Get(LayoutName) ; 1 ) ]

Go to Object [ Object Name: GetGlobalVariable ( Get(LayoutName) ; 2 ) ]

There does not seem to be a big performanc hit at this point, so I have my fingers crossed.

Thank you all for your help.

Link to comment
Share on other sites

Just as a general note:

Case(

isFrontMostTabPanel ( "a" )=1;"a";

isFrontMostTabPanel ( "b" )=1;"b";

isFrontMostTabPanel ( "c" )=1;"c";

isFrontMostTabPanel ( "d" )=1;"d";

isFrontMostTabPanel ( "e" )=1;"e";

"")

can be written:

Case(

isFrontMostTabPanel ( "a" );"a";

isFrontMostTabPanel ( "b" );"b";

isFrontMostTabPanel ( "c" );"c";

isFrontMostTabPanel ( "d" );"d";

isFrontMostTabPanel ( "e" );"e"

)

The default value is not required and the =1 is also unnecessary.

Link to comment
Share on other sites

I am having a strong sense of déjà vu here:

http://fmforums.com/forum/showtopic.php?tid/186848/post/250828/#250828

Does this make sense ? Is there a better way ?

I don't know. In general, have mixed feelings about 'reusable code' methods such as this one. Filemaker is not truly a programming environment, and these techniques inevitably end up being unduly complex by trying to overcome the inherent limitation of the medium.

Link to comment
Share on other sites

I'll be honest, I've not taken a likening (this word looks wrong) to FM tabs so haven't really played with the whole tab order reversion thing, was just making a general observation about the Case statement.

But given that you are you and that the CF looks pretty awesome, I will take you're word for it lol :)

Link to comment
Share on other sites

HI- It seems to me that a four line script isn't that complex to implement. I have had mixed feelings about using tabs for the simple reason they are a pain when they keep jumping around on you. For my situtation, once I understood the syntax of the custom functions, it took me fifteen minutes to write the script, name the tabs, and set the script parameters in my navigation buttons. I am going to check out "Black Magic" and see how it may be an improvement. Thank you very much GenX for the advice on trimming down the code. As an itermediate user who knows enough to be dangerous, but still really struggles with much of the high level techniques, I have found that sometimes less elegent (pedestrian) but understandable (to me)solutions are just what the doctor ordered to keep me productive while I learn more. Thanks again for everyones help!

Link to comment
Share on other sites

This topic is 5961 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.