1

I have this program that i am writing that calculates GPAs and quality points for each course after the student enters their letter grade and credit hours.

The part i am having an issue with is the Calculation of the quality points for each course. I have the letter grades and credit hours dumped into 2 different arrays and I created a third array called QualityPts, which will be used to store the total quality points for each class.

This will be calculated by using the index position to determine the two values that will be used in the other 2 arrays. Now i am doing this a another method, and i am getting an error saying

"Not all code paths return a value".

The second error relates to my new variable "QualityPts" that is only in this new method. It says "Use of unassigned local variable".

These 2 errors are both in the method CalcQP(). My code is as follows:

private decimal[] grades;
private decimal[] Credits;

    private decimal CalcQP()
    {
        decimal[] QualityPts;
        string msg="The total quality Points for this course is: ";

        for (int i = 0; i < grades.Length; i++)
        {

            QualityPts[i] = grades[i] * Credits[i];
            lbQuality.Items.Add(msg + QualityPts[i]);

        }           

    }
Sayse
  • 42,633
  • 14
  • 77
  • 146
Spr89
  • 81
  • 1
  • 9
  • Your `CalcQP` return type is `decimal` but you are not return anything at all. And initialize your `QualityPts` array like `decimal[] QualityPts = new decimal[grades.Length];` – Soner Gönül Apr 21 '15 at 06:28
  • I removed lots of information that had nothing to do with your issue, you should try to narrow down your question to only the necessary code needed – Sayse Apr 21 '15 at 06:31

4 Answers4

1

Looks like CalcQP() return type should be void. Is the purpose of the method just to add items to lbQuality? I'm not sure what else it should be returning from the code snippet.

Essentially the compiler is stopping because it can see that the method should be returning a double but contains no return statement.

The second error is saving you a runtime error because the compiler can see you're trying to use a variable which hasn't been initialised. So you just need to initialise QualityPts:

decimal[] QualityPts = new decimal[grades.Length];
RagtimeWilly
  • 5,265
  • 3
  • 25
  • 41
  • thanks man, i dont know why i have to initialize it, i didnt have to type this for my other arrays. And yes, it is creating a new array with the product of the other 2 arrays. – Spr89 Apr 21 '15 at 18:58
  • It sounds like the array was being initialised for you in the other case. If you just declare a variable it will get assigned a null reference. So when you say `QualityPts[i]` there's nothing to reference - so you can see why it would throw an exception at runtime? I'd recommend reading up on memory allocation in .Net to gain a better understanding of what's happening. – RagtimeWilly Apr 21 '15 at 23:38
0

private decimal calcQP() method should return a decimal value or change decimal to void.

Also, QualityPts should be initialized: decimal[] QualityPts = new decimal[grades.Length];

segarci
  • 739
  • 1
  • 11
  • 19
  • @segaric, it worked after i changed it from a deciaml to a void. THanks for the help man. – Spr89 Apr 21 '15 at 18:59
0

you are declaring QualityPts but not initializing it. That's why its giving error. Follow standard practice which says always initialize variable when you declare it. Do it like below:

decimal QualityPts = new decimal[length]; // Replace length with your length.

Second thing CalcQP() methods return type is decimal while you are not returning anything. You must have to return decimal value from it. Either make that method void. Do it like below:

 public void CalcQP()

Let me know if you have another problem.

Priyank Sheth
  • 2,352
  • 19
  • 32
-1

Not all code paths return a value

You have declared your method to need a return type of decimal

private decimal CalcQP()

but you never return a decimal.

You need to either return one (return QualtityPts[i])

Return the array

private decimal[] CalcQP()
   ... return QualityPts;

or just don't return anything

private void CalcQP()

Use of unassigned local variable

You are trying to use QualityPts but you haven't declared it so [i] won't have any value (or exist yet)

decimal[] QualityPts = new decimal[grades.Length];
Sayse
  • 42,633
  • 14
  • 77
  • 146