find average of variables - vb.net

Full disclosure, I'm working on an assignment for COP2170, but the additional feature isn't apart of the assignment just trying to stretch a little further...
I'm trying to add 5 test scores and output the average of the 5 scores, that part works just fine. In addition I'm trying to add an additional feature that will allow the user to enter less than 5 scores and the program will still output the average of whatever scores were entered.
So this works:
And this works:
The problem I'm running into is if the first score is omitted and the rest are filled in, it doesn't work:
Here's the code I've got so far:
Public Class frmTestScoreAverage
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim decScore1, decScore2, decScore3, decScore4, decScore5 As Double ' decScore1-5 to hold test scores1-5
Dim decScoreAverage As Double ' to hold test score average
Dim intDivideBy As Double = 5 ' declare intDivideBy variable with starting value of 5
lblStatusLabel.Text = String.Empty 'set error message to empty string
Try
' Read user input convert to double and assign value to variable
decScore1 = Convert.ToDouble(txtScore1.Text)
decScore2 = Convert.ToDouble(txtScore2.Text)
decScore3 = Convert.ToDouble(txtScore3.Text)
decScore4 = Convert.ToDouble(txtScore4.Text)
decScore5 = Convert.ToDouble(txtScore5.Text)
' Calculate average
decScoreAverage = (decScore1 + decScore2 + decScore3 + decScore4 + decScore5) / intDivideBy
'display result
lblResult.Text = CStr(decScoreAverage)
Catch
' Display error message, asks for all scores
lblStatusLabel.Text = "Please enter all test scores"
'Calculate average even without all scores
For Each c As TextBox In Me.Controls.OfType(Of TextBox)() 'loop through each textbox in form see: http://stackoverflow.com/a/13504361/1947286
If c.Text = String.Empty Then intDivideBy -= 1 'If text equals empty string, Then decrement intDivideBy
Next
'catch divide by zero error
Try
'calculate average
decScoreAverage = (decScore1 + decScore2 + decScore3 + decScore4 + decScore5) / intDivideBy 'add test scores and divide to find average
'display result
lblResult.Text = CStr(decScoreAverage)
Catch
lblStatusLabel.Text = "Please enter at least one test score"
End Try
End Try
End Sub
End Class
I'm pretty sure the problem has to do with the way I'm finding the average:
decScoreAverage = (decScore1 + decScore2 + decScore3 + decScore4 + decScore5) / intDivideBy
Is there a way to find the average that will allow for empty variables in any arrangement?

The problem you are having is that Convert.ToDouble(txtBox.text) (with empty string, will make Convert throw an Exception.
Add a validation that the string isn't empty or use TryParse to see if the Textbox value is parseable to a number.
Either
If not String.IsNullOrEmpty(txtBox.Text) then
Convert.ToDouble(txtBox.text)
Or
dim value as Double
if Double.TryParse(txtBox.Text, value) then
avg += value
When the first one is empty, it goes to the Catch part of the code, then you try to calculate the avg, but the values of the Txtbox are not assigned to the declarations you made, so when it tries to do the average
decScore1 = Convert.ToDouble(txtScore1.Text) // Exception here to Catch
decScore2 = Convert.ToDouble(txtScore2.Text) // Not evaluated
decScore3 = Convert.ToDouble(txtScore3.Text) // Not evaluated
decScore4 = Convert.ToDouble(txtScore4.Text) // Not evaluated
decScore5 = Convert.ToDouble(txtScore5.Text) // Not evaluated
Theses do not have any value, in return this doesn't do what you want
decScoreAverage = (decScore1 + decScore2 + decScore3 + decScore4 + decScore5) / intDivideBy
UPDATE
(Vb code not tested)
Function GetTextValue(dim score as string) as Double
dim value as Double = 0
if (Double.TryParse(score, value))
return value
else
return value
End Function
This
decScore1 = Convert.ToDouble(txtScore1.Text)
Becomes
decScore1 = GetTextValue(txtScore1.Text)

I suggest you store reference to each textbox in an array. You'll see why at the end of this example.
Public Class frmTestScoreAverage
Public Sub New()
Me.InitializeComponent()
Me.boxes = {Me.txtScore1, Me.txtScore2, Me.txtScore3, Me.txtScore4, Me.txtScore5}
End Sub
Private boxes As TextBox()
The common way to validate user input in winforms is to handle the Validating event, usually combined with the error provider class as mentioned by Hans Passant.
Private Sub HandleScoreValidating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtScore1.Validating, txtScore2.Validating, txtScore3.Validating, txtScore4.Validating, txtScore5.Validating
With DirectCast(sender, TextBox)
If ((Not String.IsNullOrEmpty(.Text)) AndAlso (Not Double.TryParse(.Text, 0.0R))) Then
e.Cancel = True
'Alert or set error provider:
'Me.ErrorProvider1.SetError(DirectCast(sender, Control), "Not double")
Else
e.Cancel = False
'Clear error provider:
'Me.ErrorProvider1.Clear()
End If
End With
End Sub
Now, back to the textbox array. Create a new list of double and iterate the textbox array. If the text isn't empty, parse and add the value to the list. At the end, use the Average extension method to get the average value.
Private Sub HandleCalculate(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim values As New List(Of Double)
For Each box As TextBox In Me.boxes
If (Not String.IsNullOrEmpty(box.Text)) Then
values.Add(Double.Parse(box.Text))
'Else
' values.Add(0.0R)
End If
Next
Dim average As Double = values.Average()
'....
End Sub
End Class

Related

If I click the calculate button the program crashes?

Just for context;
I need to calculate the average of 5 numbers located in 5 textboxes.
Nummer means number
Gemiddelde means average
and
Bereken means calculate
What is causing it to crash?
Private Sub butBereken_Click(sender As Object, e As EventArgs) Handles butBereken.Click
'Variabelen'
Dim nummer1 As Decimal = txtNummer1.Text
Dim nummer2 As Decimal = txtNummer2.Text
Dim nummer3 As Decimal = txtNummer3.Text
Dim nummer4 As Decimal = txtNummer4.Text
Dim nummer5 As Decimal = txtNummer5.Text
Dim somNummers As Decimal = nummer1 + nummer2 + nummer3 + nummer4 + nummer5
Dim Gemiddelde As String = (somNummers) / 5
lblGemiddelde.Text = Gemiddelde
If Gemiddelde < 5.5 Then
lblGemiddelde.Text = Gemiddelde + " Dit is onvoldoende"
End If
If nummer1 = "" Or nummer2 = "" Or nummer3 = "" Or
nummer4 = "" Or nummer5 = "" Then
butBereken.Enabled = False
MessageBox.Show("your mom")
Else
butBereken.Enabled = True
End If
End Sub
I don't see what would crash the program but check to that the TextBoxes have values before assigning them to numeric variables. A Decimal value will never = "".
Private Sub butBereken_Click(sender As Object, e As EventArgs) Handles butBereken.Click 'Variabelen'
If Not IsNumeric(txtNummer1.Text) Or _
Not IsNumeric(txtNummer2.Text) Or _
Not IsNumeric(txtNummer3.Text) Or _
Not IsNumeric(txtNummer4.Text) Or _
Not IsNumeric(txtNummer5.Text) Then
MessageBox.Show ("your mom wants you to fill in all the number boxes")
Exit Sub
End If
Dim nummer1 As Decimal = CDec(txtNummer1.Text)
Dim nummer2 As Decimal = CDec(txtNummer2.Text)
Dim nummer3 As Decimal = CDec(txtNummer3.Text)
Dim nummer4 As Decimal = CDec(txtNummer4.Text)
Dim nummer5 As Decimal = CDec(txtNummer5.Text)
Dim somNummers As Decimal = nummer1 + nummer2 + nummer3 + nummer4 + nummer5
Dim Gemiddelde As String = (somNummers) / 5
lblGemiddelde.Text = Gemiddelde
If Gemiddelde < 5.5 Then
lblGemiddelde.Text = Gemiddelde + "Dit is onvoldoende"
End If
If nummer1 = 0 Or nummer2 = 0 Or nummer3 = 0 Or nummer4 = 0 Or nummer5 = 0 Then
butBereken.Enabled = False
MessageBox.Show ("your mom")
Else
butBereken.Enabled = True
End If
End Sub
If this doesn't work I would consider setting breakpoints in the could to determine what line is causing the crash.
If that doesn't work consider adding this line to the form's initialization:
butBereken.Caption = "Warning: Do not Click!"
Assuming the user populated all the textboxes with numeric only data, (and you have checked this) try replacing these lines in your code with this code
Dim nummer1 As Decimal = txtNummer1.Text
Dim nummer2 As Decimal = txtNummer2.Text
Dim nummer3 As Decimal = txtNummer3.Text
Dim nummer4 As Decimal = txtNummer4.Text
Dim nummer5 As Decimal = txtNummer5.Text
Dim somNummers As Decimal = nummer1 + nummer2 + nummer3 + nummer4 + nummer5
Dim Gemiddelde As Decimal = somNummers / 5
lblGemiddelde.Text = Gemiddelde.ToString("##0.0")
I'd do something more like:
Private Sub butBereken_Click(sender As Object, e As EventArgs) Handles butBereken.Click
Dim TBs() As TextBox = {txtNummer1, txtNummer2, txtNummer3, txtNummer4, txtNummer5}
Dim inputs() As String = TBs.Select(Function(x) x.Text).ToArray()
Dim values() As Decimal
Try
values = Array.ConvertAll(inputs, Function(s) Decimal.Parse(s))
Dim Gemiddelde As String = values.Average()
lblGemiddelde.Text = Gemiddelde & If(Gemiddelde < 5.5, " Dit is onvoldoende", "")
Catch ex As Exception
MessageBox.Show("your mom")
End Try
End Sub
I prefer this approach as it doesn't require repetitive lines of code, manually converting each TextBox to a Decimal. Since the TextBoxes are in an Array, we could also add another TextBox to the form and then add that name to the end of the Array. The rest of the code would not need to change at all; it would still just work as is.
From the Array of TextBox, we use a LINQ statement to extract the Text from each TextBox and add it to an Array of String called "inputs". From there, we convert every single String to a Decimal using Array.ConvertAll(), again avoiding repetitive code. If any of the input values is not a valid Decimal then an Exception will be thrown and we'll jump the the Catch block where the not so well written error message is displayed.
If there are no exceptions, then all String inputs were successfully converted to Decimals and stored in the "values" Array. Next we simply use the LINQ function Average() to get the average of all the values.
Lastly we display the computed average in the Label, adding the "Dit is onvoldoende" message if approapriate. The If() function used in this line is simply a shorthand version of a longer If...Else...End If statement.
In your original attempt, it looks like you wanted to disable the button if any of the values are blank (or maybe if they are invalid decimals?):
If nummer1 = "" Or nummer2 = "" Or nummer3 = "" Or nummer4 = "" Or nummer5 = "" Then
butBereken.Enabled = False
MessageBox.Show("your mom")
Else
butBereken.Enabled = True
End If
This makes no sense as if you disable the button, how would it get turned back on so that user could click it again?
A different approach would be to handle the TextChanged() event of all the TextBoxes and simply update your Label with the average in real-time whenever one of the TextBoxes is changed:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
UpdateAverage()
End Sub
Private Sub txtAll_TextChanged(sender As Object, e As EventArgs) Handles txtNummer5.TextChanged, txtNummer4.TextChanged, txtNummer3.TextChanged, txtNummer2.TextChanged, txtNummer1.TextChanged
UpdateAverage()
End Sub
Private Sub UpdateAverage()
Dim TBs() As TextBox = {txtNummer1, txtNummer2, txtNummer3, txtNummer4, txtNummer5}
Dim inputs() As String = TBs.Select(Function(x) x.Text).ToArray()
Try
Dim values() As Decimal = Array.ConvertAll(inputs, Function(s) Decimal.Parse(s))
Dim average As Decimal = values.Average
lblGemiddelde.Text = average & If(average < 5.5, " Dit is onvoldoende", "")
Catch ex As Exception
lblGemiddelde.Text = "{ Invalid Input }"
End Try
End Sub
End Class
Sample run:

Need some assistance with a Area Calculator in Visual Basics

I need some help with my code in a Rectangle calculator... So I made a rectangle area calculator that features Length, Width, Area, Number of Rectangles, and Smallest Rectangle... I am also creating a catch bock that handles invalid-cast extractions 2 of them... Also a A expectation class when the value of the result is greater then 1 Million... Here is my code
Public Class Form1
Dim area As Decimal
Dim numberofRectangles As Integer
Dim smallestRectangle As Decimal
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Try
Dim length As Decimal = CDec(txtLength.Text)
Dim width As Decimal = CDec(txtWidth.Text)
Dim Area As Decimal = width * length
Dim numberofRectangles As Integer = numberofRectangles + 1
Dim smallestRectangle As Decimal = Math.Min(smallestRectangle, Area)
txtArea.Text = Area.ToString("n2")
txtNumberOfRectangles.Text = numberofRectangles.ToString
txtSmallestRectangle.Text = smallestRectangle.ToString("n2")
txtLength.Select()
Catch ex As InvalidCastException
MessageBox.Show("Please check entries for valid numeric data",
ex.GetType.ToString)
Catch ex As OverflowException
MessageBox.Show("Please check to make sure entries aren't too large.",
ex.GetType.ToString)
Catch ex As Exception
MessageBox.Show(ex.Message & vbNewLine & vbNewLine & ex.StackTrace,
ex.GetType.ToString)
If area < 1000000000 Then
Throw New FormatException("The rectangle Is too large!")
Return
End If
Finally
End Try
End Sub
For one I am having a error where my smallest rectangle is at 999,999,999,00 and For the expectation when the value of the result is greater then 1 Million I am having trouble as you can probably see from the code. Looking for some advice on my code
Edit: Fix the top part Now getting 0.00 in the smallest rectangle box
Should I being using me.compute somehwhere aswell
You misspelled smallestRecntangle - this is why you have needed to redeclare it - it's different to the class variable smallestRectangle. For this reason, you never set smallestRectangle to anything other than 999999999.
Also - you can use a value called Decimal.MaxValue to do the same thing as 999999999, but using the actual maximum possible decimal number. However, I would recommend instead you use Decimal? and assign to null initially. In code, have a check which "is if smallestRectangle is null, then set smallestRectangle, otherwise set it to the minimum of smallestRectangle and area".
Exception handling is for unexpected errors that are out of your control. We can test the user input so we don't need exception handling for that.
ex.GetType.ToString
This doesn't make sense. If you want to display the Exception message you can use and ampersand & and ex.Message. ex.ToString generally provides more information than you want a user to see.
TryParse will check if the string provided in the first parameter can be converted. It returns True or False. If True it will assign the converted string to the second parameter.
numberofRectangles += 1
is a shortcut way of writing
numberofRectangle = numberofRectangles + 1
The only line of code that could throw an exception is the area calculation so we will limit the Try block to that line.
Your main problem was using the Dim statement for numberofRectangles and smallestRectangle inside the Sub. These variables loose there value at End Sub. They only exist inside the Sub. Although Dim has the same meaning as Private for declaring Class level varaibles, Private is preferred. Dim is used for local variable in a method.
To avoid the problem of having smallest rectangle always being zero, I checked the count and set smallestRectangle to the first rectangles area. After that Math.Min takes over.
Private numberofRectangles As Integer
Private smallestRectangle As Decimal
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim length As Decimal
Dim width As Decimal
If Not Decimal.TryParse(txtLength.Text, length) Then
MessageBox.Show("Please check entries for valid numeric data")
Exit Sub
End If
If Not Decimal.TryParse(txtWidth.Text, width) Then
MessageBox.Show("Please check entries for valid numeric data")
Exit Sub
End If
Dim Area As Decimal
Try
Area = width * length
Catch ex As OverflowException
MessageBox.Show("Please check to make sure entries aren't too large." &
ex.Message)
Exit Sub
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
numberofRectangles += 1
If numberofRectangles < 2 Then
smallestRectangle = Area
Else
smallestRectangle = Math.Min(smallestRectangle, Area)
End If
txtArea.Text = area.ToString("n2")
txtNumberOfRectangles.Text = numberofRectangles.ToString
txtSmallestRectangle.Text = smallestRectangle.ToString("n2")
txtLength.Select()
End Sub

Invalid Conversion - Cast Exception: 'System.InvalidCastException'

I have written a code that works perfectly as long as the user enters the information in correctly. If the user does enter a number in the text box, I have an error message that tells the user data must be entered. When this happens I am getting a cast exception:
Exception thrown: 'System.InvalidCastException' in Microsoft.VisualBasic.dll
Additional information: Conversion from string "" to type 'Integer' is not valid.
I am new to this and I would appreciate any suggestions.
The exception occurs on this line:
intDetermineSubTotalCost(intSiteSelected, CInt(txtNumberOfNights.Text))
If I remove the option strict on, it will work with expected conversation, however the project requires it to be on.
Dim intSiteSelected As Double
Dim intNumberOfNights As Integer
Dim blnNightsStayingIsVaild As Boolean = False
Dim blnDiscountIsSelected As Boolean = False
Dim intDiscountChoice As Integer
Dim strDiscountSelected As String = ""
' Call a function to confirm if the Number of Nights staying is valid.
blnNightsStayingIsVaild = ValidateNightsStaying()
' call a function to confirm a discount has been seected.
intDiscountChoice = ValidateSelectedDiscount(blnDiscountIsSelected, strDiscountSelected)
' If number of nights staying and available discount has been selected,
' calculate the subtotal, tax, and final cost.
If (blnNightsStayingIsVaild And blnDiscountIsSelected) Then
intNumberOfNights = Convert.ToInt32(txtNumberOfNights.Text)
intSiteSelected = Me.cmbSelectASite.SelectedIndex
End If
If cmbSelectASite.SelectedIndex = 0 Then
intSiteSelected = 20D
ElseIf cmbSelectASite.SelectedIndex = 1 Then
intSiteSelected = 35D
ElseIf cmbSelectASite.SelectedIndex = 2 Then
intSiteSelected = 55D
End If
intDetermineSubTotalCost(intSiteSelected, CInt(txtNumberOfNights.Text))
Private Function ValidateNightsStaying() As Boolean
' This function validate the value entered for the number of nights staying in campground.
Dim intNightsStaying As Integer
Dim blnValidityCheck As Boolean = False
Dim StrNumberOfNightsErrorMessage As String =
"Please Enter The Number Of Nights You Are Staying (1-99)"
Dim strMessageBoxTitle As String = " You Must Choose Number Of Nights"
Try
intNightsStaying = Convert.ToInt32(txtNumberOfNights.Text)
If intNightsStaying > 0 And intNightsStaying < 100 Then
blnValidityCheck = True
Else
MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle)
blnValidityCheck = True
End If
Catch Exception As FormatException
MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle)
txtNumberOfNights.Focus()
txtNumberOfNights.Clear()
Catch execption As OverflowException
MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle)
txtNumberOfNights.Focus()
txtNumberOfNights.Clear()
Catch execption As SystemException
MsgBox(StrNumberOfNightsErrorMessage, , strMessageBoxTitle)
txtNumberOfNights.Focus()
txtNumberOfNights.Clear()
End Try
Return blnValidityCheck
End Function
You could look at something like this that would restrict them from entering non-numeric keypresses.
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'97 - 122 = Ascii codes for simple letters
'65 - 90 = Ascii codes for capital letters
'48 - 57 = Ascii codes for numbers
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End If
End Sub
Just Do some input checking before your perform you calculations.
For Example A Textbox requiring Input:
If txtInput.text.Contains("whatever you don't want there") Then
MessageBox.Show("Please Enter The Correct Info")
txtInput.Focus()
Return
End if
Or
If Integer.TryParse(txtInput.Text, input) Then
MessageBox.Show("Please Enter Some Text.")
txtInput.Focus()
Return
End If
This Stops the code crashing if there is an error and gives the user the opportunity to fix their input. Once all the input has been checked, perform your calculations.
There are a few ways to solve this.
You could check that the string is not empty and validate on that.
Change the conversion to a TryParse which IIRC will return 0 on empty
If possible, change the input from a textbox to a numeric up/down control with min and max values of 1 and 99 respectively.
Change your TextBox to a Masked TextBox. Allow only numbers in the mask.
https://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox%28v=vs.110%29.aspx

VB.NET - Accumulating Numbers and Calculating Average

I have started over and everything is working as intended except for 2 of the accumulating totals, the calculation is not working the way I need it to. Instead of adding previous sales to the total it tacks the new sale on the end...
for example: I input 1 Snowboard and 1 Snowboard with Boots, this works, 1 is shown in the summary for both Snowboards and Snowboards with Boots. However when I try to input again in order to keep a running total of sales I have a problem, I enter 1 Snowboard and 1 Snowboard with Boots, and the summaries show 11 rather than 2. Why does this happen?
' Declare module-level variables and constants.
Private SnowBoardsSold, BootsSold, SaleCount As Integer
Private ItemsSold As Integer
Private TotalSales As Decimal
Const SNOWBOARD_RATE As Decimal = 20D
Const BOOTS_RATE As Decimal = 30D
Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
' Calculate the prices
Dim SnowBoards, Boots As Integer
Dim SnowBoardSale, BootsSale, ThisSale, AverageSalesEver As Decimal
Try
' Convert the Snowboard input to numeric variable.
SnowBoards = Integer.Parse(SnowboardsTextBox.Text)
Try
' Convert Boots if Snowboard was successful.
Boots = Integer.Parse(WithBootsTextBox.Text)
' Calculate values for sale.
SnowBoardSale = SnowBoards * SNOWBOARD_RATE
BootsSale = Boots * BOOTS_RATE
ThisSale = SnowBoardSale + BootsSale
' Calculate summary values.
TotalSales += ThisSale
BootsSold += BootsSale
SnowBoardsSold += SnowBoardSale
SaleCount += 1
AverageSalesEver = TotalSales / SaleCount
' Format and display prices for the sale.
SnowBoardsPriceTextBox.Text = SnowBoardSale.ToString("c")
BootsPriceTextBox.Text = BootsSale.ToString("c")
TotalPriceTextBox.Text = ThisSale.ToString("c")
' Format and display values for the summary.
SnowBoardRentalTextBox.Text += SnowBoards.ToString()
BootsRentalTextBox.Text += Boots.ToString()
TotalChargesTextBox.Text = TotalSales.ToString("c")
AverageChargeTextBox.Text = AverageSalesEver.ToString("c")
Catch BootsException As FormatException
' Handle a Boots exception.
MessageBox.Show("Please enter numbers.", "Data Entry Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
With WithBootsTextBox
.Focus()
.SelectAll()
End With
End Try
Catch SnowBoardsException As FormatException
' Handle a SnowBoard exception.
MessageBox.Show("Please enter numbers.", "Data Entry Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
With SnowboardsTextBox
.Focus()
.SelectAll()
End With
Catch AnException As Exception
'Handle any other exception.
MessageBox.Show("Error: " & AnException.Message)
End Try
End Sub
First, you might want to change the way you grab the numbers:
'Convert snowboard input value to numeric variable.
SnowboardInteger = Integer.Parse(SnowboardsTextBox.Text)
to:
Dim SnowboardInteger As Integer
If TryParse.Integer(SnowboardsTextBox.Text, SnowboardInteger) = False then
' if it parses, SnowboardInteger will have the value,
'else the function returns false
MessageBox.Show("Please enter numbers")
End if
Also, you are parsing the textboxes twice. Once in the declaration then right after. This doesnt look right:
SnowboardSaleCountInteger += 1
WithBootsSaleCountInteger += 1
TotalSaleCountInteger += TotalChargesSumInteger ' ?????
AverageChargeInteger = TotalChargesSumInteger / TotalSaleCountInteger
Adding 'charges' to a 'counter' doesnt seem right. If you want the avg shouldnt it be:
TotalSaleCountInteger = SnowboardSaleCountInteger + WithBootsSaleCountInteger
If you want fractions such as 'xx.yy' in the result, AverageChargeInteger should be a Double or Decimal, unless whole dollars are ok for the assignment.
If you need to accumulate from previous clicks, then you need to move some declarations out of the procedure up by SnowboardSumInteger, WithBootsSumInteger so they can accumulate. As it is, WithBootsSaleCountInteger, TotalSaleCountInteger dont do anything and are always 1. I wonder if as a 'SaleCounter' they shouldnt increase by the value in the textbox rather than just 1 (dont have the assignment in front of me).
One more thing you might need to do is here:
SnowboardPriceInteger = SnowboardInteger * SNOWBOARD_RENTAL_RATE
WithBootsPriceInteger = WithBootsInteger * WITH_BOOTS_RENTAL_RATE
TotalPriceInteger = SnowboardPriceInteger + WithBootsPriceInteger
Your constants are Decimal (SNOWBOARD_RENTAL_RATE) so, the result of the calc has to go into a Decimal var, but SnowboardPriceInteger and the other are not. An integer cannot store a fractional result from the multiplication or division.
something like:
' sales accumulators
Private TotalSales As Decimal = 0
Private ItemsSold As Integer = 0
Click Event:
' this sale:
Dim Snowboards As Integer
Dim Boots As Integer
' get the values from the text boxes, then:
Dim SnowBoardSale As Decimal = (SnowBoards * SnowBoardRate)
Dim BootsSale As Decimal = (Boots * BootsRate)
Dim ThisSale As Decimal = SnowBoardSale + BootsSale
' update accumulators
TotalSales += ThisSale
ItemsSold += (Boots + Snowboards)
' calc the average
Dim AverageSalesEver AS Decimal = TotalSales / ItemsSold
' Display results in textboxes
'(this exercise is left to the student ;) )
HTH
I think you just confused yourself with too many variables and ones left over from old attempts.

Default number in a text box

Foreword: This is code for a game.
On one of the forms on my VB program I have a large number of text boxes, the issue is regarding the following code:
Total_Gold.Text = (WoodPrice * TxtBoxWoodUnits.Text) + (MagicWoodPrice * TxtBoxMagicWoodUnits.Text) + (Pheonix_EggsPrice * TxtBoxPheonixEggsUnits.Text) + (MetalPrice * TxtBoxMetalUnits.Text) + (Dwarven_GemsPrice * TxtBoxDwarvenGemsUnits.Text) + (LeatherPrice * TxtBoxLeatherUnits.Text) + (Dragon_ScalesPrice * TxtBoxDragonScalesUnits.Text) + (Raw_SilverPrice * TxtBoxRawSilverUnits.Text) + (Raw_GoldPrice * TxtBoxRawGoldUnits.Text) + (DownPrice * TxtBoxDownUnits.Text) + (CottonPrice * TxtBoxCottonUnits.Text) + (QuicksilverPrice * TxtBoxQuicksilverUnits.Text) + (StonePrice * TxtBoxStoneUnits.Text) + (CoalPrice * TxtBoxCoalUnits.Text) + (ThreadPrice * TxtBoxThreadUnits.Text) + (FurPrice * TxtBoxFurUnits.Text)
This is contained in a timer event that shows how much it would cost the player to purchase all of the items. This works as intended, until the text box is blank. Each "section" draws information from one of 16 text boxes.
My question is this: Is it possible to have each text box to have a "default" number that gets put into a text box when it is empty.
This is needed to be done for all of the text boxes, so either a loop or a non specific code slice would be preferred.
The short answer is no.
The long answer is yes, you can.
Create your own textbox that inherits from the TextBox class, adding a property called "DefaultValue" Override the appropriate property/method adding logic for the default value.
Then simply replace the standard textbox with your textbox.
I hope you are validating the input as well because if TxtBoxWoodUnits.Text = "Foo" and you multiply it by 4.5, it's not gonna be pretty. You should turn on Error notifications for Implicit conversions as well, so you get compiler warnings when you try to multiply strings by numbers.
Public Class MyTextBox
Inherits TextBox
Public Property DefaultValue As String
Public Overrides Property Text As String
Get
If String.IsNullOrWhiteSpace(MyBase.Text) Then
Return DefaultValue
Else
Return MyBase.Text
End If
End Get
Set(ByVal value As String)
MyBase.Text = value
End Set
End Property
End Class
Well in the designer you could put the text value as "0", then in the textchanged event handler do this:
If TxtBoxWoodUnits.Text = "" Then
TxtBoxWoodUnits.Text = "0"
EndIf
Basically everytime the text of your textbox is changed, it checks if it is null, if it is, then make the value of textbox "0".
Hope this helps.
Private Sub btn_generate_text_array_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click ' will generate a group of 10 text boxes
Dim j As Integer
For j = 0 To 10
ReDim Preserve c(j)
c(j) = New TextBox
c(j).Name = "txt" & j
c(j).Parent = Me
c(j).Top = j * c(j).PreferredHeight + 2
c(j).Tag = j
c(j).Visible = True
Next
End Sub
Private Sub c_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
For j = 0 To 10 'loop will load 0 to all the text boxes having ""(null) values
If c(j).Text = "" Then
c(j).Text = "0"
End If
Next
End Sub