Solved

Alternate Class and Late Binding

Posted on 2014-12-05
4
102 Views
Last Modified: 2014-12-07
                        Dim daOrmCustom As New Object
                        If AdminFunctions.IsDevelopmentOrLocal Then
                            daOrmCustom = New DataAccess.CustomClass2015()
                        Else
                            daOrmCustom = New DataAccess.CustomClass2014()
                        End If
                        dataItem("Id").Text = daOrmCustom.GetOptions(CInt(IdStr))

Open in new window


here's what I want to do:
I have to rework an entire class with changes for 2015 and I want to leave the 2014 code in tact and only test with the 2015 code. So in my code, I have it check if we are in development or local, and if so, load the 2015 class, if not load the 2014 class. I have done it as above.

But when I actually use the class, it errors with "option strict on disallows late binding"

What is the best method of implementing the above scenario without any errors? I don't want to remove option strict on if there's another way.

thanks!
0
Comment
Question by:BobCSD
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 40483772
Does CustomClass2015 and CustomClass2014 share the same method signature?
If this is the case you can create a custom interface ICustomClass with all your common methods and this interface being inherited by CustomClass2015  and CustomClass2014. For example:
Public Interface ICustomClass 
    //your common methods here
End Interface

Public Class CustomClass2014
    Implements ICustomClass 
    //your imp here
End Class

Public Class CustomClass2015
    Implements ICustomClass 
    //your imp here
End Class

Open in new window

Then you can use it in your existing code as:
Dim daOrmCustom As ICustomClass
Then on you code you can call any of the common methods with ease.
0
 
LVL 1

Author Comment

by:BobCSD
ID: 40483790
yes, they share the same things. But how does it know which one to call?
 I don't see where you tell it to use 2015 vs 2014.

thanks.
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 40483980
LInes 3 and 5 assigns daOrmCustom to the required CustomClass implementation. It is based on interface-based programming as explained here.

For example say the interface contains the following method: GetOptions(ByVal Id As Integer) and CustomClass2015 and CustomClass2014 classes both implement ICustomClass, thus GetOptions method, based on this info a commented version of your code is shown below:
Dim daOrmCustom As ICustomClass //Declares the interface
If AdminFunctions.IsDevelopmentOrLocal Then 
      daOrmCustom = New DataAccess.CustomClass2015()  //Assigns daOrmCustom  to use CustomClass2015 implementation
Else
      daOrmCustom = New DataAccess.CustomClass2014()   //Assigns daOrmCustom  to use CustomClass2014 implementation
End If
dataItem("Id").Text = daOrmCustom.GetOptions(CInt(IdStr)) //Use GetOptions method of the assigned instance.

Open in new window

0
 
LVL 1

Author Closing Comment

by:BobCSD
ID: 40485768
thanks!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

19 Experts available now in Live!

Get 1:1 Help Now