0

Beginner here. I am trying to find a way how to access property of object in array without always using "array[0] as ClassName" when they are not the same type of objects. No need detailed answer. You can also just recomend/guide me to where should i read about these things, because maybe the answer will be in front of me but i wont be able to recognize it because i am beginner.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Vezba_9_object_ecercises
{
    class Program
    {
        static void Main(string[] args)
        {

            object[] books = new object[2];

            books[0] = new SomeBooks("Book_1", "Good", 22);
            Console.WriteLine((books[4] as SomeBooks).name); // working
           // Console.WriteLine((books[4].name); // not working, how to make it work

            books[1] = new OtherBooks("Book_2", "Bad", 2);
            Console.WriteLine((books[4] as OtherBooks ).name); // working
          //  Console.WriteLine((books[4].name); // not working, how to make it work

        }
    }
    public class SomeBooks
    {
        public SomeBooks(string _name, string _likability, int _numberOfPages)
        {
            name = _name;
            likability = _likability;
            numberOfPages = _numberOfPages;
        }

        public string name { get; set; }
        public string likability { get; set; }
        public int numberOfPages { get; set; }


    }
    public class OtherBooks
    {
        public OtherBooks(string _name, string _likability, int _numberOfAuthors)
        {
            name = _name;
            likability = _likability;
            NumberOfAuthors = _numberOfAuthors;
        }

        public string name { get; set; }
        public string likability { get; set; }
        public int NumberOfAuthors { get; set; }


    }
}
5
  • (books[4].name) that won't work since books[4] is an object and it had no name property by contract.. you will need to learn OOP more i think.. like instead of array of objects you could array of interfaces or abstract.. Commented Mar 1, 2018 at 1:15
  • 2
    You could define an interface that each class implements and then access properties of the common interface. Commented Mar 1, 2018 at 1:18
  • 1
    here an example. Commented Mar 1, 2018 at 1:20
  • Notice how your classes only differ by one property? You should make one of them the "super" or parent class, and derive the other one from the parent class and add the new property. Then you can store them in the array as the parent class and only as them when you need the child class. Commented Mar 1, 2018 at 1:20
  • Thaks a lot, clearly i will need to learn more OOP before doing things like this/ Thank you for your time. Commented Mar 1, 2018 at 1:35

2 Answers 2

2

You could make the SomeBook and OtherBook classes inherit from a Book base class (or interface). This would allow you to store them in an array whose type is Book[] instead of object[]. You could access the inherited properties this way, such as Name and Likability, without casting.

public class Book
{
    public string Name { get; set; }
    public string Likability { get; set; }
}

public class SomeBook : Book
{
    public int NumberOfPages { get; set; }
}

public class OtherBook : Book
{
    public int NumberOfAuthors { get; set; }
}

However I'd argue against the necessity of doing this. Ask yourself if this is over-designed. You could just have a single Book class that allows for those other values to be null.

public class Book
{
    public string Name { get; set; }
    public string Likability { get; set; }
    public int? NumberOfPages { get; set; }
    public int? NumberOfAuthors { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your type of array must be an interface and type of each element must be a class that inherit the interface.

public interface IBook
{
    string Name { get; set; }
    string Likability { get; set; }
}

public class SomeBook : IBook
{
    public string Name { get; set; }
    public string Likability { get; set; }
    public int NumberOfPages { get; set; }
}

public class OtherBook : IBook
{
    public string Name { get; set; }
    public string Likability { get; set; }
    public int NumberOfAuthors { get; set; }
}

public class Books
{
    public Books()
    {
        IBook[] books = new IBook[2] {
            new SomeBook(),
            new OtherBook()
        };

        for (int i = 0; i < books.Length; i++)
        {
            switch (books[i].GetType().Name)
            {
                case "SomeBook":
                    int numberOfPages = ((SomeBook)books[i]).NumberOfPages;
                    break;

                case "OtherBook":
                    int numberOfAuthors = ((OtherBook)books[i]).NumberOfAuthors;
                    break;
            }
        }
    }
}

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.