Solved

Alternate Class and Late Binding

Posted on 2014-12-05
4
107 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 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 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 2

Author Closing Comment

by:Starr Duskk
ID: 40485768
thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

861 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