Book a Demo

Author Topic: Help on using the Code Template Editor  (Read 3469 times)

MikeLamenza

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Help on using the Code Template Editor
« on: March 29, 2018, 02:07:55 am »
Hello,

I'm working in C# and would like to modify a template for Operations. Currently there's a standard setter/getter but as I'm working in WPF I'd like to add support for bindings and thus add some stuff to the setter. See below.  Despite my noodling in the Template editor I'm not seeing a clear way to do what I'd like to do. Help is appreciated.

Code fragment that is currently generated:
        private bool _SystemOn;
        public bool SystemOn{
           get{
              return _SystemOn;
           }
           set{
              _SystemOn = value;
           }
        }

What I would like to be generated:
        private bool _SystemOn;
        public bool SystemOn
        {
            get
            {
                return _SystemOn;
            }
            set
            {
                if (_SystemOn != value)
                {
                    _SystemOn = value;
                    RaisePropertyChanged(nameof(SystemOn));
                }
            }
        }

Nizam

  • Prolab Moderator
  • EA User
  • *
  • Posts: 320
  • Karma: +15/-2
  • Model Sharing - Simplified
    • View Profile
    • Professional Model Collaboration
Re: Help on using the Code Template Editor
« Reply #1 on: March 29, 2018, 08:07:07 am »
You must modified the 'Code Templates' (accessible from Code -> Configure (Source Code) -> Code Template Editor.

In C# Language you'll find Operation Body Template , you've to select the 'property' override under Stereotype Overrides and then insert your custom code.

Hint you've to modify the $write variable to include your logic and it must be something like
$write="\tset{\n\t\t if" "$att" + "!= value \n\t{" +  $att + " = value;\n\t RaisePropertyChanged(nameof(" + %opName% + "));\n }\n}"

PS - Just refer for syntax, haven't tested it.

MikeLamenza

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Help on using the Code Template Editor
« Reply #2 on: March 29, 2018, 09:55:51 am »
Excellent - that worked - thanks for your help. It was unclear to me that those files had context sensitivity requiring selecting property override. Here's the actual text I had to develop. Ouch!

$write="\tset\n\t{\n\t\t if (" + $att + " != value)\n\t\t { \n\t\t\t" + $att + " = value;\n\t\t\tRaisePropertyChanged(nameof(" + %opName% + "));\n\t\t}\n\t}"

Nizam

  • Prolab Moderator
  • EA User
  • *
  • Posts: 320
  • Karma: +15/-2
  • Model Sharing - Simplified
    • View Profile
    • Professional Model Collaboration
Re: Help on using the Code Template Editor
« Reply #3 on: March 29, 2018, 10:09:36 am »
Good it worked :)
I've always felt these are like Excel formulas, not that difficult when writing, but painfully tough with readability :)