mosquito87
mosquito87

Reputation: 4440

Call the same method of multiple classes

I have a class where the same method of multiple objects (all objects are basically of different classes- but they're all derived from the same class):

public abstract class MyBaseClass
{
    public abstract void New();
}

public class MyRealClass
{
    public MyBaseClass Object1;
    public MyBaseClass Object2;
    public MyBaseClass Object3;

    public void MyMethod()
    {
        Object1.New();
        Object2.New();
        Object3.New();
    }
}

I wonder if there's a cleaner way doing that because it looks like a code smell to me that I call the same method three times. A collection of Object1 - Object3 would make sense. On the other hand I need those Objects out of an collection because my view (WPF) must directly access them.

Upvotes: 0

Views: 1427

Answers (3)

Benj
Benj

Reputation: 989

foreach(MyBaseClass c in new MyBaseClass[] { Object1, Object2, Object3 }) 
{
    c.New();
}

Or just store your Objects in an Array if you know you will end up having exactly n Objects. This way you even avoid creating a new Array each time you run the loop

MyBaseClass[] MyObjects = new MyRealClass[3];

for(Int32 i = 0; i < MyObjects.Length; i++)
[
    MyObjects[i] = new MyRealClass();
}

// ... snip

foreach(MyBaseClass c in MyObjects)
{
    c.New();
}

Upvotes: 0

Carlos
Carlos

Reputation: 243

Since you're using objects, all variables will hold references to the real objects. So you can have all three objects in a list and also have a variable with each one of them. If you update a variable for object 1, for example, and if later you iterate through the list, you will see the object has been updated.

class Program
{
    class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }

        public Person(int id, string name)
        {
            this.ID = id;
            this.Name = name;
        }
    }

    static void Main(string[] args)
    {
        Person person1 = new Person(1, "name1");
        Person person2 = new Person(2, "name2");

        List<Person> items = new List<Person>();
        items.Add(person1);
        items.Add(person2);

        Console.WriteLine(string.Format("Person 1...{0}, {1}", person1.ID, person1.Name));
        Console.WriteLine(string.Format("Person 2...{0}, {1}", person2.ID, person2.Name));
        foreach (Person p in items)
            Console.WriteLine(string.Format("Person from list...{0}, {1}", p.ID, p.Name));

        person1.Name = "name1 after";
        person2.Name = "name2 after";

        Console.WriteLine(string.Format("Person 1 after...{0}, {1}", person1.ID, person1.Name));
        Console.WriteLine(string.Format("Person 2 after...{0}, {1}", person2.ID, person2.Name));
        foreach (Person p in items)
            Console.WriteLine(string.Format("Person from list after...{0}, {1}", p.ID, p.Name));

        Console.ReadKey();
    }
}

Upvotes: 0

Omid Jalali
Omid Jalali

Reputation: 19

You could get list of properties of MyRealClass which are driven class of MyBaseClass and cast and then run the method (New).

Upvotes: 1

Related Questions