2

I'm wondering if there is a way to access the different methods in the objects that I have in the code below?

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            _test[0] = new TallGuy() {Height = 74, Name = "Teddy Long"};
            _test[1] = new TallGuy() {Height = 64, Name = "Teddy Shorter"};
            _test[2] = new TallGuy() {Height = 54, Name = "Teddy Shortest"};
        }

        private readonly object[] _test = new object[3];

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < _test.Length; i++)
            {
                //_test[i].        I can't call any methods here...
            }
        }
    }
}

The reason behind me using the Object type instead of an array of one class is because I want to store different types of objects in an array. Midway through my testing though I noticed that I was unable to access the methods of the objects I had already stored in the array, hence why there is only one type of object in there.

2
  • Indeed, I was planning to add different types but stumbled upon this issue. Decided to try solve it first before I continue. Commented Mar 14, 2015 at 17:56
  • You need to cast the object to the appropiate type to access its methods Commented Mar 14, 2015 at 17:58

2 Answers 2

4

You can access the methods by checking the types and casting them:

var obj = _test[i] as TallGuy;
if (obj != null) Console.WriteLine("Height: {0}", obj.Height);

You can also use reflection.

However are the types of the objects related? Maybe you should consider creating a common interface o super class and define the array of objects of that type.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. This was a "Brain Power" question in the Head First C# book that I decided to take a look at. The question was the following "If you have some other class that doesn’t inherit from Worker but does implement the INectarCollector interface, then it’ll be able to do the job, too! But since it doesn’t inherit from Worker, you can’t get it into an array with other bees. Can you think of a way to get around the problem and create an array with both bees and this new class?"
This seem to be right up my alley this book :P Anyway. This is called polymorphism
2

You are only dealing with TallGuy objects in your array so you could use:

TallGuy[] _test = new TallGuy[3];

Which would provide you access to the TallGuy properties.

If you are also dealing with other object types such as SmallGuy then you would be better off having a object hierarchy and then having a strongly typed list rather than using object.

For instance:

public abstract class Guy 
{
     abstract public int Height { get; set; }
}

public class TallGuy : Guy 
{
    public override int Height 
    {
        get { return 100; }
        set { } 
    }
}

public class ShortGuy : Guy 
{
    public override int Height 
    {
        get { return 10; }
        set { }
    } 
}

With this structure in place you could have a List<Guy> and then let Contravariance/Covariance take over.

List<Guy> people = new List<Guy>();
people.Add(new TallGuy());
people.Add(new ShortGuy());

An alternative approach is to cast the object to the type within the foreach

 var tallGuy = _test[i] as TallGuy;

And then check if the cast was successfully by checking if it is null:

if (tallGuy != null) {

}

(But you would not have a strongly typed list and could hit performance problems from boxing the objects and then casting back).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.