Solved

Alternate Class and Late Binding

Posted on 2014-12-05
4
108 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:Starr Duskk
  • 2
  • 2
4 Comments
 
LVL 36

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 2

Author Comment

by:Starr Duskk
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 36

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 2

Author Closing Comment

by:Starr Duskk
ID: 40485768
thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

713 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