473,382 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

C# Interface Implementation Question

Hi,

I am new to C# and have a question regarding interface implementation in C#. In the COM days, we can have multiple COM objects implementing the same interface and instantiating one of them depending on the business requirements. For instance:

public function GetSavingAccountBalance(str_AcctNo as string, int_BankID as Integer) as double

Dim objSavingAcct as IBankAccount

if ( ID_BANK_1 = int_BankID ) then

set objSavingAcct = CreateObject ("Bank1.SavingAccount.1")

elseif ( ID_BANK_2 = int_BankID ) then

set objSavingAcct = CreateObject ("Bank2.SavingAccount.1")

end if

GetSavingAccountBalance = objSavingAcct.Balance( str_AcctNo )

set objSavingAcct = nothing

End Sub

In this case, both Bank1.SavingAccount.1 and Bank2.SavingAccount.1 are ProgIDs for 2 COM objects, and they both implement IBankAccount interface. If there are changes in the business logic, all I need to do is to instantiate another object with a different ProgID (which can be retrieved from a configuration file). This way I do not have to recompile the client again and makes the architecture very flexible.

How do I do this in C#? Any help will be appreciated. Thanks in advance.

Cheers,
Soon
Nov 15 '05 #1
1 4871
Hi,

You could define an interface, then have several classes to implement it, doing this you will get the same functionality.
But you can go a step further, though. You can declare in the app configuration file the class to be instantiate and the dll location and at runtime you can use it instead of using a multiple if construction ( in this way you do not have to hard code the different possibilities) , take a look at the code below:

Assembly ass = Assembly.LoadFile( Config.DataTransformatorLocation);

dataStrategic = (BaseTransformation) ass.CreateInstance( Config.DataTransformatorFQN);

Here I load an assembly and then instantiate a class declare in it that I know implement a defined interface. Config is a class that provide static properties to access configuration settings, DataTransformatorLocation return the path of the dll and DataTransformatorFQN return the Fully qualified name of the type.

Hope this help,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Soon" <ys*******@yahoo.com> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

I am new to C# and have a question regarding interface implementation in C#. In the COM days, we can have multiple COM objects implementing the same interface and instantiating one of them depending on the business requirements. For instance:

public function GetSavingAccountBalance(str_AcctNo as string, int_BankID as Integer) as double

Dim objSavingAcct as IBankAccount

if ( ID_BANK_1 = int_BankID ) then

set objSavingAcct = CreateObject ("Bank1.SavingAccount.1")

elseif ( ID_BANK_2 = int_BankID ) then

set objSavingAcct = CreateObject ("Bank2.SavingAccount.1")

end if

GetSavingAccountBalance = objSavingAcct.Balance( str_AcctNo )

set objSavingAcct = nothing

End Sub

In this case, both Bank1.SavingAccount.1 and Bank2.SavingAccount.1 are ProgIDs for 2 COM objects, and they both implement IBankAccount interface. If there are changes in the business logic, all I need to do is to instantiate another object with a different ProgID (which can be retrieved from a configuration file). This way I do not have to recompile the client again and makes the architecture very flexible.

How do I do this in C#? Any help will be appreciated. Thanks in advance.

Cheers,
Soon
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paul Fame | last post by:
Hello World, This is not a flame, but a question about the fundamentals of the language. Unlike some languages, C++ requires class member functions to be declared twice: once in the class...
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
175
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be...
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
13
by: John Salerno | last post by:
Hi all. I have a question about interfaces now. According to the book I'm reading, when you implement an interface, the class or structure has to declare all the methods that the interface...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
8
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
2
by: psandler70 | last post by:
All, This might be a stupid question. When defining an interface, is it possible to force the class that implements it to support a property/method with the same name, but not force a...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
1
by: =?Utf-8?B?Sk0=?= | last post by:
In an application I have an interface with methods and properties. The interface is used on a Class (ie class MyClass : IMyClassA, IMyClassB). On a windows form I define a BindingSource...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.