I have a base class named Broadcaster
that can broadcast any info (byte arrays) using TCP. Now I want to create a class named ScreenBroadcaster
that is derived from Broadcaster
and can broadcast screenshots.
But I faced the following problem. Class Broadcaster
has a method Broadcast(byte[] data)
that should be replaced in ScreenBroadcaster
with just Broadcast()
(it should take the data to broadcast from the screen). I can't figure out how to do it, please help me!
I know about the keyword override
, but it doesn't allow to change the signature of the method to override, so it doesn't work in this case.
CodePudding user response:
You can't implement exactly what you want. Why? Consider this sample code:
byte[] data = new byte[] { 0, 1, 2, 3 };
Broadcaster bcaster = new ScreenBroadcaster();
bcaster.Broadcast(data);
It is absolutely correct. Any reference to Broadcaster
can refer to ScreenBroadcaster
because it's derived from Broadcaster
. But ScreenBroadcaster
(if it's implemented as you want) hasn't got method Broadcast(byte[])
that is called here. What should the executing environment (.NET Framework) do? No idea. So what you want isn't allowed in C#.
But why don't you leave method Broadcast(byte[] data)
in ScreenBroadcaster
? You may even use it in Broadcast()
to avoid code duplication, as said in the comments.
public void Broadcast() {
byte[] data;
// Fill "data" with an image from screen
Broadcast(data);
}