On 5/22/20 4:04 PM, Vinod K Chandran wrote:
On Friday, 22 May 2020 at 16:12:12 UTC, Steven Schveighoffer wrote:
On 5/22/20 9:10 AM, Vinod K Chandran wrote:
On Friday, 22 May 2020 at 12:21:25 UTC, rikki cattermole wrote:
if (Child child = cast(Child)parent) {
assert(child !is null);
}
Actually, problem occurs in addHandler function. It expects an
argument of type "EventArgs", not MouseEventArgs.
Yes, because what if you did this with your function:
fnp(new EventArgs(...));
It would be called with the type being implicitly cast to the child
type without that being true.
What Rikki was recommending is that you write your handler like this:
void onClick(EventArgs e){
if(auto me = cast(MouseEventArgs)e) {
log("form clicked on x = ", me.x, ", and y = ", me.y);
}
}
Actually, if you are certain it's a programming error for onClick to
be called with a different type of event args, I'd do:
void onClick(EventArgs e){
auto me = cast(MouseEventArgs)e;
assert(me !is null, "Error, onClick called with invalid event type");
log("form clicked on x = ", me.x, ", and y = ", me.y);
}
Thanks for the answer. I understand that, in D, derived class and base
class are not the same as in vb.net or any other language. (Please
correct me if i am wrong).
In vb.net, assume that i have a class setup like this--
Public Class Base
Public Property SampleInt As Integer
End Class
Public Class Child : Inherits Base
Public Property SampleDouble As Double
End Class
//Assume that i have a list of Base class like this--
Dim sampleList As New List(Of Base)
// Now, i can use this list like this--
sampleList.Add(New Child(10.5)) Is this possible in D without casting ?
Yes. What you cannot do is this (which I hope doesn't compile in VB.net,
but I wouldn't be surprised):
Dim sampleList As New List(Of Child)
sampleList.Add(New Base(10))
Which is the equivalent of what you were requesting.
-Steve