Book a Demo

Author Topic: Implement C# property in interface  (Read 11103 times)

jepessen

  • EA User
  • **
  • Posts: 106
  • Karma: +1/-1
    • View Profile
Implement C# property in interface
« on: June 15, 2016, 07:26:06 am »
I want to implement a property in a C# interface, so when I generate the code I obtain something like:

Code: [Select]
public interface IMyInterface
{
   string MyString { get; set; }
}

and in inherited classes:

Code: [Select]
public class MyClass : IMyInterface
{
   public string MyString { get; set; }
}

I've tried to add as attributes of interface my properties



But when I generate the interface I obtain the following code:

Code: [Select]
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;



namespace MyNamespace {
public interface IMyInterface  {

}//end IMyInterface

}//end namespace MyNamespace

What's the correct way to implement properties in interfaces?

VKN

  • EA User
  • **
  • Posts: 187
  • Karma: +9/-1
    • View Profile
Re: Implement C# property in interface
« Reply #1 on: June 15, 2016, 09:53:30 am »
Did you try selecting a property on the dialog and fill up the Property field?

jepessen

  • EA User
  • **
  • Posts: 106
  • Karma: +1/-1
    • View Profile
Re: Implement C# property in interface
« Reply #2 on: June 18, 2016, 05:37:09 am »
This is what I set now:



And now this is the interface in EA:



And this is the code:

Code: [Select]
namespace VCS {
  public interface Repository  {

  string Remote{
    get;
    set;
  }
}//end Repository

}//end namespace VCS

The code seems correct now, thanks. But since it's the first time, I'd like to know if it's correct the UML class representation for the property, since I see the property twice.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Implement C# property in interface
« Reply #3 on: June 18, 2016, 06:51:38 am »
You don't see it twice. The upper is the property (the "storage") and the lower is the method you access it (it's marked with a <<property>> stereotype). This way the code generator knows that it must spit out the get/set. For different target languages the code will look different. Some languages even don't have explicit getter/setters.

q.

jepessen

  • EA User
  • **
  • Posts: 106
  • Karma: +1/-1
    • View Profile
Re: Implement C# property in interface
« Reply #4 on: June 18, 2016, 07:08:20 am »
Ok now I understand. Thanks for the explanation.