I have just bought a book called "DirectX 9 User Interfaces: Design and Implementation". It's got code examples in C++, so I was hoping I could just port them over to C#.
The problem is with the base class called "CXControl". It uses a linked list type implementation and a bunch of methods for adding and removing child controls etc. I'm having difficulty translating it to C#.
C++
class CXControl
{
CXControl* m_Parent;
CXControl* m_FirstChild;
CXControl* m_NextSibling;
CXControl* m_PreviousSibling;
}
My first attempt started along the lines of
C#
class Control
{
Control Parent;
Control FirstChild;
Control NextSibling;
Control PreviousSibling;
}
I have my reservations about going this way because of having all these referenced objects could mess with Garbage Collection, and also performance issues. Am I in the right ballpark in thinking that?
So next I had a look at using System.Collections.Generic.LinkedList<T> and System.Collections.Generic.LinkedListNode<T> to implement this. But I just can't wrap my head around it. Do I inherit LinkedList<Control>? Do I change all the pointers to be nodes instead?
public LinkedListNode<Control> Parent = null;
public LinkedListNode<Control> FirstChild = null;
public LinkedListNode<Control> NextSibling = null;
public LinkedListNode<Control> PreviousSibling = null;
But then I'm having issues converting over some of the methods this way..
public Control AddChildControl(Control ctrl)
{
ctrl.Parent = this;
if (FirstChild == null)
FirstChild = ctrl;
else
{
LinkedListNode<Control> temp = this.FirstChild;
while (temp.Next != null)
temp = temp.Next;
temp.Next = ctrl;
ctrl.PreviousSibling = temp;
}
return ctrl;
}
It complains that ctrl.Parent "Cannot implicitly convert Control to LinkedListNode<Control>". And it also complains that temp.Next is read-only. It's really confusing me, in C++ it is such a simple task to have an internal linked list to manage all the controls and child controls for a GUI.
Is this a bad way to design a GUI for use in C#? I like the ideas presented in the book but are they only suited to C++?
This book is available on Google if you want to see more of it's implementation of the CXControl class on Chapter 7 (Page 135)..
http://books.google.com/books?id=j2N4jrL7jFkC&printsec=frontcover#PPA135,M1
Any help or recommendations would be greatly appreciated!