473,480 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Identify Interface in ArrayList of Intfaces

Sam
Hi

I am trying to create a arraylist of Intefaces so I only need to create each
once and I would like to
be able to identify the specific interface when I loop thru ???

Is there some way to set a Custom Attribute on each Instance of the
Interface???

Thanks
public interface IAnimal
{
bool Print();
}

public class Dog : IAnimal
{
public bool Print()
{
Console.WriteLine("Dog");
return true;
}
}

public class Cat : IAnimal
{
public bool Print()
{
Console.WriteLine("Cat");
return true;
}
}

public class PROGRAM
{
static public void Main()
{

ArrayList myArrList = new ArrayList();
myArrList.Add(new Dog());
myArrList.Add(new Cat());

foreach (IAnimal thing in myArrList)
{
//How to tell What IAminal is???
}

}

}
Mar 11 '06 #1
3 2549
>foreach (IAnimal thing in myArrList)
{
//How to tell What IAminal is???
}


You can use the is operator to check if an object is of a certain
type.

if (thing is Dog) ...

But isn't the whole point of using the IAnimal interface that you
shouldn't have to care what kind of animal it is?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 11 '06 #2
Sam
You are correct but at some point you need to pick an interface depending
on incoming data

if (dog data)
myAnimal = new (dog)
if(cat data)
myAnimal = new(cat)

......

for (int i =0; i<1000000; i++)
{
if (dog data)
myAnimal = new (dog)
if(cat data)
myAnimal = new(cat)

......

}

if I have to do this in a loop a million times and I have some significant
i/o needed on the constructors
then i was thinking of holding onto the interface - I guess when I do "new"
I can add to a hastable with
a type as the key instead of an arraylist and try to find type

Thanks

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl...
foreach (IAnimal thing in myArrList)
{
//How to tell What IAminal is???
}


You can use the is operator to check if an object is of a certain
type.

if (thing is Dog) ...

But isn't the whole point of using the IAnimal interface that you
shouldn't have to care what kind of animal it is?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Mar 12 '06 #3
> You are correct but at some point you need to pick an interface
depending on incoming data

if (dog data)
myAnimal = new (dog)
if(cat data)
myAnimal = new(cat)
Unless you need to do specific processing of cats and dogs in this code,
myAnimal can be of type IAnimal.
.....

for (int i =0; i<1000000; i++)
{
if (dog data)
myAnimal = new (dog)
if(cat data)
myAnimal = new(cat)
.....

}

if I have to do this in a loop a million times and I have some
significant
i/o needed on the constructors
then i was thinking of holding onto the interface - I guess when I do
"new"
I can add to a hastable with
a type as the key instead of an arraylist and try to find type


What interface would you like to hold on to and why? Before you construct
the object you don't have an interface to it and if you only need to handle
it as an IAnimal afterwards then you don't need to know which specific one
you have.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Mar 13 '06 #4

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

Similar topics

3
1767
by: Erik Harris | last post by:
I apologize if this is a stupid question - I'm relatively new to OOP. I have a property that must exist in a class in order to be used by another class. The property, however, does not change with...
21
13780
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
10
2941
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract...
5
3160
by: Joe Black | last post by:
Hi all, Using Windows CP Pro VS .net 2005 I'm creating an app that allows user to extend its functionality by installing plug in modules, these modules support an interface I have created...
6
1918
by: Ricky W. Hunt | last post by:
It's dawning on my a lot of my problems with VB.NET is I'm still approaching it in the same way I've programmed since the late 70's. I've always been very structured, flow-charted everything, used...
11
3926
by: Andy | last post by:
Make the story short, I have a VB.NET client interface calling .NET webservice, written in VB.NET as well. I am trying to make the client as thin as possible so I let the webservice part to...
7
1209
by: tshad | last post by:
I am trying to understand why I would use interfaces. In the following example for IPrinciple, I have the following code: ************************************************************ using...
15
2766
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interfaceâ€. In a functional language, a function can be specified by its name and parameter specs....
6
150
by: Mike P | last post by:
I am looking through some code and have found a lot of methods that return an interface such as ICollection. What is the reason for this? If you return ICollection, does that mean that you can...
0
7044
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
7045
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,...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6944
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
5341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4782
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...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.