Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counter to count the exceptions 1

Status
Not open for further replies.

Ramnarayan

Programmer
Jan 15, 2003
56
US
Hi,

I am trying to keep track of counting the # of times the program throws an exception. When the number crosses 3, It should throw an exception saying that number of tries are 3 and program is exiting.

Here is the main class (Centrigade to farenheit)

Imports System.Windows.Forms

Public Class CentrigatetoFahrenheit
Inherits Form
Public Function ctof(ByVal cnum As Double) As Double
Return (9 * (cnum / 5)) + 32
End Function

Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click
fLabel.Text = ""
Dim ctr As ExceptionCounter = New ExceptionCounter
Try
Dim ctofresult As Double = ctof(Convert.ToDouble(cTextBox.Text))
fLabel.Text = ctofresult.ToString
Catch format As FormatException
MessageBox.Show(format.Message, "Invalid Number Format", MessageBoxButtons.OK, MessageBoxIcon.Error)
!!!!! ctr = ctr + 1 'Not allowed by VB.NET
' Dim excctr As ExceptionCounter = New ExceptionCounter(1)
'Catch blank As BlankException
' MessageBox.Show(blank.Message, "Invalid Number Format", MessageBoxButtons.OK, MessageBoxIcon.Error)

End Try
End Sub
End Class

Here is the exceptionCounter Class:

Public Class ExceptionCounter
Inherits ApplicationException
Private mctr As Integer

Public Sub New()
mctr = 0
End Sub
Public Sub New(ByVal ctr As Integer)
mctr = ctr
End Sub
Private Function excctr() As Integer
Return mctr = mctr + 1
End Function

End Class
 
This: ctr = ctr + 1 wont work =until .Net 2. Its called and overloaded opperator.

I would say try something like this in your counter class:
Code:
Public Class ExceptionCounter
    Inherits ApplicationException
    Private shared m_ctr As Integer = 0

    Public Sub New()
      m_ctr += 1
    End Sub

    public readonly property Count as integer
      get
        return m_ctr
      end get
    end property
End Class

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Hi Rick,

Thanks for the tip. But how do I keep track of the counter from the base class i.e. centrigadetoFarenheit class. Note that i already put the code in the main class as :
Code:
Dim ctr As ExceptionCounter = New ExceptionCounter
But when an exception is thrown, which is under :
Code:
        Catch format As FormatException
            MessageBox.Show(format.Message, "Invalid Number Format", MessageBoxButtons.OK, MessageBoxIcon.Error)
how do I indicate that the ctr is to be incremented by 1. And where should I put the if loop that if ctr is = 3, it should break the program and exit.

Thanks always for your assistance.
 
Try something like this:

Code:
    Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click
      Dim ctr As ExceptionCounter 
      dim bSuccess as boolean = false 

      Do While ctr.Count < 3 and not bSuccess
        fLabel.Text = ""
        Try
          Dim ctofresult As Double = ctof(Convert.ToDouble(cTextBox.Text))
          fLabel.Text = ctofresult.ToString
          bSuccess = true
        Catch format As FormatException
          MessageBox.Show(format.Message, "Invalid Number Format", MessageBoxButtons.OK, MessageBoxIcon.Error)
          ctr = new ExceptionCounter 
        End Try
      Loop
    End Sub
End Class

The ExceptionCounter class will automaticly incriment the counter each time it is instantiated.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks man for the useful tip. However an error came up i.e.

"An unhandled exception of type 'System.NullReferenceException' occurred.

Additional Information: Object reference not set to an instance of an object.

How to remove this error?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top