Single responsibility principle
When talking about SOLID principles, we will start off with the single responsibility principle (SRP). Here, we are actually saying that a class has a specific task that it needs to fulfill and it should not do anything else.
Getting ready
You will create a new class and write code to log an error to the database when an exception is thrown on adding more troops to the star ship, causing it to be over capacity. For this recipe, ensure that you have added using System.Data;
and using System.Data.SqlClient;
namespaces to your application.
How to do it...
- Create a new class called
StarShip
:
public class Starship { }
- To your class, add a new method that will set the maximum troop capacity of the
StarShip
class:
public void SetMaximumTroopCapacity(int capacity) { }
- Inside this method, add a
trycatch
clause that will attempt to set the maximum troop capacity, but for some reason, it will fail. Upon failure...