Reputation: 1063
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; }
}
}
Upvotes: 0
Views: 311
Reputation: 31
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;
}
}
}
}
Upvotes: 0
Reputation:
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; }
}
Upvotes: 2