Okay, so, I've got this code-
List<Post> list = new List<Post>();
foreach(XElement post in posts)
{
var postType = post.Descendants();
var type = (string)post.Attribute("type");
Post item;
if (type == "regular")
{
item = new RegularPost();
item.title = (string)postType.ElementAt(0);
item.body = (string)postType.ElementAt(1);
}
if (type == "quote")
{
item = new Quote();
item.source = (string)postType.ElementAt(1);
item.text = (string)postType.ElementAt(0);
}
if (type == "photo")
{
item = new Photo();
item.caption = (string)postType.ElementAt(0);
var photoUrl = post.Descendants("photo-url");
foreach (var photoUrlSpecific in photoUrl)
{
var photoSize = (string)photoUrlSpecific.Attribute("max-width");
switch (photoSize)
{
case "500":
item.url.width500 = (string)photoUrlSpecific;
break;
case "400":
item.url.width400 = (string)photoUrlSpecific;
break;
case "250":
item.url.width250 = (string)photoUrlSpecific;
break;
case "100":
item.url.width100 = (string)photoUrlSpecific;
break;
case "75":
item.url.width75 = (string)photoUrlSpecific;
break;
}
}
}
item.date = (string)post.Attribute("date");
item.id = (int)post.Attribute("id");
list.Add(item);
Post is an abstract class whereas others are the classes that extend from it.
I've made sure that the methods exists and there are no typos but everytime I try compiling the program, it gives an error-
"Error 1 'Tumblarity.Post' does not contain a definition for 'title' and no extension method 'title' accepting a first argument of type 'Tumblarity.Post' could be found (are you missing a using directive or an assembly reference?)"
Is there any way to declare a single variable which can change its type depending upon the type of post? It should also be accessible so as to be added to a list.
Thanks in advance!