Reputation: 568
I have 2 DLL's which have 90% the same methods, each DLL is dealing with different external cards. what is the best way to organize my code so I can write less code: ex:
Call_Method_A(1); //1 is the dll/card number
public void Call_Method_A(int cardNumber)
{
if(cardNumber==1)
//call function from dll 1
else
//call functio from dll 2
}
Upvotes: 0
Views: 114
Reputation: 1366
Your proposal is good but it may complicate your methods with all this if then... flow control.
If you were able to refactor you code by extracting common code snippets, then you would be able to make separate methods for each card.
This way can reuse your code and keep the methods as simple as possible.
You can do it on the same dll or in separate ones and reference a common one.
Upvotes: 1
Reputation: 22511
Use polymorphism to solve this. You can create an interface that contains the methods that the classes share. Let the classes implement this interface.
Then you can create a variable of that interface and assign one of the classes to that variable. Afterwards, you can use the variable to access the class and call the methods.
This way, you avoid lots of conditional expressions like
If (cardNumber == 1) // ...
Upvotes: 2