0

This is throwing an error that EdgeList is not initialized. Here is the class (the pertinent part is way at the bottom):

public class TacLineStruct
{
    // The number of unit groups in the Army
    public int NumGroups
    {
        get
        {
            return _NumGroups;
        }
        set
        {
            _NumGroups = value;
        }
    }
    private int _NumGroups;

    // The number of edges, 
    public int NumEdges
    {
        get
        {
            return _NumEdges;
        }
        set
        {
            _NumEdges = value;
        }
    }
    private int _NumEdges;

    // The number of units below the threshold 
    public int NumBelowThreshold
    {
        get
        {
            return _NumBelowThreshold;
        }
        set
        {
            _NumBelowThreshold = value;
        }
    }
    private int _NumBelowThreshold;

    // The specific Group that a unit belongs to
    public int[] GroupID
    {
        get;
        set;
    }

    // The list of all the edges
    public int[][] EdgeList
    {
        get;
        set;
    }

    // The list of all the edge weights
    public float[] EdgeWeight
    {
        get;
        set;
    }

    // The geographical center of each group
    public Point[] GroupCenter
    {
        get;
        set;
    }

    public TacLineStruct(int arrayLength)
    {
        GroupID = new int[arrayLength];
        int[,] EdgeList = new int[(arrayLength * arrayLength),2];
        EdgeWeight = new float[arrayLength * arrayLength];
        GroupCenter = new Point[arrayLength];
    }
}

And this is how I'm calling and initializing it (snippet):

TacLineStruct TLS = new TacLineStruct(Army.Count);

for (int i = 0; i <= Army.Count; i++)
{
    for (int j = i + 1; j <= Army.Count; j++)
    {
        TLS.EdgeList[NumEdges][0] = i;     /* first vertex of edge */
        TLS.EdgeList[NumEdges][1] = j;     /* second vertex of edge */
        
        // ...
    }
}

I'm getting a runtime error that EdgeList is not initialized. My best guess is that I'm not doing something correctly with a 2D array with the length set at runtime.

2
  • In the constructor you initialize a local variable and not the class member (EdgeList). In addition, I think your loop needs to be < Army.Count. Also, you keep setting the same array cell again and again inside the loop. Commented Apr 25, 2019 at 18:45
  • I tried to indicate that I cut out a lot of stuff in the loop that wasn't relevant to the problem. NumEdges gets incremented further down Commented Apr 25, 2019 at 18:51

1 Answer 1

2

In your constructor, you are doing:

int[,] EdgeList = new int[(arrayLength * arrayLength), 2];

which creates a new (local) variable with the same name as the field. Instead you should do:

this.EdgeList = new int[(arrayLength * arrayLength), 2];

You could omit the this, but it can prevent you from making this mistake again.

Further, you should change the field declaration to

public int[,] EdgeList

Then you can set individual fields in the array via:

 EdgeList[i,j] = value; 
Sign up to request clarification or add additional context in comments.

6 Comments

This throws a compiler error: Invalid rank specifier.
Your class member is defined differently (as a jagged 2d array) You need to decide which type of a 2d array you're using
Well you had the field declared as a jagged array (array of arrays), you should rather be using a 2D array. I've edited my answer
Thanks! That's the ticket. What's the difference between arrays like [][[] and [,]?
@zetar: [,] can only be a "rectangular" array, which means that every row has the same amount of columns (e.g. 2x3, 2x2, 4x2 etc). [][] means that every row can have a different number of columns (i.e. jagged).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.