0

I need to implement this scheme. But I face a warning

S3459: "Unassigned members should be removed".

This is a tutorial code:

private readonly IEngine engine; *S3459*

public void SomeMethod(int params)
{
    this.engine.StartEngine(params);
}

And the scheme. enter image description here

Question: what exactly am I doing wrong?

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
DaRy
  • 23
  • 7
  • 1
    It seems that you never assign a value to `IEngine engine`, meaning that it will be `default(IEngine)` (i.e. `null`). If you were to ever call `SomeMethod(someNumber);` then you would get a [`NullReferenceException`](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). – ProgrammingLlama Nov 28 '21 at 14:27
  • That diagram is not UML. And the shared aggregation you are using from UML has no defined semantics. – qwerty_so Nov 28 '21 at 16:43

1 Answers1

0

Your code:

private readonly IEngine engine;

expects an engine to be initialized in the declaration or the constructor, because the field is readonly. This is why you get the warning

Moreover IEngine is an interface. An interface cannot be automatically instantiated as it is only a contract. You must initialize engine with an instance of a class that implements this interface.

Here an improved UML model with some comments/code that explain how it could work:

enter image description here

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Well, that left rectangle in that diagram is a plain rectangle and no UML notation. – qwerty_so Nov 28 '21 at 16:45
  • The fact is that this is not my scheme and I need to implement it somehow, but I do not quite understand the entry in my scheme in the class attributes: -engine : IEngine. I need to be able to switch between two types of engines and somehow do it via aggregation, but I do not understand how exactly in the main class I need to describe it – DaRy Nov 28 '21 at 18:05
  • do i need to use a constructor? – DaRy Nov 28 '21 at 18:07
  • @DaRy without constructor, how ho you want to initalize engine? I showed the constructor in my diagram, but you're not obliged to. Very often the detailed plumbing is left out from the class diagrams because it's obvious that classes need some constructor. In your code, you cannot switch between different engines, because engine is readonly. The "aggregation" allows you to select between two kind of engines, but the readonly requires this to happen at construction. Then, either you pass an engine to the constructor (what I did in the main() for demo), or you create one in the constructor – Christophe Nov 28 '21 at 18:42
  • but if you decide which engine to use in the constructor, you'd not make use of the interface (and have a hidden coupling). – Christophe Nov 28 '21 at 18:44
  • @DaRy the whole thing with an interface like IEngine, is that you do not hardwire the real class of the engine. Your class can use an object of any class that realizes the interface (in my diagram, I've added two, for illustration). – Christophe Nov 28 '21 at 18:46