First Chance Exception in Grade Calculator Code

Page 1 of 1

5 Replies - 543 Views - Last Post: 05 July 2013 - 09:44 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=324225&amp;s=6ddeb61adccbe4348bc91df93b498635&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 ajparkerv1

Reputation: 0
• Posts: 6
• Joined: 04-July 13

First Chance Exception in Grade Calculator Code

Posted 04 July 2013 - 09:52 AM

Greetings,

New to programming. I'm taking a 16 week CINS 113 course and jamming it into 8 weeks over the summer. I'm a little lost.

My assignment is to take three test scores, average them, and give a letter grade. Seems easy enough?

Here is the function.

```    Private Function CalculateGrade _
(ByVal testScore1 As Decimal, ByVal testScore2 As Decimal, _
ByVal testScore3 As Decimal) As String

Dim decTestScore1 As Decimal
Dim decTestScore2 As Decimal
Dim decTestScore3 As Decimal
intGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3

Else   '
End If

End Function
```

Here is the button display click procedure:

```Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
' prompts user to input 3 testscores,
' then calls a function to calculate them,

Dim decTestScore1 As Decimal
Dim decTestScore2 As Decimal
Dim decTestScore3 As Decimal

decAvgGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3

```

Whenver I try to call the function as intGrade = CalculateGrade(decAvegGrade) I get an error. Without it, there are no visable errors until I run it. Then I get this error:

A first chance exception of type 'System.NotImplementedException' occurred in Grade Project.exe
A first chance exception of type 'System.NotImplementedException' occurred in Grade Project.exe
A first chance exception of type 'System.NotImplementedException' occurred in Grade Project.exe
A first chance exception of type 'System.NotImplementedException' occurred in Grade Project.exe
A first chance exception of type 'System.NotImplementedException' occurred in Grade Project.exe

I'm very, very close on this one. Very very close.

Is This A Good Question/Topic? 0

Replies To: First Chance Exception in Grade Calculator Code

#2 modi123_1

• Suitor #2

Reputation: 10846
• Posts: 42,516
• Joined: 12-June 08

Re: First Chance Exception in Grade Calculator Code

Posted 04 July 2013 - 10:16 AM

First off - order of operations in line 12..

```12	        decAvgGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3
```

You are adding decTestScore1 to decTestScore2 to the result of "decTestScore3 / 3". Doesn't seem right.
Same here:
```10	        intGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3
```

What are you doing here?
```13	        intGrade = (decAvgGrade)
```

You do know that the default value for a decimal is 0, right? If you don't assign your incoming parameters (the testscore1, testscore2, etc) to the 'dectestcore1', 'dectestscore2', etc that you are just adding up zeros in line 10.
```02	(ByVal testScore1 As Decimal, ByVal testScore2 As Decimal, _
03	ByVal testScore3 As Decimal) As String
05
07	        Dim decTestScore1 As Decimal
08	        Dim decTestScore2 As Decimal
09	        Dim decTestScore3 As Decimal
10	        intGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3
```

#3 lar3ry

• Coding Geezer

Reputation: 313
• Posts: 1,296
• Joined: 12-September 12

Re: First Chance Exception in Grade Calculator Code

Posted 04 July 2013 - 10:34 AM

Quote

I'm very, very close on this one. Very very close.

Unfortunately, you are not close. Fortunately, you have made the attempt, and have asked the right questions.

The first thing you should do is to add the line "Option Strict On" above the Class statement. See the link in my signature for why you should. After you put the line in there, you can fix any errors that show up with a wiglly underline.

Now... here's what I see in your code.

1. According to your comments in the Button Click Sub, you are prompting the user for three testscores, but you are not. You can use InputBoxes or TextBoxes for that.

2. You Dim 3 decimal variables, which is fine (though they do not have to be Decimal if your possible test scores do not have decimal points); they can be integers. The problem here is that you never assign any values to them, so they are all initialized to 0.

3.You then add them together, and divide by three, assigning the result to decAvgGrade. This again is fine, but 0+0+0 divided by 3 is always going to be 0.

4 Your comments further state that you are calling a function, but you are not. You are trying to call it, I think, with the line: intGrade = (decAvegGrade) , but all that line does is to assign decAvgGrade to intGrade. Yiu need to actually call the function. Reading the function itself tells us that it is called like this:

```   <somevariable> = CalculateGrade(testScore1,testScore2, testScore3)

```

Now, <somevariable> has to be a String variable if your function returns a string, which its definition says it does.

5. In your function, testScore1, testScore2 ,testScore3 are already declared, by virtue of being in the parameter list in the Fnction definition. BUt shortly after that, you DIm 3 Decimal variables, and never assign values to them. You then (again) calculate 0+0+0 divided by 3, which will always be 0.

6. You should also learn about operator precedence, and use parentheses to force the precedence. in your calculation of the average, scores of 40, 50, and 60 will result in an average of 110, which is obviously wrong.

Get back to us if you have any further difficulties.

#4 ajparkerv1

Reputation: 0
• Posts: 6
• Joined: 04-July 13

Re: First Chance Exception in Grade Calculator Code

Posted 04 July 2013 - 10:47 AM

lar3ry, on 04 July 2013 - 10:34 AM, said:

Quote

I'm very, very close on this one. Very very close.

Unfortunately, you are not close. Fortunately, you have made the attempt, and have asked the right questions.

The first thing you should do is to add the line "Option Strict On" above the Class statement. See the link in my signature for why you should. After you put the line in there, you can fix any errors that show up with a wiglly underline.

Now... here's what I see in your code.

1. According to your comments in the Button Click Sub, you are prompting the user for three testscores, but you are not. You can use InputBoxes or TextBoxes for that.

2. You Dim 3 decimal variables, which is fine (though they do not have to be Decimal if your possible test scores do not have decimal points); they can be integers. The problem here is that you never assign any values to them, so they are all initialized to 0.

3.You then add them together, and divide by three, assigning the result to decAvgGrade. This again is fine, but 0+0+0 divided by 3 is always going to be 0.

4 Your comments further state that you are calling a function, but you are not. You are trying to call it, I think, with the line: intGrade = (decAvegGrade) , but all that line does is to assign decAvgGrade to intGrade. Yiu need to actually call the function. Reading the function itself tells us that it is called like this:

```   <somevariable> = CalculateGrade(testScore1,testScore2, testScore3)

```

Now, <somevariable> has to be a String variable if your function returns a string, which its definition says it does.

5. In your function, testScore1, testScore2 ,testScore3 are already declared, by virtue of being in the parameter list in the Fnction definition. BUt shortly after that, you DIm 3 Decimal variables, and never assign values to them. You then (again) calculate 0+0+0 divided by 3, which will always be 0.

6. You should also learn about operator precedence, and use parentheses to force the precedence. in your calculation of the average, scores of 40, 50, and 60 will result in an average of 110, which is obviously wrong.

Get back to us if you have any further difficulties.

This is embarrassing. You (not sure if guys or gals) people are EXTREMELY helpful. Thank you so much.

re: 1-3 I thought the user iputs the testscores and thus assigns the variables?

I need to try to wrap my head around this.

I thought the user assigns the variables when they input their scores?

modi123_1, on 04 July 2013 - 10:16 AM, said:

First off - order of operations in line 12..

```12	        decAvgGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3
```

You are adding decTestScore1 to decTestScore2 to the result of "decTestScore3 / 3". Doesn't seem right.
Same here:
```10	        intGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3
```

What are you doing here?
```13	        intGrade = (decAvgGrade)
```

You do know that the default value for a decimal is 0, right? If you don't assign your incoming parameters (the testscore1, testscore2, etc) to the 'dectestcore1', 'dectestscore2', etc that you are just adding up zeros in line 10.
```02	(ByVal testScore1 As Decimal, ByVal testScore2 As Decimal, _
03	ByVal testScore3 As Decimal) As String
05
07	        Dim decTestScore1 As Decimal
08	        Dim decTestScore2 As Decimal
09	        Dim decTestScore3 As Decimal
10	        intGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3
```

Line 13 I'm trying to call the function. I agree it doesn't seem right at all. Very little of this does.

Ugh.

#5 lar3ry

• Coding Geezer

Reputation: 313
• Posts: 1,296
• Joined: 12-September 12

Re: First Chance Exception in Grade Calculator Code

Posted 04 July 2013 - 12:29 PM

ajparkerv1, on 04 July 2013 - 11:47 AM, said:

This is embarrassing. You (not sure if guys or gals) people are EXTREMELY helpful. Thank you so much.

No need for embarrassment. We've all been there. Everyone has to start somewhere, and we are here because we like to help those who want to learn.

Quote

re: 1-3 I thought the user iputs the testscores and thus assigns the variables?

I need to try to wrap my head around this.

I thought the user assigns the variables when they input their scores?

The programmer has to ask the user for input in one way or another. In your program, you need three testscores, so you need to have some way the user can put them in. Otherwise, your program doesn't know what the scores are. There are a number of ways to accomplish this. You can use three TextBoxes, and position a Label next to each one, then provide a Button to start the process of gatting the input into variables and calculating the averag.

Or, you could use an InputBox within a loop, and gather the three values that way.

Check out the MSDN Library, where you can search for any control names (InputBox, TextBox, etc.), or any object, method or property, to get documentation and/or examples of usage.

Quote

Line 13 I'm trying to call the function. I agree it doesn't seem right at all. Very little of this does.

As I mentioned, you call a Function by assigning its return value to a variable. Have a look at our Subroutine and Function Tutorial for some ideas.

#6 ajparkerv1

Reputation: 0
• Posts: 6
• Joined: 04-July 13

Re: First Chance Exception in Grade Calculator Code

Posted 05 July 2013 - 09:44 AM