Problem: I am trying to typecast an object type back to a root class
I'm starting with a generic doubly linked list and I am able to add values but not retrieve them
class CarPart
{
public long PartNumber;
public string PartName;
public double UnitPrice;
}
class DoublyLinkedList
{
private class DoublyLinkedListNode
{
private object element;
private DoublyLinkedListNode next;
private DoublyLinkedListNode previous;
public DoublyLinkedListNode(object element)
{
this.element = element;
this.next = null;
this.previous = null;
}
public DoublyLinkedListNode(object element, DoublyLinkedListNode prevNode)
{
this.element = element;
this.previous = prevNode;
prevNode.next = this;
}
public object Element
{
get { return this.element; }
set { this.element = value; }
}
public DoublyLinkedListNode Next
{
get { return this.next; }
set { this.next = value; }
}
public DoublyLinkedListNode Previous
{
get { return this.previous; }
set { this.previous = value; }
}
}
private DoublyLinkedListNode head;
private DoublyLinkedListNode tail;
private int count;
public DoublyLinkedList()
{
this.head = null;
this.tail = null;
this.count = 0;
}
public int Count
{
get { return this.count; }
}
public object this[int index]
{
get
{
if (index >= count || index < 0)
{
throw new ArgumentOutOfRangeException("Out of range!");
}
DoublyLinkedListNode currentNode = this.head;
for (int i = 0; i < index; i++)
{
currentNode = currentNode.Next;
}
return currentNode.Element;
}
set
{
if (index >= count || index < 0)
{
throw new ArgumentOutOfRangeException("Out of range!");
}
DoublyLinkedListNode currentNode = this.head;
for (int i = 0; i < index; i++)
{
currentNode = currentNode.Next;
}
currentNode.Element = value;
}
}
public void Add(object item)
{
if (this.head == null)
{
this.head = new DoublyLinkedListNode(item);
this.tail = this.head;
}
else
{
DoublyLinkedListNode newItem = new DoublyLinkedListNode(item, tail);
this.tail = newItem;
}
count++;
}
public void Insert(object item, int index)
{
count++;
if (index >= count || index < 0)
{
throw new ArgumentOutOfRangeException("Out of range!");
}
DoublyLinkedListNode newItem = new DoublyLinkedListNode(item);
int currentIndex = 0;
DoublyLinkedListNode currentItem = this.head;
DoublyLinkedListNode prevItem = null;
while (currentIndex < index)
{
prevItem = currentItem;
currentItem = currentItem.Next;
currentIndex++;
}
if (index == 0)
{
newItem.Previous = this.head.Previous;
newItem.Next = this.head;
this.head.Previous = newItem;
this.head = newItem;
}
else if (index == count - 1)
{
newItem.Previous = this.tail;
this.tail.Next = newItem;
newItem = this.tail;
}
else
{
newItem.Next = prevItem.Next;
prevItem.Next = newItem;
newItem.Previous = currentItem.Previous;
currentItem.Previous = newItem;
}
}
public void Remove(object item)
{
int currentIndex = 0;
DoublyLinkedListNode currentItem = this.head;
DoublyLinkedListNode prevItem = null;
while (currentItem != null)
{
if ((currentItem.Element != null &&
currentItem.Element.Equals(item)) ||
(currentItem.Element == null) && (item == null))
{
break;
}
prevItem = currentItem;
currentItem = currentItem.Next;
currentIndex++;
}
if (currentItem != null)
{
count--;
if (count == 0)
{
this.head = null;
}
else if (prevItem == null)
{
this.head = currentItem.Next;
this.head.Previous = null;
}
else if (currentItem == tail)
{
currentItem.Previous.Next = null;
this.tail = currentItem.Previous;
}
else
{
currentItem.Previous.Next = currentItem.Next;
currentItem.Next.Previous = currentItem.Previous;
}
}
}
public void RemoveAt(int index)
{
if (index >= this.count || index < 0)
{
throw new ArgumentOutOfRangeException("Out of range!");
}
int currentIndex = 0;
DoublyLinkedListNode currentItem = this.head;
DoublyLinkedListNode prevItem = null;
while (currentIndex < index)
{
prevItem = currentItem;
currentItem = currentItem.Next;
currentIndex++;
}
if (this.count == 0)
{
this.head = null;
}
else if (prevItem == null)
{
this.head = currentItem.Next;
this.head.Previous = null;
}
else if (index == count - 1)
{
prevItem.Next = currentItem.Next;
tail = prevItem;
currentItem = null;
}
else
{
prevItem.Next = currentItem.Next;
currentItem.Next.Previous = prevItem;
}
count--;
}
public int indexOf(object item)
{
int index = 0;
DoublyLinkedListNode currentItem = this.head;
while (currentItem != null)
{
if (((currentItem.Element != null) && (item == currentItem.Element)) ||
((currentItem.Element == null) && (item == null)))
{
return index;
}
index++;
currentItem = currentItem.Next;
}
return -1;
}
public bool Contains(object element)
{
int index = indexOf(element);
bool contains = (index != -1);
return contains;
}
public void Clear()
{
this.head = null;
this.tail = null;
this.count = 0;
}
public object First()
{
if (this.count < 0)
{
throw new ArgumentOutOfRangeException("Out of range!");
}
else
return this.head.Element;
}
public object Retrieve(int Position)
{
DoublyLinkedListNode current = this.head;
for (int i = 0; i < Position && current != null; i++)
current = current.Next;
return current;
}
}
class Program
{
static void Main(string[] args)
{
DoublyLinkedList Parts = new DoublyLinkedList();
object element;
CarPart Part;
CarPart PartToFind;
Part = new CarPart();
Part.PartNumber = 9743;
Part.PartName = "Air Filter";
Part.UnitPrice = 8.75;
Parts.Add(Part);
Part = new CarPart();
Part.PartNumber = 27487;
Part.PartName = "Clutch Disk";
Part.UnitPrice = 47.15;
Parts.Add(Part);
Part = new CarPart();
Part.PartNumber = 87873;
Part.PartName = "Brake Disk";
Part.UnitPrice = 35.15;
Parts.Add(Part);
Part = new CarPart();
Part.PartNumber = 27644;
Part.PartName = "A/C Filter Drier";
Part.UnitPrice = 55.55;
Parts.Add(Part);
Console.WriteLine(" -=- Store Inventory -=-");
Console.WriteLine("Number of Parts: {0}", Parts.Count);
object item = (object)Parts;
for (int i = 0; i < Parts.Count; i++)
{
// CarPart part = (CarPart)Parts.Retrieve(i);
element = Parts.Retrieve(i);
if (element != null)
{
// FAILS HERE AFTER RETRIEVING THE ELEMENT
CarPart part = (CarPart)element;
Console.WriteLine("\nCar Part Information");
Console.WriteLine("Part #: {0}", part.PartNumber);
Console.WriteLine("Description: {0}", part.PartName);
Console.WriteLine("Unit Price: {0:C}", part.UnitPrice);
}
}
}
}
What I am trying to achieve is to use the doubly linked list to store many different types - there are no problems adding items to the linked list but I've tried many different ways of typecasting but all fail.
Grateful for any advice on what should be a simple problem to resolve.
Your Retrieve
method returns the node itself - not the value within the node, which I assume is what you expected it to. That's why you're getting an InvalidCastException
. All you need to change is the last line of Retrieve
from
return current;
to
return current.element;
I'd also strongly recommend that you make DoublyLinkedList
a generic type (i.e. DoublyLinkedList<T>
), at which point you won't need to cast and the error would have been obvious at compile time.
See more on this question at Stackoverflow