15,942,401 members
5.00/5 (1 vote)
See more:
Hi guys!

How are you?
We are from Argentina
Sorry but my english isn't very good.

I have got an exercise to resolve with OOP and we are looking for the best way to solve it

The problem to solve is count the number of instances of any subtype (Car, Truck) in a supertype collection (Vehicle)

http://img808.imageshack.us/img808/9120/problemni.png[^]

```IList<Vehicle> vehicles = new List<Vehicle>();

Car aCar = null;
Truck aTruck = null;

aCar = new Car();

aCar = new Car();

aCar = new Car();

aTruck = new Truck();

aTruck = new Truck();

// 3 cars
// 2 trucks

int cars = 0;
int trucks = 0;

foreach(Vehicle v in vehicles) {
if(v is Car){
cars++;
} else if(v is Truck){
trucks++;
}
}```

is there a better way to solve this problem, without using clause "is Type"?
Cheers!
Fernando Lopez Guevara
Posted
Dalek Dave 10-Sep-10 3:26am
Your English is perfectly understandable my friend.

## Solution 2

I think in order to have counters you need to get type of each of them atleat once. Thus your code:
C#
```foreach(Vehicle v in vehicles)
{
if(v is Car)
{
cars++;
}
else if(v is Truck)
{
trucks++;
}
}```

Looks fine. :thumbsup:

## Solution 3

May be this alternate helps you

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{

class Vehicle
{
public static int Count = 0;
public Vehicle()
{
Count++;
}
}

class Car :Vehicle
{
public static int Count = 0;
public Car()
{
Count++;
}
}

class Truck : Vehicle
{
public static int Count = 0;
public Truck()
{
Count++;
}
}

class Class3
{
public static void Main()
{
Vehicle V1 = new Car();
Vehicle V2 = new Car();

Vehicle V3 = new Truck();

Console.WriteLine("No of Cars =" + Car.Count);
Console.WriteLine("No of Truck =" + Truck.Count);
Console.WriteLine("No of Vehicle=" + Vehicle.Count);

}
}
}```

## Solution 5

i could it! and i want to share the code with you =)

```using System.Collections.Generic;
using System;
namespace Visitor {
public interface IVisitor {
void Visit(Car c);
void Visit(Truck t);
}
public class PaintVisitor : IVisitor {
public void Visit(Car car) {
car.Paint("red");
}
public void Visit(Truck truck) {
truck.Paint("blue");
}
}
public abstract class Vehicle {
private string _color = "Black";
public void Paint(string color) {
this._color = color;
}
public abstract void Accept(IVisitor v);
public override string ToString() {
return string.Format("{0} - {1}", this.GetType(), this._color);
}
}
public class Car : Vehicle {
public override void Accept(IVisitor v) {
v.Visit(this);
}
}
public class Truck : Vehicle {
public override void Accept(IVisitor v) {
v.Visit(this);
}
}
public class Program {
public static void Main(string[] args) {
List<Vehicle> vehicles = new List<Vehicle>();
vehicles.ForEach(x => Console.WriteLine(x));
PaintVisitor pVisitor = new PaintVisitor();
vehicles.ForEach(x => x.Accept(pVisitor));
vehicles.ForEach(x => Console.WriteLine(x));
}
}
}```

thanks everyone

## Solution 4

Khaniya thanks!

Now if i wanna paint all cars but no the trucks?

```IList<Vehicles> vehicles = ......

foreach(Vehicle v in vehicles) {
if(v is Car){
v.Paint("Red");
}
}```

the point isn't ask for the type of instance, i can't solve it!!

my teacher told me, that the solution for this problem is "Double Dispatching" (Visitor) but i don't know how implement this pattern, any idea?

Chris Trelawny-Ross 10-Sep-10 18:00pm
Sound like you're asking us to solve a problem your teacher has set you. Perhaps asking one of your fellow students what the teacher said in class about Double Dispatch would be the way to go?
Chris Trelawny-Ross 10-Sep-10 18:05pm
I also recommend Googling Double Dispatch and Visitor Pattern. If you really want to learn OO programming, reading the Wikipedia articles, and figuring out what they mean, will serve you far better in the long run than having one of us provide the answer to a test your teacher has set.
Khaniya 11-Sep-10 6:12am
Hi Fernando Lopez
thought I do not have more experience with Designing
but I think you are miss guided.
It is not Possible by Visitor design that only Car object is painted
anyway if you get solution put it here

Thanks

## Solution 6

When you are counting Car or Truck, it is fine with your solution. "is" operator is quite capable of determining the type of an object.

For more simplistic an On demand logic, you can also use LINQ to filter out the element from the list.

`int cars = vehicle.Where(item => item is Car).Count;`

Here Where allow you to filter elements based on its type as well.

Another implementation could be use of ObservableCollection.

ObservableCollection notifies when a new object is added or removed. You can handle CollectionChanged event to get your counter updated when a new object is added.