Book a Demo

Author Topic: C# code generated from event stereotype ?  (Read 4165 times)

gfoote

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
C# code generated from event stereotype ?
« on: February 04, 2015, 02:03:39 am »
I'm evaluating EA 12  RC2 and have the following issue. I've defined an operation FlagsChanged for a class, and applied the C# event stereotype to it. When I generate code from the class I get the following C# code:

Code: [Select]
           public event EventHandler FlagsChanged{
                  add{
                        <unknown> += value;
                  }
                  remove{
                        <unknown> -= value;
                  }
            }

I'm not sure why the add/remove accessors are being created, but I don't need them, and the <unknown>s mean the code won't compile. How do I  stop them being generated ? Thanks.

webfox

  • EA Novice
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Re: C# code generated from event stereotype ?
« Reply #1 on: February 04, 2015, 06:09:02 pm »
Hi,

you need to modify the C# code generation templates to achieve the desired behaviour:

In EA, press CTRL + SHIFT + P to bring up the code generation template editor. Make sure "C#" is selected in the Language drop-down list. The item you need to modify is the "event" stereotype override (lower left panel) of the "Operation Body" template:

Just strip the entire content and replace it by a semicolon. Then save your changes and retry.

Further details about the code generation template framework can be found here: http://www.sparxsystems.com/enterprise_architect_user_guide/11/extending_uml_models/codetemplates_2.html.

Cheers,

Till

gfoote

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: C# code generated from event stereotype ?
« Reply #2 on: February 04, 2015, 08:32:17 pm »
Ahh, I see. Thanks. all working now.

I'm new to EA, but it seems rather odd that the default template can't generate valid code for the most common requirement i.e. no backing field required.

Also, that I need to read a template to discover the attribute_name tag to specify the backing filed, but maybe I missed that in the documentation ?

Anyway, thanks again for the pointer.

Geoff

webfox

  • EA Novice
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Re: C# code generated from event stereotype ?
« Reply #3 on: February 04, 2015, 08:38:14 pm »
Hi,

it's not explicitly documented, but EA expects events to be implemented like properties, that is, you'd have a backing field. The events itself are then exposed through add and remove methods like so:

Code: [Select]
private EventHandler flagsChanged;

public event EventHandler FlagsChanged
{
   add {
      flagsChanged += value;
   }
   remove {
      flagsChanged -= value;
   }
}

You'll find yourself adjusting the templates to your needs anyway at some point, trust me! ;) Looooots of things to discover! ;D

Cheers,

Till
« Last Edit: February 04, 2015, 08:43:37 pm by webfox »