Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AddressOf in base class

Tags:

.net

oop

vb.net

When referencing a method's address, should we take into account the overriding or not?

Class B 
  Inherits A

  Overrides Sub Foo
     Console.WriteLine("B")
  End Sub
End Class


Class A
  Public Sub PFoo
  ... AddressOf Foo ... ' WHAT WILL DO THIS METHOD??? '
  End

  Protected Overridable Sub Foo()
     Console.WriteLine("A")
  End Sub
End Class
like image 761
serhio Avatar asked May 07 '26 22:05

serhio


2 Answers

It will print B. To make it more obvious on what your intent is, you could put AddressOf Me.Foo. Also, just as an FYI, if you put MyClass.Foo, it will print A

    Module Module1

    Sub Main()

        Dim b As B = New B
        b.PFoo() ' prints B
        Console.ReadLine()

    End Sub

End Module

Public Class B
    Inherits A

    Protected Overrides Sub Foo()
        Console.WriteLine("B")
    End Sub
End Class

Public Class A
    Public Sub PFoo()
        Dim f As Action = New Action(AddressOf Me.Foo)
        f.Invoke()
    End Sub

    Protected Overridable Sub Foo()
        Console.WriteLine("A")
    End Sub
End Class
like image 126
Justin Largey Avatar answered May 10 '26 11:05

Justin Largey


I believe that PFoo will reference Foo that is overridden in the inheriting class, if PFoo is called within the inheriting B class's/object instance.

like image 36
Kon Avatar answered May 10 '26 13:05

Kon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!