Last post Nov 18, 2009 12:45 AM by sourabh3120
Nov 17, 2009 12:40 PM|rspiet|LINK
Please explain why the following code generated an OverFlow Exception on the Return Statement. The Exception Occurred during the Return Statement so you can ignore the first part of the function. The values of the five inputs to the multiplication statement
were 10, 8, 8, 10, and 10 which are clearly in the range of an Int16. The product of the multiplication statement should be 64,000 which is clearly in the range of an Int32. So why did the Overflow Exception occur?
Public Function CalculateRiskAssessmentCode(ByVal RiskAssessmentSeverityCode As Int16, _
ByVal RiskAssessmentExposureCode As Int16, _
ByVal RiskAssessmentProbabilityCode As Int16, _
ByVal RiskAssessmentNumberAtRiskCode As Int16, _
ByVal RiskAssessmentMajorRiskCode As Int16) As Int32
Dim RiskAssessmentSeverityData As DataRow = GetRiskAssessmentSeverityData(RiskAssessmentSeverityCode)
Dim RiskAssessmentExposureData As DataRow = GetRiskAssessmentExposureData(RiskAssessmentExposureCode)
Dim RiskAssessmentProbabilityData As DataRow = GetRiskAssessmentProbabilityData(RiskAssessmentProbabilityCode)
Dim RiskAssessmentNumberAtRiskData As DataRow = GetRiskAssessmentNumberAtRiskData(RiskAssessmentNumberAtRiskCode)
Dim RiskAssessmentMajorRiskData As DataRow = GetRiskAssessmentMajorRiskData(RiskAssessmentMajorRiskCode)
Dim RiskAssessmentCodeMultiplierSeverity As Int16 = 0
Dim RiskAssessmentCodeMultiplierExposure As Int16 = 0
Dim RiskAssessmentCodeMultiplierProbability As Int16 = 0
Dim RiskAssessmentCodeMultiplierNumberAtRisk As Int16 = 0
Dim RiskAssessmentCodeMultiplierMajorRisk As Int16 = 1
If RiskAssessmentSeverityData IsNot Nothing Then
RiskAssessmentCodeMultiplierSeverity = RiskAssessmentSeverityData("RiskAssessmentCodeMultiplier")
If RiskAssessmentExposureData IsNot Nothing Then
RiskAssessmentCodeMultiplierExposure = RiskAssessmentExposureData("RiskAssessmentCodeMultiplier")
If RiskAssessmentProbabilityData IsNot Nothing Then
RiskAssessmentCodeMultiplierProbability = RiskAssessmentProbabilityData("RiskAssessmentCodeMultiplier")
If RiskAssessmentNumberAtRiskData IsNot Nothing Then
RiskAssessmentCodeMultiplierNumberAtRisk = RiskAssessmentNumberAtRiskData("RiskAssessmentCodeMultiplier")
If RiskAssessmentMajorRiskData IsNot Nothing Then
RiskAssessmentCodeMultiplierMajorRisk = RiskAssessmentMajorRiskData("RiskAssessmentCodeMultiplier")
Return RiskAssessmentCodeMultiplierSeverity _
* RiskAssessmentCodeMultiplierExposure _
* RiskAssessmentCodeMultiplierProbability _
* RiskAssessmentCodeMultiplierNumberAtRisk _
Nov 18, 2009 12:15 AM|Pandiya Krishnan|LINK
If the calculation in the return statement is returning the value, which is beyond the range of –2147483648 to 2147483647, then only overfow exception would occur.
As all your variables are Int16, cast your calculation to Int32.
Use Convert.ToInt32 to accomplish this.
Nov 18, 2009 12:45 AM|sourabh3120|LINK
You are returning a value which is going out of range. so check out for the value which comes out after calculating the value you have in return statement, and than Convert that accordingly.