amykrug

amykrug

  • NA
  • 1
  • 0

Problems getting values to add using a class

Feb 16 2005 3:47 PM
Hi. I would be grateful for any help with a project I have to work on - my first time trying to use a class. I have to use a class to take average values that are entered on a form via text boxes. The problem I'm encountering is that the second textbox value is not being added to the first value, rather the first value is simply being divided by 2 in the Average function. So for example if I type 10 for the first value and 20 for the second value, the average should be 15, but the output displays 5. Thanks much, kyma The code that I have for the Animal class: Public Class cAnimal 'Class Name: cAnimal 'Description: Handles animal information #Region "Properties" 'Member variables Private mstrName1, mstrName2 As String Private mdecWeight1, mdecWeight2 As Decimal Private mblnWeightError1, mblnWeightError2 As Boolean Property Name1() As String Get Return mstrName1 End Get Set(ByVal Value As String) mstrName1 = Value End Set End Property Property Name2() As String Get Return mstrName2 End Get Set(ByVal Value As String) mstrName2 = Value End Set End Property Property Weight1() As Decimal Get Return mdecWeight1 End Get Set(ByVal Value As Decimal) If Value > 0 Then mdecWeight1 = Value mblnWeightError1 = False Else MessageBox.Show("Weight must be greater than 0", "Error") mblnWeightError1 = True End If End Set End Property Property Weight2() As Decimal Get Return mdecWeight2 End Get Set(ByVal Value As Decimal) If Value > 0 Then mdecWeight2 = Value mblnWeightError2 = False Else MessageBox.Show("Weight must be greater than 0", "Error") mblnWeightError2 = True End If End Set End Property ReadOnly Property WeightFlag1() As Boolean Get Return mblnWeightError1 End Get End Property ReadOnly Property WeightFlag2() As Boolean Get Return mblnWeightError2 End Get End Property #End Region #Region "Methods" 'Sub New() is a constructor. 'Constructors are executed when an object of the class is created. 'Constructors are used for initializing member variables. Public Sub New() mstrName1 = "" mdecWeight1 = 0 mblnWeightError1 = False mstrName2 = "" mdecWeight2 = 0 mblnWeightError2 = False End Sub 'Other class methods Public Sub Clear() mstrName1 = "" mdecWeight1 = 0 mblnWeightError1 = False mstrName2 = "" mdecWeight2 = 0 mblnWeightError2 = False End Sub Public Function Average() As Decimal Dim decAverage As Decimal decAverage = (mdecWeight1 + mdecWeight2) / 2 Return decAverage End Function #End Region End Class The code that I have for the form: Option Strict On Imports System.Convert Public Class frmLab3amk Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " ' snipped code #End Region 'Declare and instantiate a variable to be an object of Animal class type Dim mAnimal As New cAnimal Private Sub btnAverageWeight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAverageWeight.Click Try mAnimal.Name1 = txtAnimal1.Text 'Try...Catch will not invalidate zero-length strings assigned 'to another variable of string data type. If mAnimal.Name1 = "" Then MessageBox.Show("Must enter a valid animal id", "Error") txtAnimal1.Focus() Exit Sub End If mAnimal.Weight1 = ToDecimal(txtWeight1.Text) Catch MessageBox.Show("Error in Weight field", "Error") txtWeight1.Focus() Exit Sub Try mAnimal.Name2 = txtAnimal2.Text 'Try...Catch will not invalidate zero-length strings assigned 'to another variable of string data type. If mAnimal.Name2 = "" Then MessageBox.Show("Must enter a valid animal id", "Error") txtAnimal2.Focus() Exit Sub End If mAnimal.Weight2 = ToDecimal(txtWeight2.Text) Catch MessageBox.Show("Error in Weight field", "Error") txtWeight2.Focus() Exit Sub End Try End Try 'Display average only if input data is valid 'Use class member variables to check validity If Not mAnimal.WeightFlag1 Then If Not mAnimal.WeightFlag2 Then lblOutput.Text = Format(mAnimal.Average) End If End If End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click 'Clear the object 'Clear the text boxes mAnimal.Clear() Call Clear() End Sub Private Sub Clear() 'Clears the form's controls txtAnimal1.Clear() txtWeight1.Clear() txtAnimal2.Clear() txtWeight2.Clear() lblOutput.Text = Nothing End Sub Private Sub btnQuit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuit.Click Me.Close() End Sub End Class