1

I am creating an array for some monetary values. I created the array as an integer and realized it needs to be decimal values. When I change the variables to decimals and try and run it, I get "Cannot be implicitly converted from decimal to int." I hover over the variables and they all appear to be decimals. I remember in the past placing .00M after int's to force them to be a decimal, but this doesn't seem to make a difference. Does this make sense to anyone else?

    //Global var
    Decimal lastIndexUsed = -1;
    Decimal[,] quarters = new Decimal[10, 5];
    string[] Branch = new string[10];

       //Calc button
            decimal Q1;
            decimal Q2;
            decimal Q3;
            decimal Q4;

            Q1 = Decimal.Parse(txtQ1.Text);
            Q2 = Decimal.Parse(txtQ2.Text);
            Q3 = Decimal.Parse(txtQ3.Text);
            Q4 = Decimal.Parse(txtQ4.Text);

            lastIndexUsed = lastIndexUsed + 1;
            quarters[lastIndexUsed, 0] = Q1;
            quarters[lastIndexUsed, 1] = Q2;
            quarters[lastIndexUsed, 2] = Q3;
            quarters[lastIndexUsed, 3] = Q4;
            Branch[lastIndexUsed] = txtBranch.Text;

The marked part is the first of many variables which error.

            Decimal row;
            Decimal col;
            Decimal accum;

            //Calculate

            for (row = 0; row <= lastIndexUsed; row++)
            {
                accum = 0;

                for (col = 0; col <= 3; col++)
                {
               ***     accum = accum + quarters[row, col];***
                }
                quarters[row, 4] = accum;
4
  • I have changed lastIndexUsed to an integer, still getting the same error. Commented Sep 26, 2011 at 0:35
  • Have also tried messing with "quarters" and changing it back to a int. Commented Sep 26, 2011 at 0:39
  • your row and col must be integers too Commented Sep 26, 2011 at 1:01
  • quarters[1,2] = 12.50; the value of your array elements are type Decimal. Row and Column must be integers as you can't address memory with fractions of numbers. Commented Sep 26, 2011 at 1:10

3 Answers 3

3

lastIndexUsed is used as an array index and should remain an integer.

Sign up to request clarification or add additional context in comments.

2 Comments

I have changed lastIndexUsed to an integer, still getting the same error.
All variables that are used as array indexes should be integers, including any ones that you didn't put in the code in your question.
1
Decimal[,] quarters = new Decimal[10, 5];

The index numbers are integers. You can't index by decimals. The array contains decimals.

I changed it to this to get it to run and it prints 10.15 like you'd expect

`//Global var
    int lastIndexUsed = -1;
    Decimal[,] quarters = new Decimal[10, 5];
    string[] Branch = new string[10];

   //Calc button
        decimal Q1;
        decimal Q2;
        decimal Q3;
        decimal Q4;

        Q1 = Decimal.Parse("10.15");
        Q2 = Decimal.Parse("13");
        Q3 = Decimal.Parse("123.9877");
        Q4 = Decimal.Parse("321");

        lastIndexUsed = lastIndexUsed + 1;
        quarters[lastIndexUsed, 0] = Q1;
        quarters[lastIndexUsed, 1] = Q2;
        quarters[lastIndexUsed, 2] = Q3;
        quarters[lastIndexUsed, 3] = Q4;
        Branch[lastIndexUsed] = "hello";

        Console.WriteLine(quarters[0,0]);`

2 Comments

What your saying makes sense, but visual studio is not agreeing. Your saying the only thing that needs to be changed is lastindexused?
Thanks! such simple errors always seem to trip me up, First time writing c# in a while.
1

Even though you are using an array of decimals, the indexer is still an integer.

Decimal lastIndexUsed;

should be

int lastIndexUsed

Comments

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.