I'm trying to build a pizza application and for the sides, based on what's selected they need to be added up and put into a total. I'm using a function to do this, but its only returning the value from the first selection I make in the combo box, and doesn't go through the if to continue to add the remaining values, I'm confused as to why its doing this. My code for the function is below.
Function sides() As Decimal
Dim total As Decimal
If cmboSides.SelectedIndex.Equals(0) Then
total = total + 4.99
ElseIf cmboSides.SelectedIndex.Equals(1) Then
total = total + 6.99
ElseIf cmboSides.SelectedIndex.Equals(2) Then
total = total + 6.99
ElseIf cmboSides.SelectedIndex.Equals(3) Then
total = total + 5.99
ElseIf cmboSides.SelectedIndex.Equals(4) Then
total = total + 6.99
ElseIf cmboSides.SelectedIndex.Equals(5) Then
total = total + 7.99
End If
Return total
End Function
The specific issue here is that you are declaring total inside that method. As a result, it starts at zero every time you call the method so you only get the most recent value. If you expect the running total to be used each time then its value needs to persist between calls to that method, which means that it must be declared outside that method:
Private total As Decimal
Function sides() As Decimal
If cmboSides.SelectedIndex.Equals(0) Then
total += 4.99D
ElseIf cmboSides.SelectedIndex.Equals(1) Then
total += 6.99D
ElseIf cmboSides.SelectedIndex.Equals(2) Then
total += 6.99D
ElseIf cmboSides.SelectedIndex.Equals(3) Then
total += 5.99D
ElseIf cmboSides.SelectedIndex.Equals(4) Then
total += 6.99D
ElseIf cmboSides.SelectedIndex.Equals(5) Then
total += 7.99D
End If
Return total
End Function
There are various other things you could do to improve that code too but this addresses the specific issue that you're asking about.
Store each price as a value then use
Dim total As Decimal
For Each item In lstSides.SelectedItems
total+= CDbl(lstSides.GetItemValue(item))
Next
The most basic solution, assuming you are actually using a ListBox (rather than a ComboBox which can't be set to multiselect), is this:
Function sides() As Decimal
Dim total As Decimal
If lstSides.SelectedIndices.Contains(0) Then total += 4.99
If lstSides.SelectedIndices.Contains(1) Then total += 6.99
Etc
Return total
End Function
Your current code will never move to a second ElseIf after one has been evaluated as true, similar to a Case Select statement.
A much better solution would be to use data binding to a class of Pizza with properties such a Price, Availability, Size, etc.
Related
I need some help getting this code to work. I need the code to calculate the first 100 even and odd numbers. The code below shows the even portion, but I assume they both will work the same way, just with different numbers. I know that the first 100 even numbers are 2 to 200 or 0 to 200?, and the first 100 odd numbers are 1 to 199. (I also need to use while loops, would I use a separate while loop to determine how the code calculate the sum?)
here is the code.
Dim sum, count As Integer 'the sum and count are changing and they must be a whole number.
Const num As Integer = 2
'now we must deveolp a while loop/equation that adds every number lower or equal to 100 together.
While count Mod 2 <= 200 'while the count is smaller or equal to 100,
count =
sum += count 'The sum will be add or equaled to the count
count += 1 'The count will be added to equaled to 1
End While
Console.WriteLine("The Sum of the first 100 even numbers is: {0}!", sum) 'This is the text the user sees, which tells them that the sum of the first 100 numbers will be 5050.
Console.ReadLine()
End Sub
As usual the math portion is giving me trouble. I feel like I have the right idea, but I cant execute it properly.
Dim sumEven, sumOdd As Integer
Dim even = 2
Dim odd = 1
While even <= 200 AndAlso odd <= 200
sumEven += even
sumOdd += odd
even += 2
odd += 2
End While
Console.WriteLine("The Sum of the first 100 even numbers is: {0}!", sumEven)
Console.WriteLine("The Sum of the first 100 odd numbers is: {0}!", sumOdd)
Simple calculation TotalPrice = QTY * UnitPrice
Does not calculate when I get the Unit Price to display as currency.
The unit price is retrieved from a combo box in the AfterUpdate event as follows:
Private Sub cboItemRequested_AfterUpdate()
' 0 1 2 3 4 5 6
' ITEM Category SIZE UI PRICE NSN_ORDER UNIT_PACK
With Me
.txtDescrOfItemRequested = .cboItemRequested
.txtUI = .cboItemRequested.Column(3)
.txtQTY = 1
.txtUnitPrice = Format(.cboItemRequested.Column(4), "Currency")
.txtPartNumNSN = .cboItemRequested.Column(5)
End With
End Sub
Even though the combo box displays the currency correctly in the 4th column, it does not populate the Unit Price txtbox correctly unless I apply the Format(XXXXX, "Currency"). Incidentally, the textbox is also formatted as currency. However, when I do get the dollar sign to appear, the final calculation TotalPrice remains zero.
I even applied the following to the txtTotalPrice
=Val(Nz([txtQTY],0))*Val(Nz([txtUnitPrice],0))
I tried responding to #krish KM so I scrapped everything above and instead created this function:
Public Function cTotalPrice(vQTY As Variant, vUnitPrice As Variant) As Currency
'only return a value if both fields are numeric
If IsNumeric(vQTY) = True And IsNumeric(vUnitPrice) = True Then
cTotalPrice = vQTY * vUnitPrice
End If
End Function
which I only call like this the AfterUpdate event of the cboItemRequested box and any time txtQTY or txtUnitPrice are updated
.txtTotalPrice = cTotalPrice(.txtQTY, .txtUnitPrice)
But txtUnitPrice simply does not display as currency even with txtUnitPrice formatted as currency.
SOLUTION:
I needed to Format the txtUnitPrice with VBA
.txtUnitPrice = Format(.cboItemRequested.Column(4), "Currency")
Yes, the txtBoxPrice formatted as currency just ignores it for some reason.
But in order for the TotalPrice to come out right, it was necessary to recalulate it at the end. This was the first tip I was given. Thanks krish KM
I have a problem for this code. lstTotal is the error. It says Expressions is not a method for the lstTotal. Im not very knowledgable in coding yet so any help would be appreciated.
Private Sub UpdateTotal()
' Clear the previous subtotal, tax and total
lstTotal.Items.Clear()
' Compute and display the subtotal
lstTotal.Items.Add("SUB TOTAL = ")(subtotal.ToString("C"))
' Compute and Display the tax
Tax = subtotal * TAX_RATE
lstTotal.Items.Add(" TAX=" & Tax.ToString("C"))
' Compute and display the total
lstOrderReceipt.Items.Add("---------------")
End Sub
This doesn't make sense:
lstTotal.Items.Add("SUB TOTAL = ")(subtotal.ToString("C"))
Presumably you mean this:
lstTotal.Items.Add("SUB TOTAL = " & subtotal.ToString("C"))
The error message is saying that you are doing this:
Expression(subtotal.ToString("C"))
which is how you would call a method but what you have in place of Expression, i.e. lstTotal.Items.Add("SUB TOTAL = ") does not evaluate to a method so your code doesn't make sense.
I have data like: (id,volume,median.percantile)
1 Normal Normal Low
2 Low Normal High
3 High High Normal
What I need to do is to have a counter that it increases by 1 when it is not Normal. So I expect
1 Normal Normal Low Counter:1
2 Low Normal High Counter:2
3 High High Low Counter:3
To provide it I wrote the function below. However, when I send there (Normal,Normal,High), it is calculating it 2 instead 1. So it is not working correctly. Where is my mistake?
My code is below:
Public Function getPoint(ByVal volume As String, ByVal median As String, ByVal percantile As String) As Integer
Dim Total As Integer = 0
If volume = “Normal” then
total = total + 1
end if
If median = “Normal” then
total = total + 1
end if
If percantile = “Normal” then
total = total + 1
end if
return total
End Function
In the first record, both volume and median are "Normal". So both of these conditions are true:
If volume = "Normal" Then
total = total + 1
End If
If median = "Normal" Then
total = total + 1
End If
Thus, total gets incremented twice. If you want it to be incremented only once (and essentially ignore the remaining comparisons) then use ElseIf to stop the comparisons once a condition has been satisfied:
If volume = "Normal" Then
total = total + 1
ElseIf median = "Normal" Then
total = total + 1
End If
Or just put them into a single condition:
If volume = "Normal" Or median = "Normal" Then
total = total + 1
End If
Note that this behavior is exactly the opposite of what you describe:
increases by 1 when it is not Normal
A typo perhaps?
What you are doing is opposite to what you want.
You want the count of not-Normal whereas what you are counting and returning the total is Normal.
Simple fix would be return (3 - total).
Btw, is this VBA or VB.Net? Both are based on VB but are different.
If it's VBA for Excel, you can achieve this using simple count formula (3 - count of Normal).
Change your "="'s to "<>"
i.e.
If volume <> "Normal" then
total = total + 1
End if
I'm doing this program for a class; I have a listbox with values from 0-10 which are 'scores' which get recorded every time the user selects a new score (and clicks the button of course). I need to do the following;
an average of the scores, which I get through an accumulator and the counter
a number of scores, which I get through a counter
get the total scores (this is what's giving me trouble)
here's the code I have for this so far :
intScore = Convert.ToDecimal(lstScores.SelectedItem)
'the counter and accumulator
intTotal = intTotal + 1
decScoreAccumulator += intScore
' here's what calculates the average
If intScore > 0 Then
decScorAvg = decScoreAccumulator / intTotal
End If
Here's an image of it : http://i.stack.imgur.com/aomQO.png
The number and average I can get to work, since its just using the accumulator and the counter. But no matter what I do, I can't add the values selected for the total. So does anyone know how I can get it to give me total of all the selected scores?
You're code looks good, and you look like your doing everything correctly. You even already have the total in your current code:
decScoreAccumulator += intScore
Because you're adding the score each time to decScoreAccumulator, this variable has the total you're looking for (assuming it's not confined to the scope of the button click). HTH. Good luck!