Solved

Alternate Class and Late Binding

Posted on 2014-12-05
4
110 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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