Advertisement

10.10.2007 at 04:40PM PDT, ID: 22885773
[x]
Attachment Details

How do I access LinkedListNode.Item.Value within class derived from LinkedList

Asked by Woodster in C# Programming Language

I have just started using the linked list class in an application and have derived my own class from it.  What I want to do is add a Write function in my derived list class that will iterate through all members of the list , cast them to another class and then call the write function.

I do NOT want to have to do this iteration in a class that contains a member variable of type MyLinkedList.  Doing this introduces the possibility of some member variables not being written correctly.  By implementing this funciton in MyLinkedList class, I can ensure that all MyLinkedLIst variables are written as expected and that all their list members are also written.

The problem I have is that in the MyLinkedList.Write function, I am unable to do any casting as it throws a compile error (obviously because <T> is unknown.  How can I achieve this functionality?

public class MyWritableClass
{
  ...
  public virtual void Write(BinaryWriter bw)
  {
    ...
  }
}

public class MyLinkedList<T> : LinkedList<T>
{

  ...

  public WriteBinary(BinaryWriter bw)
  {

   ...

    // We can safely assume that all items contained within this list are (or are derived from) MyWritableClass (above)
    for (ListNode<T> item = First; item != null; item = item.Next)
    {
      // This line does not compile due to the casting of item.Value from the unknown type <T>
      ((MyWritableClass)item.Value).Write(bw))

      // This line does not compile because items of type <T> fo not have a Write member function
      item.Value.Write(bw))
    }
  }
}Start Free Trial
 
 
[+][-]10.10.2007 at 07:12PM PDT, ID: 20053724

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C# Programming Language
Sign Up Now!
Solution Provided By: drichards
Participating Experts: 1
Solution Grade: A
 
 
[+][-]10.11.2007 at 12:34AM PDT, ID: 20054857

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.11.2007 at 06:11AM PDT, ID: 20056522

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.11.2007 at 07:01PM PDT, ID: 20062546

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.11.2007 at 09:52PM PDT, ID: 20063141

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_1_20070628