0

I would like to know how to use a class constructor to set the values of a multidimensional array. I have used a constructor to set the values of integers before (see below), but this syntax doesn't seem to work with arrays.

Here is how I would use a constructor with integers

public class Warehouse
{   
    //declares instance variables
    public int radios;
    public int televisions;
    public int computers;


    //Creates constructor with 0 inventory
    public Warehouse()
    {
        radios = 0;
        televisions = 0;
        computers = 0;
    }

This code above has worked for me in previous assignments. However, the code below is what I am currently attempting to fix. Visual Studio says that the variables are unused and will remain at the default value. Also, the commas in the lines assigning a value to each index are underlined red saying that a semicolon was expected instead. Is there another way of using a constructor to set the values of these arrays? I would just declare the values along with the arrays, but the assignment asks for a constructor to be used.

public class Matrix
{
    public double[,] matrixX;
    public double[,] matrixY;
    public double[,] xySum;
    public double[,] xyDiff;
    public double[,] xScalar;

    public Matrix()
    {
        matrixX = { { 1.1, 2.2, 3.3 }, { 4.4, 5.5, 6.6 }, { 7.7, 8.8, 9.9 } };
        matrixY = { { 9.9, 8.8, 7.7 }, { 6.6, 5.5, 4.4 }, { 3.3, 2.2, 1.1 } };
        xySum = { { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 } };
        xyDiff = { { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 } };
        xScalar = { { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 } };
    }
2
  • 2
    matrixX = new double[,] { { 1.1, 2.2, 3.3 }, { 4.4, 5.5, 6.6 }, { 7.7, 8.8, 9.9 } }; Commented Sep 24, 2018 at 21:43
  • Side note: Always pay attention to these red wiggly lines in Visual Studio. Pay attention to not just some of them, pay attention to all of them. Some errors indicated by those red wiggly snakes are just a side effect of other errors in your code that also have a red wiggly snake. (Oh, and sometime those snakes can be green, too... ;-) Commented Sep 24, 2018 at 21:45

1 Answer 1

1

Whenever instantiating a new object, the new keyword has to appear somewhere, always (unless you are using Reflection and Activator.CreateInstance()). So to initialize the arrays, you have to declare the new double[,] as part of the assignment.

So instead of this:

    matrixX = { { 1.1, 2.2, 3.3 }, { 4.4, 5.5, 6.6 }, { 7.7, 8.8, 9.9 } };

Use this:

    matrixX = new double[,] { { 1.1, 2.2, 3.3 }, { 4.4, 5.5, 6.6 }, { 7.7, 8.8, 9.9 } };
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, but the variables at the top are still underlined in green and commenting them out make the variables in the constructor to be underlined in red for "not being found in the current context"
I'm not able to reproduce that issue @Zachary. What message do you see when you hover over the green lines?
the lines like public double[,] matrixX; have a green line under the variable that says "is never assigned to, and will always have its default value null."
I believe I have fixed the issue. There must have been an error elsewhere in the code causing an issue. Thank you very much for the assistance

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.