473,406 Members | 2,217 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,406 software developers and data experts.

Q: Yes its another interface question for a dummy like me.

Hi!

I realy dont get the point of interfaces

If i have this code

Public class MyClass
{
Public MyClass()
{
}

Public string Text
{
Get { return xxx;}
Set { xxx = value;}
}
}

why on earth would i need or should i use an interface like this
interface MyInterface
{
stinrg Text
{
get;
set;
}
}

and declare myclass : myinterface?

I just don't see the point of doing this. I do the code once more anyhow in
the class...

Yes i am stupid, thats why i am asking ;)

Regards
Martin
Nov 24 '06 #1
3 1266
Martin Arvidsson (Visual Systems AB) wrote:

<snip>
why on earth would i need or should i use an interface like this
interface MyInterface
{
stinrg Text
{
get;
set;
}
}

and declare myclass : myinterface?
Because then in various cases you could write the calling code to only
know that it needed an implementation of MyInterface, rather than your
concrete class.

Look at IDisposable - all kinds of classes need to implement it, and
using an interface means they don't all need to derive from the same
concrete class.

Jon

Nov 24 '06 #2
Hi,

In this specific situation it wouldn't be interesting at all.

It would become interesting as soon as you have more controls that all
have a 'Text' property, and you'd have to write a method that works on
objects with a Text property.

Consider the following, this will only work if both classes implement
IMyInterface:

public class MyClass3{
public method Test(){
WriteText(new MyClass2());
WriteText(new MyClass3());
}
public method WriteText(IMyInterface interface){
Console.WriteLine(interface.Text)
}
}

public class MyClass2 {
string _test;
public string Text{get{return _text}};
}

public class MyClass3 {
string _test;
public string Text{get{return _text}};
}

interface IMyInterface
{
string Text
{
get;
}
}

but there are so many design considerations to use interfaces, I'd start
looking at how some design patterns and best practices work:
http://codebetter.com/blogs/david.ha...croKernel.aspx
http://en.wikipedia.org/wiki/Design_Patterns
http://en.wikipedia.org/wiki/Law_of_Demeter

Regards,

Wiebe Tijsma
http://www.e-office.com

Martin Arvidsson (Visual Systems AB) wrote:
Hi!

I realy dont get the point of interfaces

If i have this code

Public class MyClass
{
Public MyClass()
{
}

Public string Text
{
Get { return xxx;}
Set { xxx = value;}
}
}

why on earth would i need or should i use an interface like this
interface MyInterface
{
stinrg Text
{
get;
set;
}
}

and declare myclass : myinterface?

I just don't see the point of doing this. I do the code once more anyhow in
the class...

Yes i am stupid, thats why i am asking ;)

Regards
Martin

Nov 24 '06 #3
Hi!
>
I realy dont get the point of interfaces

If i have this code

Public class MyClass
{
Public MyClass()
{
}

Public string Text
{
Get { return xxx;}
Set { xxx = value;}
}
}

why on earth would i need or should i use an interface like this
interface MyInterface
{
stinrg Text
{
get;
set;
}
}

and declare myclass : myinterface?

I just don't see the point of doing this. I do the code once more anyhow in
the class...

Yes i am stupid, thats why i am asking ;)

Regards
Martin
When you have just that one class, probably you don't need that
interface.
Interfaces are for situations like this:
* you have two (or more) classes, say MyClass and MyOtherClass. Both
contain a Text property.
* for some reason they can not be derived from the same baseclass
* you have some method that takes some object and does something to
it's Text property

then you could declare an interface:
interface IHasTextProperty
{
string Text
{
get;
set;
}
}

declare your classes MyClass and MyOtherClass to implement that
interface:
class MyClass: IHasTextProperty
{}
then you can create that method as
public void AddText(IHasTextProperty TheClass, string NewText)
{
TheClass.Text = NewText;
}

and use it like
MyClass x = new MyClass();
AddText(x, "foo");
MyOtherClass y = new MyOtherClass();
AddText(y, "bar");
an interface is often referred to as a "contract": the compiler now
knows that a class that implements that interface does have these
methods and/or properties (and doesn't care about any other
properties).

A real life example would be IDisposable: a class that implements this
can (and should) be Dispose()d.

Hans Kesting
Nov 24 '06 #4

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

Similar topics

10
by: HB Kim | last post by:
Hello, What could possibly cause data in the SQL server database to be removed, except being deleted manually? We had a couple of situations where data in certain records disappeared although the...
10
by: Lorenzo J. Lucchini | last post by:
Do you see any counter-indication to a token extractor in the following form? typedef int token; token ExtractToken(const char** String, char* Symbol); If it finds a valid token at the start...
0
by: yccheok | last post by:
I try to write a c implementation of java abstract class which serve the same purpose. however, I feel that there might be some potential holes in the future especially when I plan to scale up this...
5
by: cs | last post by:
We have a VB.NET interface that I just dont have the time to rewrite at the moment, and I need to implement it on C#, I have done it already however I get the following error when referencing my...
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...
9
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must...
1
by: pemigh | last post by:
I experienced the same problem that I found posted from summer 2005 (apparently too long ago to continue the thread). See below for full description. I found that reinstalling Access 2000 did...
2
by: Gavin Sullivan | last post by:
How would you (if you can?) code the following VB.Net in C#? public interface IDummy sub SomeMethod() end interface public class Dummy implements IDummy public sub DifferentName()...
0
by: Ken | last post by:
Hi I have a little application that does datavalidation. It supports dynamically loaded plugins (you drop a dll with a class implementing IValidator<Tin the same dir as the main application)....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.