26 Replies
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
I have a class that draws multiple shapes 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
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
this says different sstuff
nice
yeah it seems I am not using my interface at all
that link is safe right ?
I'm not sure any of that has to do with the implementation though
Also I'd just use the Microsoft docs
ok
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
ok so if we use my interface to declare it It should be IObstacles RBox = new Redbox(10, 10);
Sure
Not really how you'd use it
That's why I wanted to see an example implementation; that's a bad one
List<IObstacles> RBox = new List<IObstacles>();
is this also wrong ?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 useC# 7.3. Please use language version 9.0 or greater
Come on, don't just copy and paste
?
I mean seriously, the error is so obvious, why even complain about it
sorry I guess didnt know there where newer version of c#
not supporting 2019 visual studio anymore
You don't need a new version
I need .NET 7.x no ?
Just do
= new List<IObstacle>();
like you did beforenvm I am stupid
Yes but you should be using that anyway.
Don't use 4.8 or whatever. Don't.
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@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?you will want to add a
Position
property to your interfaceok
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.