Click here to Skip to main content
15,884,298 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W16-Sep-22 21:54
Otto_W16-Sep-22 21:54 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff16-Sep-22 22:13
mveOriginalGriff16-Sep-22 22:13 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W24-Sep-22 1:09
Otto_W24-Sep-22 1:09 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff24-Sep-22 1:34
mveOriginalGriff24-Sep-22 1:34 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W24-Sep-22 2:45
Otto_W24-Sep-22 2:45 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff24-Sep-22 3:16
mveOriginalGriff24-Sep-22 3:16 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W25-Sep-22 21:20
Otto_W25-Sep-22 21:20 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff25-Sep-22 22:01
mveOriginalGriff25-Sep-22 22:01 
OK, those aren't properties: they are fields and it's considered bad practice to expose fields directly.
And the problems with fields are that they can be changed at any time, and your derived classes don't have to do anything with them - like giving them a value!

Creating abstract properties gets round both of those: the derived class must implement the property - which means it must return a relevant value - and it can be read only.
Here's my version of your code:
C#
public abstract class Ship
    {
    public abstract string Name { get; }
    public abstract int Length { get; }
    }
public class Destroyer : Ship
    {
    public override string Name { get => "Destroyer"; }
    public override int Length { get => 2; }
    }
Because the properties are marked abstract, every time your derive a class from Ship it must implement it or your code won't compile!
Because I don't specify a setter in the abstract class property definition you can't add a setter to the derived class either, so the property cannot be changed by the outside world.
The derived class Destroyer "knows" how long it should be, and what the type of ship is called, so it implements that in the getter - the outside world doesn't need to know anything about it, the Destroyer class provides it as needed.
If you haven't seen this syntax before:
C#
public override string Name { get => "Destroyer"; }
It's just a "short form" of this:
C#
public override string Name
    {
    get
        {
        return "Destroyer";
        }
    }
Each of your derived classes implements it's own version, and the system uses the right one later:
C#
private void MyButton_Click(object sender, EventArgs e)
    {
    Destroyer d1 = new Destroyer();
    Cruiser c1 = new Cruiser();
    ShowMe(d1);
    ShowMe(c1);
    }
private void ShowMe(Ship s)
    {
    Console.WriteLine($"A {s.Name} that is {s.Length} long.");
    }

Prints:
A Destroyer that is 2 long.
A Cruiser that is 3 long.


You are letting Ships handle themselves instead of needing information to set be in your main code.
For example, if you wanted to add a helicopter class:
 *
***
 *
You don't have to change your main code at all: it doesn't "need to know" that it's a weird shape, the new Helicopter class would deal with it, and it's just another Ship as far as your code is concerned:
C#
public class Helicopter: Ship
    {
    public override string Name { get => "Helicopter"; }
    public override int Length { get => 5; }
    }

Does that make sense?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!

GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W29-Sep-22 16:23
Otto_W29-Sep-22 16:23 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff29-Sep-22 19:10
mveOriginalGriff29-Sep-22 19:10 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W4-Oct-22 0:29
Otto_W4-Oct-22 0:29 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff4-Oct-22 1:09
mveOriginalGriff4-Oct-22 1:09 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W13-Oct-22 17:45
Otto_W13-Oct-22 17:45 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W13-Oct-22 17:46
Otto_W13-Oct-22 17:46 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W25-Oct-22 17:16
Otto_W25-Oct-22 17:16 
GeneralRe: C# Battleship gameboard & random assignment Pin
OriginalGriff25-Oct-22 19:34
mveOriginalGriff25-Oct-22 19:34 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W30-Oct-22 19:30
Otto_W30-Oct-22 19:30 
GeneralRe: C# Battleship gameboard & random assignment Pin
Otto_W15-Nov-22 14:46
Otto_W15-Nov-22 14:46 
AnswerRe: C# Battleship gameboard & random assignment Pin
Gerry Schmitz6-Sep-22 5:06
mveGerry Schmitz6-Sep-22 5:06 
QuestionChanging Windows registry with C# Pin
Ismael_199931-Aug-22 12:50
Ismael_199931-Aug-22 12:50 
AnswerRe: Changing Windows registry with C# Pin
Dave Kreskowiak31-Aug-22 14:37
mveDave Kreskowiak31-Aug-22 14:37 
GeneralRe: Changing Windows registry with C# Pin
Ismael_19993-Sep-22 4:18
Ismael_19993-Sep-22 4:18 
GeneralRe: Changing Windows registry with C# Pin
Dave Kreskowiak3-Sep-22 9:51
mveDave Kreskowiak3-Sep-22 9:51 
AnswerRe: Changing Windows registry with C# Pin
OriginalGriff31-Aug-22 18:51
mveOriginalGriff31-Aug-22 18:51 
GeneralRe: Changing Windows registry with C# Pin
Ismael_19993-Sep-22 4:20
Ismael_19993-Sep-22 4:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.