Can I use an Interface instead of Late Binding?

Posted on 2005-05-06
Last Modified: 2010-04-16
1)I have two child forms which both have a method with the same name and signature:
    Public Sub initialize_validation()
        For Each control As Control In util.get_controlTagCol(Me)
            controlValidation.Control_Validating(control, Me.DefInstance, ErrorProvider1, Me.DefInstance.btnAddDisabled, False)
    End Sub

2)I would like to call this method from a third, parent form based on a reference passed for one of the two child forms.

3)for example:

if somethingTrue then
  frm = childFormOne
  frm = childFormTwo
end if

4) I can accomplish this with late binding, if I turn off Option Strict. However, I would prefer not to have to use late binding. Does anyone know a better way? Perhaps with an Interface? (I have not used interefaces before, I have only read about them, and don't quite understand how to use them and if they will solve my issue here)
Question by:brokeMyLegBiking
    LVL 96

    Assisted Solution

    by:Bob Learned
    An interface is a contract.  It defines the rules and behaviors that a class must adhere to.  In that way, when a class implements an interface, then you know what to expect.  Data-binding is an example.  Anything that implements the IList interface can be used as a Datasource.

    Implementing Interfaces in VB .NET:

    When defining an interface, all you need to do is to define the properties, methods, and events that a class needs to implement:

    Public Interface ISomething

        Public Sub DoSomethingHere()
        Public Function GetSomethingBack() As String

    End Interface

    This interesting thing to note is that this is the C# topic area, but this question shows VB.NET code.  I made the assumption that you would like to accomplish this in VB.

    LVL 4

    Author Comment

    Well I knew that C# programmers would be smarter regarting Interface than VB.NET coders :).  You can answer in C#, or VB.NET either way I can understand.

    Can a class which impliments an Interface have more methods than are defined in the Interface?
    LVL 96

    Assisted Solution

    by:Bob Learned
    Yes, they can have more, but they can't have less.  All methods defined in the Interface will need to be implemented, or you'll get a compile-time error.

    BTW, I am a long-time VB/VB.NET programmer, learning C/C++/C#, so I guess I am a rare case, huh?

    LVL 14

    Accepted Solution

    Interfaces are the best way to solve your problem(s). From the info from what I read I suggest you to use a Bridge Pattern (More Info can be found here:

    You can add extra methods to your classes, but you will not be able to call them when using the interface. Eg:
    public interface Test {
    void Connect();
    void Disconnect();

    public class ClassA : Test {
    public ClassA() {}
    public void Connect() { }
    public void Disconnect() { }

    public class ClassB : Test {
    public ClassB() {}
    public void Connect() { }
    public void Disconnect() { }
    public void SendMessage() {}

    Test _test1 = new ClassA();
    Test _test2 = new ClassB();
    ClassB _classB = new ClassB();

    Using the given interface you have only two known methods which are defined in the interface and in your classes, namely "Connect" and "Disconnect". When you will call _test1 and/or _test2 you will only be able to use these 2 commands. But when calling _classB you will also be able to use "SendMessage". So adding extra methods to a class which implements an interface is possible, but the method won't show up when you call the instance from the interface, only when you make a direct instance from the class. You cant have less methods in your classes then in your interface though.
    LVL 4

    Author Comment

    it worked, it's alive! (evil laugh)

    awesome, I've been wondering how to do that for a while. So long ugly case statements and late binding!

    thx for your help and the sample. kudos!

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video discusses moving either the default database or any database to a new volume.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now