• Saves Valuable Time
  • Trusted Accuracy for 12 Years
  • 15-Day Money-Back Guarantee

VB Equivalent to Java Anonymous Inner Classes

The closest equivalent to Java anonymous inner classes in VB is to use a private class which implements the corresponding interface (but if the interface is a functional interface, then the closest equivalent is to replace the functional interface with a delegate and the anonymous inner class with a lambda).

Java VB
public class TestClass
{
    private void TestMethod()
    {
        MyInterface localVar = new MyInterface()
        {
            public void method1()
            {
                someCode();
            }
            public void method2(int i, boolean b)
            {
                someCode();
            }
        };
    }
}
Public Class TestClass
    Private Sub TestMethod()
        Dim localVar As MyInterface = New MyInterfaceAnonymousInnerClass(Me)
    End Sub

    Private Class MyInterfaceAnonymousInnerClass
        Inherits MyInterface

        Private ReadOnly outerInstance As TestClass

        Public Sub New(ByVal outerInstance As TestClass)
            Me.outerInstance = outerInstance
        End Sub

        Public Overridable Sub method1()
            someCode()
        End Sub
        Public Overridable Sub method2(ByVal i As Integer, ByVal b As Boolean)
            someCode()
        End Sub
    End Class
End Class


Use Java to VB Converter to convert from Java to VB.NET.     

Additional resource: VB.NET and Java Equivalents


Copyright © 1997 – 2017 Tangible Software Solutions Inc.