1

New to the forum, and I am stuck on the logic of creating an abstract class from a pretty basic function. I know how inherit and override works with abstract classes, but I can't figure out the logic behind converting this function to an abstract class. Any help would be appreciated.

To add to this, I am doing this to help me understand the fundamentals and view what other options or good practices I should apply. Thanks again for any help.

Code:

private readonly List<Shape> Square1 = new List<Shape>();

private void GenerateSquare()
{
    Shape newSquare = RandomSquareLocation();

    while (Square1.Any(square => square.X == newSquare.X && square.Y == newSquare.Y))
    {
        newSquare = RandomSquareLocation();
    }

    Square1.Add(newSquare);
}

private Shape RandomSquareLocation()
{
    int maxXPosition = pictureBox1.Size.Width / playerOne.Width;
    int maxYPosition = pictureBox1.Size.Height / playerOne.Height;

    return new Shape { X = random.Next(0, maxXPosition), Y = random.Next(0, maxYPosition) };
}

private readonly List<Shape> Square1 = new List<Shape>();

private void GenerateSquare()
{
    Shape newSquare = RandomSquareLocation();

    while (Square1.Any(square => square.X == newSquare.X && square.Y == newSquare.Y))
    {
        newSquare = RandomSquareLocation();
    }

    Square1.Add(newSquare);
}

private Shape RandomSquareLocation()
{
    int maxXPosition = pictureBox1.Size.Width / playerOne.Width;
    int maxYPosition = pictureBox1.Size.Height / playerOne.Height;

    return new Shape { X = random.Next(0, maxXPosition), Y = random.Next(0, maxYPosition) };
}

The GenerateSquare is just going to spawn a Square somewhere on the pictureBox. The reason at least for my point of view to why I need abstract class for this is. GenerateSquare function would need to be replemented over and over again if there a need for multiple different Squeares. The RandomSquareLocation function acts as a base for the position that the squares spawn

Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
Darke
  • 383
  • 1
  • 13
  • 3
    Hi and welcome, I'm afraid it is not at all clear what you are trying to do, nor how it relates to abstract methods/classes. – Jamiec Apr 12 '21 at 08:43
  • @Jamiec I hope this clarifies if not I am sorry for being a noob at this and asking wrong question or providing wrong code sry again – Darke Apr 12 '21 at 08:54
  • Can you post the class definitions as well? – Youp Bernoulli Apr 12 '21 at 08:56
  • From the looks of it, you do not need an abstract class for this. `GenerateSquare` is rather concrete. That would be different if you had a `GenerateShape` function, which you implement on different classes (ie. for Squares, Circles, etc) – PMF Apr 12 '21 at 08:58
  • I agree, theres nothing about this code screaming "needs abstract" to me either – Jamiec Apr 12 '21 at 09:00
  • @PMF ty for the answer I will delete the question as it seems that I was completely wrong. I just hope it wasnt a bad question dont want to clutter the forum with useless questions – Darke Apr 12 '21 at 09:03

1 Answers1

-1

I'm not sure I understand well your question.

But I think what you want to do is to have your method virtual.

A virtual method provides a default implementation. It CAN be overriden in the class that inherits from your base class.

An abstract method MUST be implemented by the class that inherits from your base class.

A similar answer was already given here

Nk54
  • 751
  • 7
  • 15