Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I wrote a function name as functionTest and also mentioned default Options.

   BeginPackage["OptionTesting`"]
   (*Options*)
   Options[functionTest] := {checks -> True, bColor -> Automatic};
   (*defination*)
   functionTest[buttonName_, OptionsPattern[]] :=
    (If[
        (OptionValue[checks] == True),
        (Button[buttonName, Null,Background -> OptionValue[bColor]]),
        (SetOptions[OptionTesting`functionTest,bColor -> Green];
          Button[buttonName, Null, Background -> OptionValue[bColor]]
        )
       ](*if is closed*)
     )
    EndPackage[]

calling the above function in different cases

case1:

   OptionTesting`functionTest["Ok", checks -> True]

in case1 OptionValue[checks]=True so we get Button with Backgroung color is default color.

case2:

  OptionTesting`functionTest["Ok", checks -> False]    

in case2 OptionValue[checks]=False so we get Button with Backgroung color is Green color.

Problems:

if you are evaluate case1 before case2,we are getting button with default color.

if you are evaluate case1 after case2,we are getting button with Green color.

but I want,

If OptionValue[check]==True,button with default color.

If OptionValue[check]==False,button with Green color

I guess because Of setOptions,it's not working.

can anybody help me?

share|improve this question
With your edits the question now makes much less sense to me. You're right that SetOptions is persistent--this is as it should be! If you don't want the option to persist, then don't use SetOptions--just pass the option normally. (Or at least set the option back to how it was afterwards.) I am going to vote to close this as too localized. Try this: BeginPackage["OptionTesting`"]; Options[functionTest]={checks->True,bColor->Automatic}; functionTest[buttonName_,OptionsPattern[]]:= Button[buttonName,Null,Background->If[TrueQ@OptionValue[checks],OptionValue[bCol‌​or],Green]]; EndPackage[]; – Oleksandr R. Feb 1 at 11:33

closed as too localized by Oleksandr R., Yves Klett, Sjoerd C. de Vries, Artes, rm -rf Feb 4 at 18:44

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

I am not sure if this is what you are actually asking, but a frequently encountered issue that may be relevant here is that one should usually define options using RuleDelayed (:>) instead of Rule (->) if their final values are to be determined at a later stage. Essentially, RuleDelayed is to Rule what SetDelayed is to Set, in both cases because of the presence or absence (respectively) of the HoldRest attribute (SetDelayed is HoldAll, and that implies HoldRest).

A common scenario might look as follows:

ClearAll[f];
$defaultReturnValue = "hello!"; Options[f] = {returnValue -> $defaultReturnValue};
f[] := OptionValue[f, returnValue];

But:

f[]
(* -> "hello!" *)
$defaultReturnValue = "goodbye!"; f[]
(* -> "hello!" *)

Clearly, this is not what was intended. Instead, we should use:

Options[f] = {returnValue :> $defaultReturnValue};

The indirectly set option value is now reflected in the output:

f[]
(* -> "goodbye!" *)
$defaultReturnValue = "hello!"; f[]
(* -> "hello!" *)

Although this example involves a dependency of an option value on a symbol, it can just as well depend on another option as seems to be the case in the question. In general, the value is determined at definition time if Rule is used, but deferred until evaluation if RuleDelayed is employed instead.

share|improve this answer
I tried this way also but not working... – subbu Feb 1 at 6:45
I updated my question,check it .. – subbu Feb 1 at 7:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.