Solved

Alternate Class and Late Binding

Posted on 2014-12-05
4
103 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 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: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 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:Starr Duskk
ID: 40485768
thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

12 Experts available now in Live!

Get 1:1 Help Now