newbie question about Interfaces

  • Thread starter Thread starter John Mas
  • Start date Start date
J

John Mas

Having read up about interfaces i have a rough idea about the hows but still
a little unsure of why or when.

if i have a class with a number of properties and public routines could i
and should i be using interfaces to define what are the mandatory ones.

My actual scenario is a child form that can be used to either add a new
record or edit an existing record. So I have a friend variable m_State
which i set from the parent form to be either _addNew or _edit. If i'm
editing the form i need to populate it with specific data so i am using
another friend variable m_iResID. So in this scenario if i have opted to
edit i need to ensure that the m_iResID is also set so would i put the two
into an interface?

I know that it is a slightly clumsy example and i could do it differently
but the main thing is to understand why and when about interfaces.

Cheers

John
 
John,

It is a bit hard to explain the use of interfaces in a short email, but I
will try using a simple approach.

First, the idea of an Interface is that it is a contract between two parts.
Someone who will develop a class or set of classes and someone (or some
other classes) that will use those classes. If we can agree on that, here's
a nice example:

Suppose you are writing an application to be used in ATMs for a specific
bank. Your application will need to allow users to check their balance for
bank accounts. You know that there are several types of bank accounts, but
again, all you care about in your app is to be able to access the balance.

Since those different types of account maybe implemented in different
classes, they may use a different approach on how to exposed the balance. In
this case, you can agree upon a contract with the developers responsible for
creating and maintaining those classes to implement an Interface that will
allow you to access the balance. If all bank account classes (checking,
savings, etc..) implement your interface you now know you will be able to
access the balance in the same way, for every account type.

Does that make any sense to you?

Telmo Sampaio
 
Telmo,

thanks for your explanation.
I understand the the first bit, the contract, I think I understand the last
bit. I've also just had a reread of my book so lets see if i'm getting
there.

In order to be effective we, the developers on the project and all the
classes involved must have access to the interface to see what it contains.
eg
Interface iBankAccount
function CheckBalance( ) as decimal
property ChargeFee() as decimal
end Interface

so if you write a class that implements this interface I can use this class
provided that i know what the interface looks like. So I know that you have
a checkbalance function and whilst i don't know what it does or even if it
returns the correct value I can at least be sure that it will return a
decimal value.

So in the real world would you treat the class as a black box and the only
publically available Properties and Methods would be those that are
documented in the interface(s). Does this mean that if i use an interface i
will get an error if i don't implement each declared P & M. Or are we just
saying that it is the agreed way of documenting those Ps & Ms that are
necessary to use the class.

John
 
Back
Top