[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Can I use an Interface instead of Late Binding?

Posted on 2005-05-06
5
Medium Priority
?
225 Views
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)
        Next
    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
else
  frm = childFormTwo
end if
frm.initialize_validation()

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)
0
Comment
Question by:brokeMyLegBiking
  • 2
  • 2
5 Comments
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 1200 total points
ID: 13944463
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:
http://www.developer.com/lang/other/article.php/939411

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.


Bob
0
 
LVL 4

Author Comment

by:brokeMyLegBiking
ID: 13944589
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?
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 1200 total points
ID: 13944612
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?

Bob
0
 
LVL 14

Accepted Solution

by:
existenz2 earned 800 total points
ID: 13944895
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: http://www.c-sharpcorner.com/Language/BridgePatternsinCSRVS.asp).

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.
0
 
LVL 4

Author Comment

by:brokeMyLegBiking
ID: 13947472
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!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

831 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