C
C#2y ago
Xellez

❔ Is this the right way to implement an Interface

interface IObstacles
{
void Draw(Graphics g);
}

class Redbox : IObstacles
{
Pen Pen = new Pen(Color.Red);

Random Random = new Random();
Position Position;

float width;
float height;

public Redbox(float x, float y)
{
Position = new Position(x, y);

width = Random.Next(30, 100);
height = Random.Next(30, 100);
}


public void Draw(Graphics g)
{
g.DrawRectangle(Pen, Position.X , Position.Y, width, height);
}
}

class Bluebox: IObstacles
{
Pen Pen = new Pen(Color.Blue);

Random Random = new Random();
Position Position;

float width;
float height;

public Bluebox(float x, float y)
{
Position = new Position(x, y);

width = Random.Next(30, 100);
height = Random.Next(30, 100);
}

public void Draw(Graphics g)
{
g.DrawRectangle(Pen, Position.X, Position.Y, width, height);
}

}

class VerticaLine : IObstacles
{
Pen Pen = new Pen(Color.Yellow);

Position Position;

float Radius;

public VerticaLine(float x, float y, float radius)
{
Position = new Position(x, y);
Radius = radius;
}

public void Draw(Graphics g)
{
g.DrawLine(Pen, Position.X, Position.Y, Position.X, Radius);
}
}

class HorizonaLine : IObstacles
{
Pen Pen = new Pen(Color.Green);

Position Position;

float Radius;

public HorizonaLine(float x, float y, float radius)
{
Position = new Position(x, y);
Radius = radius;
}

public void Draw(Graphics g)
{
g.DrawLine(Pen, Position.X, Position.Y, Radius, Position.Y);
}
}
interface IObstacles
{
void Draw(Graphics g);
}

class Redbox : IObstacles
{
Pen Pen = new Pen(Color.Red);

Random Random = new Random();
Position Position;

float width;
float height;

public Redbox(float x, float y)
{
Position = new Position(x, y);

width = Random.Next(30, 100);
height = Random.Next(30, 100);
}


public void Draw(Graphics g)
{
g.DrawRectangle(Pen, Position.X , Position.Y, width, height);
}
}

class Bluebox: IObstacles
{
Pen Pen = new Pen(Color.Blue);

Random Random = new Random();
Position Position;

float width;
float height;

public Bluebox(float x, float y)
{
Position = new Position(x, y);

width = Random.Next(30, 100);
height = Random.Next(30, 100);
}

public void Draw(Graphics g)
{
g.DrawRectangle(Pen, Position.X, Position.Y, width, height);
}

}

class VerticaLine : IObstacles
{
Pen Pen = new Pen(Color.Yellow);

Position Position;

float Radius;

public VerticaLine(float x, float y, float radius)
{
Position = new Position(x, y);
Radius = radius;
}

public void Draw(Graphics g)
{
g.DrawLine(Pen, Position.X, Position.Y, Position.X, Radius);
}
}

class HorizonaLine : IObstacles
{
Pen Pen = new Pen(Color.Green);

Position Position;

float Radius;

public HorizonaLine(float x, float y, float radius)
{
Position = new Position(x, y);
Radius = radius;
}

public void Draw(Graphics g)
{
g.DrawLine(Pen, Position.X, Position.Y, Radius, Position.Y);
}
}
26 Replies
ero
ero2y ago
The code style definitely isn't perfect, but this is certainly a way to implement interfaces. It's perfectly valid and a fine idea, it's just more important how you use the interface in your code
Xellez
XellezOP2y ago
I have a class that draws multiple shapes
public class Engine
{
MainForm Form = new MainForm();
Timer Timer = new Timer();
List<Ball> Balls = new List<Ball>();
List<Redbox> RBox = new List<Redbox>();
Random Random = new Random();

public void Run()
{
Form.Paint += Draw;
Timer.Tick += TimerEventHandler;
Timer.Interval = 1000/25;
Timer.Start();

Application.Run(Form);
}

private void Form_Paint(object sender, PaintEventArgs e)
{
throw new NotImplementedException();
}

void TimerEventHandler(Object obj, EventArgs args)
{

if (Random.Next(100) < 25)
{
var ball = new Ball(400, 300, 10);
Balls.Add(ball);
}

if (RBox.Count() < 2)
{
var rbox = new Redbox(100 * Random.Next(1,8), 100* Random.Next(0,6));
RBox.Add(rbox);
}

foreach (var ball in Balls)
{
ball.Move();
}

Form.Refresh();
}
void Draw(Object obj, PaintEventArgs args)
{
foreach (var ball in Balls)
{
ball.Draw(args.Graphics);
}

foreach (var rbox in RBox)
{
rbox.Draw(args.Graphics);
}
}}
public class Engine
{
MainForm Form = new MainForm();
Timer Timer = new Timer();
List<Ball> Balls = new List<Ball>();
List<Redbox> RBox = new List<Redbox>();
Random Random = new Random();

public void Run()
{
Form.Paint += Draw;
Timer.Tick += TimerEventHandler;
Timer.Interval = 1000/25;
Timer.Start();

Application.Run(Form);
}

private void Form_Paint(object sender, PaintEventArgs e)
{
throw new NotImplementedException();
}

void TimerEventHandler(Object obj, EventArgs args)
{

if (Random.Next(100) < 25)
{
var ball = new Ball(400, 300, 10);
Balls.Add(ball);
}

if (RBox.Count() < 2)
{
var rbox = new Redbox(100 * Random.Next(1,8), 100* Random.Next(0,6));
RBox.Add(rbox);
}

foreach (var ball in Balls)
{
ball.Move();
}

Form.Refresh();
}
void Draw(Object obj, PaintEventArgs args)
{
foreach (var ball in Balls)
{
ball.Draw(args.Graphics);
}

foreach (var rbox in RBox)
{
rbox.Draw(args.Graphics);
}
}}
I am doing an exercise about inheritance, interfaces and abstract classes. The Ball and engine class are both written already I am supposed to add obstacles and have the engine render both the balls and my added shapes
ero
ero2y ago
I don't see you using your interface anywhere W3S is always wrong The only stuff that site is competent about is HTML and CSS
Xellez
XellezOP2y ago
this says different sstuff nice yeah it seems I am not using my interface at all that link is safe right ?
ero
ero2y ago
I'm not sure any of that has to do with the implementation though Also I'd just use the Microsoft docs
Xellez
XellezOP2y ago
ok
ero
ero2y ago
You're not really sharing your actual task, so it's hard to give any sort of advice The interface and the classes which implement the interface are perfectly fine It's just the implementation of the usages that aren't clear
Xellez
XellezOP2y ago
ok so if we use my interface to declare it It should be IObstacles RBox = new Redbox(10, 10);
ero
ero2y ago
Sure Not really how you'd use it That's why I wanted to see an example implementation; that's a bad one
Xellez
XellezOP2y ago
List<IObstacles> RBox = new List<IObstacles>(); is this also wrong ?
ero
ero2y ago
I'd change it to List<IObstacles> Obstacles = new(); So you save all of your instances in that one list That's a more proper use
Xellez
XellezOP2y ago
C# 7.3. Please use language version 9.0 or greater
ero
ero2y ago
Come on, don't just copy and paste
Xellez
XellezOP2y ago
?
ero
ero2y ago
I mean seriously, the error is so obvious, why even complain about it
Xellez
XellezOP2y ago
sorry I guess didnt know there where newer version of c# not supporting 2019 visual studio anymore
ero
ero2y ago
You don't need a new version
Xellez
XellezOP2y ago
I need .NET 7.x no ?
ero
ero2y ago
Just do = new List<IObstacle>(); like you did before
Xellez
XellezOP2y ago
nvm I am stupid
Thinker
Thinker2y ago
Yes but you should be using that anyway. Don't use 4.8 or whatever. Don't.
FusedQyou
FusedQyou2y ago
The basic idea of an interface is having the ability to define methods and properties that a class needs to implement. An interface usually consists of anything that is relevant to a certain "topic", you could say. In this case any class defining IObstacles must implement Draw, so every class relevant to obstacles would use this interface. The point of this is to create abstraction and to make it easier to define behaviour to anything related to obstacles. You can now use IObstacles as a type in your method, and it would expose Draw for you to use. There is much more you can do with interfaces
Xellez
XellezOP2y ago
@Ero I have a small question as I am creating a list of the type IObstacles would I be able to access position of any obstacles or do I need to create method that does that?
ero
ero2y ago
you will want to add a Position property to your interface
Xellez
XellezOP2y ago
ok
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server