Inner exception problem

Code is running before values set

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 6762 Views - Last Post: 11 February 2008 - 03:38 PM Rate Topic: -----

#1 Adrian Aslett  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-February 08

Inner exception problem

Post icon  Posted 11 February 2008 - 11:21 AM

Hi,

I have written a program in VS2005 in VB.net and do not recall having this problem.

I upgraded to VS2008 and now when I run the program I get the following error.

An error occurred creating the form. See Exception.InnerException for details. The error is: Arithmetic operation resulted in an overflow.

When I trace the program using F8 I see that even before the load events arise it starts to work its way through the subs. These subs call a calculation and hence the error as the values I am looking at are all set to zero.

Can any one guide me as to what may be switched on or off in the settings that could cause this to happen ?

Thanks

Adrian

Is This A Good Question/Topic? 0
  • +

Replies To: Inner exception problem

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4422
  • View blog
  • Posts: 12,289
  • Joined: 18-April 07

Re: Inner exception problem

Posted 11 February 2008 - 11:52 AM

Perhaps you can show us some of the code that is failing? If you can provide a few of the subs that being processed and generating the errors. My guess from the error message is that you are doing a calculation which is going over the limits of what a variable can hold. We will be able to see that in the code if you can provide it.

Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#3 Adrian Aslett  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-February 08

Re: Inner exception problem

Posted 11 February 2008 - 11:57 AM

View PostMartyr2, on 11 Feb, 2008 - 11:52 AM, said:

Perhaps you can show us some of the code that is failing? If you can provide a few of the subs that being processed and generating the errors. My guess from the error message is that you are doing a calculation which is going over the limits of what a variable can hold. We will be able to see that in the code if you can provide it.

Thanks! :)


Hi Martyr2,

It's as simple as this. as soon as the form tries to form it seems to be sequencing its way through and making the call to the sub. The sub is simply a range of calculations based upon the settings of controls on the form. However if I trace the sequence it seems the loading has not had a chance to set the values up before the call to the sub. I hope I have explained this OK !

As you can see I haver added the Try function as this prevents the debugger from halting.......

Thanks for looking, if you know what I am doing wrong it'll save me a bit of hair..


	Private Sub nudWrapAngle_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudWrapAngle.ValueChanged
		LoadCellCalcs()
	End Sub

	Sub LoadCellCalcs()
		Try
			If MatUnits.SelectedIndex = 0 Then MFUnitConstant = 1
			If MatUnits.SelectedIndex = 1 Then MFUnitConstant = 9.81
			If MatUnits.SelectedIndex = 2 Then MFUnitConstant = 9.81
			If MatUnits.SelectedIndex = 3 Then MFUnitConstant = 4.45
			If MatUnits.SelectedIndex = 4 Then MFUnitConstant = 4.45
			'Roll Weight Constants
			If RWeightUnits.SelectedIndex = 0 Then RWUnitConstant = 1
			If RWeightUnits.SelectedIndex = 1 Then RWUnitConstant = 9.81
			If RWeightUnits.SelectedIndex = 2 Then RWUnitConstant = 4.45

			If rbCalcCL.Checked = True Then NoCells = 1
			If rbCalcDC.Checked = True Then NoCells = 2
			If rbCalcSFB.Checked = True Then NoCells = 2
			If rbCalcDFB.Checked = True Then NoCells = 2


			'Calculate results
			Tension = nudTension.Value * MFUnitConstant
			CalConstant = nudCalConstant.Value
			Length = nudLength.Value
			RollWeight = nudRollWeight.Value * RWUnitConstant
			WrapAngle = nudWrapAngle.Value + BisectorAdj.Value
			StartAngle = nudStartAngle.Value
			BisectorAngle = (StartAngle - (WrapAngle / 2) - 360) + BisectorAdj.Value

			'Material Force
			Tensionload = 2 * Tension * Math.Sin(WrapAngle / 2 * Math.PI / 180) * CalConstant * Length
			'Roller Force
			RollLoad = RollWeight * Math.Sin(BisectorAngle * Math.PI / 180)
			' Maximum force
			MaxLoad = (Math.Abs(Tensionload) + Math.Abs(RollLoad))

			LC1 = CInt(MaxLoad / NoCells)
			LC2 = CInt(MaxLoad / NoCells)

			'Total sensing roller loads
			txtTensionload.Text = FormatNumber(Tensionload, 2) & " N"
			txtRollLoad.Text = FormatNumber(RollLoad, 2) & " N"
			txtMWF.Text = FormatNumber(MaxLoad, 2) & " N"
			'BisectorValue.Text = Bisector & " Deg"
			'Load Cell A
			txtLC1Tens.Text = FormatNumber(Tensionload / NoCells, 2) & " N"
			txtLC1RW.Text = FormatNumber(RollLoad / NoCells, 2) & " N"
			txtLC1MWF.Text = FormatNumber((Tensionload + RollLoad) / NoCells, 2) & " N"
			'Load Cell B
			txtLC2Tens.Text = FormatNumber(Tensionload / NoCells, 2) & " N"
			txtLC2RW.Text = FormatNumber(RollLoad / NoCells, 2) & " N"
			txtLC2MWF.Text = FormatNumber((RollLoad + Tensionload) / NoCells, 2) & " N"
			MatchCell()
			DrawRoll()
		Catch ex As Exception

		End Try


This post has been edited by PsychoCoder: 11 February 2008 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4422
  • View blog
  • Posts: 12,289
  • Joined: 18-April 07

Re: Inner exception problem

Posted 11 February 2008 - 12:33 PM

Do you have anything in the form_load event that is being processed? Is anything in there calling another function which is then calling your LoadCellsCalc? What are the first few lines you see when you start the trace? Can you show us the form_load? Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 Adrian Aslett  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-February 08

Re: Inner exception problem

Posted 11 February 2008 - 12:54 PM

View PostMartyr2, on 11 Feb, 2008 - 12:33 PM, said:

Do you have anything in the form_load event that is being processed? Is anything in there calling another function which is then calling your LoadCellsCalc? What are the first few lines you see when you start the trace? Can you show us the form_load? Thanks.


Hi Again,

Thanks for your patience, I am not aware that I am calling any coding that does this. When I press f8 then the first line of code that the trace stops at is the sub routine call shown before. It seems to perform this before any loading functions !

As these are events then I would not expect the program to start working their way through them unless I misunderstand something.

Here is the code :


#Region "Loading"
	'Form load operations
	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

		'Check TTS Support folders and files exist
		CheckDirectoryExists()

		CalcDate.Text = Format(Now())
		nudLength.Enabled = False

		'Personalise tabs and controls
		Dim Count As Integer
		If My.Computer.FileSystem.FileExists("C:\ProgramData\TTS Systems\TTS Support\My Details.txt") = True Then
			Dim MyDetails As New StreamReader("C:\ProgramData\TTS Systems\TTS Support\My Details.txt")
			For Count = 0 To UBound(Preferences)
				Preferences(Count) = MyDetails.ReadLine()
			Next

		End If

		'Set up welcome detail 
		FirstName = Preferences(0)
		TabControl1.SelectedTab.Text = "Welcome " & Preferences(0)
		lblWelcome.Text = "Welcome " & FirstName & " to TTS Systems Product Selector Program" & vbCrLf & vbCrLf & "We are currently developing this program which will make it easier for you to choose the right products for your tension application." & vbCrLf & vbCrLf & "By using this program you can size your load cells, choose the fixing arrangements and send the details by e-mail so we can provide you with an up to date quotation. You will also be able to place an order directly with us." & vbCrLf & vbCrLf & "This program automatically advises you when an update is ready for downloading if you are connected to the Internet." & vbCrLf & vbCrLf & "If you have an Adobe PDF creator (or one of the many free convertors) you can print out any of the screens by pressing the 'Print Tab Button'." & vbCrLf & vbCrLf & "Thank you for your interest."


		' Add serial port count
		Dim sp As String
		For Each sp In My.Computer.Ports.SerialPortNames.Count.ToString
			ListBox2.Items.Add(sp)
		Next

		'Populate array with current load ratings
		LCRatings(0) = 100 : LCRatings(1) = 250 : LCRatings(2) = 500 : LCRatings(3) = 1000 : LCRatings(4) = 2500 : LCRatings(5) = 5000 : LCRatings(6) = 10000 : LCRatings(7) = 15000 : LCRatings(8) = 20000
	End Sub

#End Region




I have rem'd out all the load statements and pressed f8. The program goes straight to the code that is the subroutine call......

PS I hope you are not too critical about my coding skills. The progams do seem to do their job and I am only just learning VB.net

This post has been edited by PsychoCoder: 11 February 2008 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4422
  • View blog
  • Posts: 12,289
  • Joined: 18-April 07

Re: Inner exception problem

Posted 11 February 2008 - 01:23 PM

Put a break point on your CalcDate.Text = Format(Now()) line. Then run the program. Do you hit your LoadCellscalc before it stops on the line where you put your breakpoint?
Was This Post Helpful? 0
  • +
  • -

#7 Adrian Aslett  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-February 08

Re: Inner exception problem

Posted 11 February 2008 - 01:45 PM

View PostMartyr2, on 11 Feb, 2008 - 01:23 PM, said:

Put a break point on your CalcDate.Text = Format(Now()) line. Then run the program. Do you hit your LoadCellscalc before it stops on the line where you put your breakpoint?


Hi,

It goes to the sub before the BP. I did add that I had rem'd out the statements and the problem still arises. What I do not understand is why it is trying to run the sub before it even loads !
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4422
  • View blog
  • Posts: 12,289
  • Joined: 18-April 07

Re: Inner exception problem

Posted 11 February 2008 - 01:47 PM

Put a break point on the CheckDirectoryExists() and rerun. Does it stop on the break point before reaching the LoadCellsCalc?
Was This Post Helpful? 0
  • +
  • -

#9 Adrian Aslett  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-February 08

Re: Inner exception problem

Posted 11 February 2008 - 02:06 PM

View PostMartyr2, on 11 Feb, 2008 - 01:47 PM, said:

Put a break point on the CheckDirectoryExists() and rerun. Does it stop on the break point before reaching the LoadCellsCalc?



Same result
Was This Post Helpful? 0
  • +
  • -

#10 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Inner exception problem

Posted 11 February 2008 - 02:14 PM

What is the type of control that you have called nudWrapAngle?

Is it a NumericUpDown control?
Was This Post Helpful? 0
  • +
  • -

#11 Adrian Aslett  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-February 08

Re: Inner exception problem

Posted 11 February 2008 - 02:24 PM

View Postjayman9, on 11 Feb, 2008 - 02:14 PM, said:

What is the type of control that you have called nudWrapAngle?

Is it a NumericUpDown control?


yes it is !
Was This Post Helpful? 0
  • +
  • -

#12 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Inner exception problem

Posted 11 February 2008 - 02:39 PM

Ok, then the problem is when the control is initialized during the form load. The ValueChanged event will get fired, this is because it is loading the default value that you have specified into the control. Since the ValueChanged event fires, this in turn will cause your LoadCellCalcs() method to fire off during loading.

The solution is to create a class level Boolean that is initially set to True and then as the very last line of code in the Load event of your form set the value to False.
Public Class Form1
 'class level variable
 Private loading As Boolean = True

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		'all your other code goes here
		
		'set the class level variable to false, your form has completely loaded
		loading = False
	End Sub


Now add a simple IF statement inside the ValueChanged event to check if the value is NOT true of this class level variable. This will stop your method from firing until after the form has completely loaded.

	Private Sub nudWrapAngle_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudWrapAngle.ValueChanged
		'if the form is currently loading then skip the method call
		If Not loading Then
			LoadCellCalcs()
		End If
	End Sub

Was This Post Helpful? 0
  • +
  • -

#13 Adrian Aslett  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-February 08

Re: Inner exception problem

Posted 11 February 2008 - 02:46 PM

Hi Jayman,

I understand now where you are coming from and will apply this. It's late night here so I am going to finish but will try this in the morning. Thanks for both your input. It's good to get such responsive assistance and I now know I am not alone in the world . :D

Will add a note to let you know how I got on . . . . . :^:
Was This Post Helpful? 0
  • +
  • -

#14 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4422
  • View blog
  • Posts: 12,289
  • Joined: 18-April 07

Re: Inner exception problem

Posted 11 February 2008 - 02:56 PM

I had already thought about the Valuechanged event firing, but unless this is new behavior in 2008, or a value is being assigned to the updown control in a place we don't see, this event should not fire before the form load. I don't know since I am still working in 2005, but theoretically it shouldn't be the cause. This is why I am attempting to isolate the problem via break points.
Was This Post Helpful? 0
  • +
  • -

#15 Adrian Aslett  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-February 08

Re: Inner exception problem

Posted 11 February 2008 - 03:05 PM

Hi Martyr, Jayman,

Could not wait until morning so I have added Jaymans suggestion. It works perfectly. I suspect that as Jayman implied I am loading a value into the in control through the properties page and this is firing the event trigger.

Forcing the program to check the status first made the difference as it still goes through the call when I step through but of course it ignores the call until the flag is set. I think this will also help me with another program that did some funnies as again I make extensive use of NUD controls and perform some calculations when these are changed. They also load with default values.

Once again many thanks for your assistance and I hope this helps others with similar problems..

Adrian
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2