views:

232

answers:

2

I have structs like below and when I do that initialization:

ArrayList nodesMatrix = null;
List<vertex> vertexMatrix = null;
List<bool> odwiedzone = null;
List<element> priorityQueue = null;

vertexMatrix = new List<vertex>(nodesNr + 1);
nodesMatrix = new ArrayList(nodesNr + 1);
odwiedzone = new List<bool>(nodesNr + 1);
priorityQueue = new List<element>();

arr.NodesMatrix = nodesMatrix;
arr.VertexMatrix = vertexMatrix;
arr.Odwiedzone = odwiedzone;
arr.PriorityQueue = priorityQueue; //only here i have exception

debuger fires Process is terminated due to StackOverflowException :/ Some idea why this collection fires this exception ?

private struct arrays
    {
        ArrayList nodesMatrix;

        public ArrayList NodesMatrix
        {
            get { return nodesMatrix; }
            set { nodesMatrix = value; }
        }
        List<vertex> vertexMatrix;

        public List<vertex> VertexMatrix
        {
            get { return vertexMatrix; }
            set { vertexMatrix = value; }
        }
        List<bool> odwiedzone;

        public List<bool> Odwiedzone
        {
            get { return odwiedzone; }
            set { odwiedzone = value; }
        }
        public List<element> PriorityQueue
        {
            get { return PriorityQueue; }
            set { PriorityQueue = value; }
        }


    }
    public struct element : IComparable
    {
        public double priority
        {
            get { return priority; }
            set { priority = value; }
        }
        public int node
        {
            get { return node; }
            set { node = value; }
        }
        public element(double _prio, int _node)
        {
            priority = _prio;
            node = _node;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            element elem = (element)obj;
            return priority.CompareTo(elem.priority);
        }

        #endregion
+8  A: 

Your PriorityQueue property is referencing itself.
You need to change the accessors to use a field.

List<element> priorityQueue;
public List<element> PriorityQueue
{
    get { return priorityQueue; }
    set { priorityQueue = value; }
}

However, you should use automatically-implemented properties instead:

public List<element> PriorityQueue { get; set; }
SLaks
A good reason for having class field names starting with underscore.
Yuriy Faktorovich
The same is true for the `note` and `priority` properties of the `element` struct.
Fredrik Mörk
Fredrik You right, I am also have this error on note and priority:/ All of this is cause I don't use VS automatic implementation of properties,but try write it myself!! But now I will remember this for sure!! Thanks!
netmajor
+3  A: 

Your property setter is recursive.

    public List<element> PriorityQueue
    {
        get { return PriorityQueue; }
        set { PriorityQueue = value; }
    }

Change this to be:

    public List<element> PriorityQueue
    {
        get { return priorityQueue; }
        set { priorityQueue = value; }
    }
sixlettervariables
He doesn't have a field, either.
SLaks
I noticed, figured he'd notice once the compile failed.
sixlettervariables