Book a Demo

Author Topic: Initial code in Setter and Getter  (Read 5159 times)

andregatti

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Initial code in Setter and Getter
« on: October 24, 2005, 07:05:18 am »
Is there a way that a setter or getter includes the initial code inside?

I mean, a setter is always like this: this->[atributte name] = newVal;
the getter is always like this: return this->[atributte name];

I don't like to write it every time I generate the setter and getter...

thomaskilian

  • Guest
Re: Initial code in Setter and Getter
« Reply #1 on: October 24, 2005, 07:24:39 am »
You can easily change the code template to include the text.

andregatti

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Initial code in Setter and Getter
« Reply #2 on: October 25, 2005, 05:58:12 am »
which template do I have to modified? the operation body impl?
You have the stereotype <<property get>>, how do I included my code?

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: Initial code in Setter and Getter
« Reply #3 on: October 25, 2005, 06:10:34 am »
Quote
which template do I have to modified? the operation body impl?
You have the stereotype <<property get>>, how do I included my code?
In my C# CTF there's a stereotype overridden «property» template for OperationBody but how that's going to help you is unclear to me.

Paolo
« Last Edit: October 25, 2005, 06:11:35 am by PaoloFCantoni »
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

andregatti

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Initial code in Setter and Getter
« Reply #4 on: October 25, 2005, 06:27:51 am »
what is really strange, is that if you generate the code in java, it automatically includes the code (for example return <atribute name>), but if you generate the code in C# or C++, it doesn't...

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: Initial code in Setter and Getter
« Reply #5 on: October 25, 2005, 06:41:17 am »
Quote
what is really strange, is that if you generate the code in java, it automatically includes the code (for example return <attribute name>), but if you generate the code in C# or C++, it doesn't...
It's not really strange, Java accessors are not true property accessors in the C# sense.  If you search for Property Redux you'll see the theoretical reasons why this is the case.

Java accessors are just methods.  C# Properties are more than that.  (I assume you meant C#)  If not, then which language were you asking about?

HTH,
Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

andregatti

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Initial code in Setter and Getter
« Reply #6 on: October 25, 2005, 07:05:01 am »
I was asking about C++. I figured out how to do it.

First, you have to add the <<property get>> and <<property set>> stereotypes (Configuration-->UML-->stereotypes).
For example:
stereotype: property get
base class: class
notes: for a getter operation

Then go to Configuration-->Code Generation Templates...
Select C++ as language.
Select Operation Body as template.
Press the button Add New Stereotyped Override
Select property get from the Feature List.
Press OK Button.
It adds the property get to the Stereotype Overrides.
Then select Java, select Operation body, select property get from the Stereotype Overrides list, and copy the code and paste it into the C++, Operation Body, property get, that you have added.

Then do the same for the property set.

For other languages it is the same process, but the code template could be a little different.


Mythri

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Initial code in Setter and Getter
« Reply #7 on: October 27, 2005, 02:28:20 am »
Hi
Can we create code templates for opertions which are reused frequently apart from getter and setter? If yes, can anyone tell me how we can do this (implementation language is C++).

thomaskilian

  • Guest
Re: Initial code in Setter and Getter
« Reply #8 on: October 27, 2005, 02:31:01 am »
You can either stereotype these ops or you can have naming conventions. Based on the stereotype you can add stereotype overrides in the code templates. With naming you have to modify the base template to decide over different code generation.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Initial code in Setter and Getter
« Reply #9 on: October 27, 2005, 03:51:25 pm »
Stereotyping makes things a lot easier in my experience.  Not only are the templates simpler, but you can create a profile with that stereotype, have it showing on the toolbar and every time you use it the appropriate tags are already added.

Of course profiles are a slightly more advanced topic, but very useful once you're used to them.