Visual Basic Currency Error keeps give me the value of C - vb.net

I'm new to coding on Visual Basics and was wondering if anyone knew why the end total cost isn't working. Currently I keep getting C as the end value
Here is the variables bit so you can see the data types I have set
Dim Height As Decimal
Dim HeightValidation As Boolean
Dim Width As Decimal
Dim WidthValidation As Boolean
Dim TotalArea As Decimal
Dim Paint As String
Dim PaintValidation As Boolean
Dim Cost As Decimal
Dim FinalCost As Decimal
Dim UndercoatValidation As Boolean
Dim Undercoat As String
And here is the last bit of the code
Do Until UndercoatValidation = True
UnderCoat = InputBox("Would you like to add an Undercoat to your purchase?")
If Undercoat = "Yes" Then
FinalCost = Cost + (TotalArea * 0.5)
UndercoatValidation = True
ElseIf Undercoat = "No" Then
FinalCost = Cost
Else
UndercoatValidation = False
End If
Loop
MsgBox("Thank you for using the Paint Calculator")
MsgBox("Your total cost is"(FormatCurrency(FinalCost)))
Thanks in advance. Btw before I added the currency bit it worked.
So the values of cost will change depending on whether or not the user wishes to have an undercoat, what type of paint they use and the area of the room.
Do Until HeightValidation = True
Height = InputBox("Enter the height of the room you are workin in: ") 'Gains users value
If (2 <= Height) AndAlso (Height <= 6) Then 'Only allows number between 2 and 6
HeightValidation = True 'breaks the loop if the requirements are met
Else
HeightValidation = False 'continues the loop if the requirements are not met
End If
Loop
Do Until WidthValidation = True
Width = InputBox("Enter the width of the room you are workin in: ") 'Gains users value
If (1 <= Width) AndAlso (Width <= 25) Then 'Only allows number between 1 and 25
WidthValidation = True 'breaks the loop if the requirements are met
Else
WidthValidation = False 'continues the loop if the requirements are not met
End If
Loop
TotalArea = Height * Width
Do Until PaintValidation = True
MsgBox("There are 3 different types of paint you could you, which are: Luxary which costs £1.75 per square metre; standard which costs £1 per square metre and economy wich costs £0.45 per square metre")
Paint = InputBox("Which type of paint will you be using to paint the walls?")
If Paint = "standard" Then
Cost = TotalArea * 1
PaintValidation = True
ElseIf Paint = "economy" Then
Cost = TotalArea * 0.45
PaintValidation = True
ElseIf Paint = "luxary" Then
Cost = TotalArea * 1.75
PaintValidation = True
Else
PaintValidation = False
End If
Loop
That's the rest of the code that works out the costs, sorry for all the code not being annotated.

MsgBox("Your total cost is"(FormatCurrency(FinalCost)))
That means "show the character at position FormatCurrency(FinalCost) from the string "Your total cost is".
Apparently the result of FormatCurrency(FinalCost) is implicitly convertible to 11, so it shows the "c".
Apparently you meant
MsgBox("Your total cost is " & FormatCurrency(FinalCost))
Please use Option Strict On to not have this kind of problems in the future.

Related

I HAVE BEEN OVER THIS GOD KNOWS HOW MANY TIMES, WHAT IS WRONG WITH IT? (TICKET MACHINE) NO ERRORS ON VS), i think the top few line may not work

I have been looking over this for the last couple of days and personally I am not to keen on VB, the tab is loading up but I am not getting an answer?
Private Sub Button1_click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Age As Double
Dim seatgrading As Double
Dim Cardaddition As Single
Dim Memberdiscount As Single
Dim installments As Double
Dim totalcost As Double
Dim eachpayment As Integer
Dim total As Single
Dim price As Single
Dim Subtotal As Single
Age = CBOAge.Text
If RBFootball.Checked = True And Age = rbChild.Checked Then
price = 275
ElseIf RBFootball.Checked = True And Age = rbAdult.Checked Then
price = 450
ElseIf RBFootball.Checked = True And Age = rbOAP.Checked Then
price = 295
End If
If RBRugby.Checked = True And Age = rbChild.Checked Then
price = 85
ElseIf RBRugby.Checked = True And Age = rbAdult.Checked Then
price = 175
ElseIf RBRugby.Checked = True And Age = rbOAP.Checked Then
price = 105
End If
' Seat Grades
If RBG1.Checked = True Then
seatgrading = 150
ElseIf RBG2.Checked = True Then
seatgrading = 120
ElseIf RBG3.Checked = True Then
seatgrading = 87.5
End If
total = price + seatgrading
MemberDiscount = installments
If RBBronze.Checked = True Then
MemberDiscount = total * 0.08
ElseIf RBSilver.Checked = True Then
MemberDiscount = total * 0.09
ElseIf RBGold.Checked = True Then
`Cardaddition` = total * 0.025
End If
If RBCard.Checked = True Then
Cardaddition = Subtotal = 0.025
End If
If installments = True Then
installments = total * 0.0375
total = totalcost + installments
eachpayment = totalcost / 12
For count = 1 To 12
installments = installments & "payment" & "is" & Format(eachpayment, "currency") & vbCrLf
Next
End If
total = Subtotal - MemberDiscount + Cardaddition
total = Format(totalcost, "currency")
End Sub
This answer is perhaps more of a way to show you how to get to the code you need than a complete answer.
When dealing with money, it is best to use the Decimal type instead of Single or Double, otherwise the pennies can go wrong after a calculation or two.
You can have an If statement inside another If statement, which can sometimes make for less typing and/or easier reading.
I can't see a need for the age variable because there are already radiobuttons for child/adult/OAP.
The way of calculating the final total price was not clear to me, so I moved some things around into what may or may not be the correct order.
Perhaps the installments variable is meant to be a checkbox for if the payments are going to be taken monthly - I assumed that is the case.
I couldn't see where the results are being presented to the user, so I used MessageBox.Show - I'm sure you will be able to adapt it to how you want to show the total etc.
Private Sub Button1_click(sender As Object, e As EventArgs) Handles Button1.Click
' What I think the variable types are:
' Dim rbChild, rbAdult, rbOAP As New RadioButton
' Dim rbFootball, rbRugby As New RadioButton
' Dim rbG1, rbG2, rbG3 As New RadioButton
' Dim rbBronze, rbSilver, rbGold As New RadioButton
' Dim cbCard As New CheckBox
' Dim cbInstallments As New CheckBox
Dim price As Decimal
If rbFootball.Checked Then
If rbChild.Checked Then
price = 275
ElseIf rbAdult.Checked Then
price = 450
ElseIf rbOAP.Checked Then
price = 295
End If
End If
If rbRugby.Checked Then
If rbChild.Checked Then
price = 85
ElseIf rbAdult.Checked Then
price = 175
ElseIf rbOAP.Checked Then
price = 105
End If
End If
Dim seatgrading As Decimal
If rbG1.Checked Then
seatgrading = 150
ElseIf rbG2.Checked Then
seatgrading = 120
ElseIf rbG3.Checked Then
seatgrading = 87.5D
End If
Dim subtotal As Decimal = price + seatgrading
Dim memberDiscount As Decimal
If rbBronze.Checked Then
memberDiscount = subtotal * 0.08D
ElseIf rbSilver.Checked Then
memberDiscount = subtotal * 0.09D
ElseIf rbGold.Checked Then
memberDiscount = subtotal * 0.025D
End If
Dim cardSurcharge As Decimal
If cbCard.Checked Then
cardSurcharge = subtotal * 0.025D
End If
Dim total As Decimal = subtotal - memberDiscount + cardSurcharge
If cbInstallments.Checked Then
Dim installmentSurcharge = total * 0.0375D
total = total + installmentSurcharge
Dim eachpayment As Decimal = total / 12
MessageBox.Show("Monthly payment is " & eachpayment.ToString("C"))
End If
MessageBox.Show("Total payment is " & total.ToString("C"))
End Sub

Scroll bar for line graph VB.NET

I created a line graph in Visual Basic to show how many calories the user eats per day. However, my user requires me to include a scroll bar to scroll back and forward along the x-axis to view more days.
Unfortunately, I have never done anything like this before, and after looking through Stack Overflow and Googling, I cannot see any examples of anyone doing so.
Here is a screenshot of my graph so far:
And here is the code:
Cursor.Current = Cursors.WaitCursor
CalorieChartView = True
BurntChartView = False
NetChartView = False
Dim Series As Series = CalorieChart.Series(0)
'keeps track of if the chart is empty, starting as true
Dim empty As Boolean = True
'Clears the chart
Series.Points.Clear()
'Draws the chart in dark red
Series.Color = Color.DarkRed
'The legend text is changed
Series.LegendText = "Calories Consumed"
'For each of the past 8 days, a point is plotted with how many calories were eaten in that day
For i = -7 To 0
Series.Points.Add(User.GetCaloriesEaten(User.Username, Date.Now.AddDays(i)))
Series.Points(7 + i).AxisLabel = Date.Now.AddDays(i).ToString("dd/MM/yyyy")
'If any of the points are not 0
If User.GetCaloriesEaten(User.Username, Date.Now.AddDays(i)) <> 0 Then
'the chart is not empty
empty = False
End If
Next
HandleEmpty(empty)
Cursor.Current = Cursors.Default
I would appreciate any help.
If I understand your question you want to add a horizontal scroll bar to your graph. I have made some modification and new code to your code as for mock data purpose. Please refer the below code. You can get the idea by running this code separately.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim blockSize As Integer = 10
Cursor.Current = Cursors.WaitCursor
CalorieChartView = True
BurntChartView = False
NetChartView = False
CalorieChart.Series.Clear()
Dim series = CalorieChart.Series.Add("My Series")
series.ChartType = SeriesChartType.Line
series.XValueType = ChartValueType.Int32
'keeps track of if the chart is empty, starting as true
Dim empty As Boolean = True
'Clears the chart
series.Points.Clear()
'Draws the chart in dark red
series.Color = Color.DarkRed
'The legend text is changed
series.LegendText = "Calories Consumed"
'For each of the past 8 days, a point is plotted with how many calories were eaten in that day
Dim sizeOfDayToDisplay As Int16 = 0
For i = 0 To 100
'Series.Points.Add(User.GetCaloriesEaten(User.Username, Date.Now.AddDays(i)))
'Series.Points(7 + i).AxisLabel = Date.Now.AddDays(i).ToString("dd/MM/yyyy")
''If any of the points are not 0
'If User.GetCaloriesEaten(User.Username, Date.Now.AddDays(i)) <> 0 Then
' 'the chart is not empty
' empty = False
'End If
' just for testing purpose.
series.Points.Add(getRandumNumber())
series.Points(i).AxisLabel = Date.Now.AddDays(i).ToString("dd/MM/yyyy")
' series.Points.AddXY(i, Date.Now.AddDays(i).ToString("dd/MM/yyyy"))
sizeOfDayToDisplay += 1
Next
'most new code added is below here
Dim chartArea = CalorieChart.ChartAreas(Series.ChartArea)
chartArea.AxisX.Minimum = 0
chartArea.AxisX.Maximum = sizeOfDayToDisplay
chartArea.CursorX.AutoScroll = True
chartArea.AxisX.ScaleView.Zoomable = True
chartArea.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number
Dim position As Integer = 0
Dim size As Integer = blockSize
chartArea.AxisX.ScaleView.Zoom(position, size)
chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll
chartArea.AxisX.ScaleView.SmallScrollSize = blockSize
'HandleEmpty(empty)
'Cursor.Current = Cursors.Default
End Sub
Public Function getRandumNumber() As Int16
Return CInt(Math.Floor((3500 - 1000 + 1) * Rnd())) + 1000
End Function
Based on this: How to scroll MS Chart along x-axis in vb.net, you can use:
Chart1.Series("LoadCell").Points.AddY(receivedData)
Chart1.ResetAutoValues()
If Chart1.Series("LoadCell").Points.Count >= 100 Then
Chart1.Series("LoadCell").Points.RemoveAt(0)
End If
It Auto scales the y axis as well as limiting the x axis to 100 by
removing the first entry when the entries exeed 100.

ElseIf statements

I want to ask how to do this based on a task i was given, i tried but it doesn't really work can anyone tell me why? Or even better if you could show me the correct version
The Task is:
Adapt the programme so the user must enter a quality for each diamond - Each diamond has a quality that is a whole number between 1 (low quality ) and 5 (high quality).
Here is the code (Sorry if its bad i am new to VB)
Can anyone perhaps show me how to fix, i've been told the mistakes, but honestly i am not sure how to actually fix them. :(
Dim Weight As Integer
Dim userValue As Boolean = True
Dim diamonds As Integer = 0
Console.WriteLine("Enter the weight of your diamond in grams")
Weight = Console.ReadLine
Dim Quality As Integer
Dim DiamondPrice As Integer
Console.WriteLine("Enter the quality of your diamond from 1 (low quality) to 5 (high quality)")
Quality = Console.ReadLine
If Quality = "1" Then
DiamondPrice = "100"
ElseIf Quality = "2" Then
DiamondPrice = "150"
ElseIf Quality = "3" Then
DiamondPrice = "200"
ElseIf Quality = "4" Then
DiamondPrice = "250"
ElseIf Quality = "5" Then
DiamondPrice = "300"
End If
Console.ReadLine()
Console.WriteLine("This diamond costs : " & Quality * )
Console.WriteLine("Would you like to price another Y/N?")
Console.ReadLine()
While userValue
Console.WriteLine("Enter the weight of the new diamond in grams")
Weight = Console.ReadLine
Console.WriteLine("This diamond costs : " & Weight * 350)
Console.WriteLine("Would you like to price another Y/N?")
userValue = Console.ReadLine.ToUpper().Trim() = "Y"
diamonds += 1
End While
Console.WriteLine("The total number of diamonds entered is : {0}", diamonds)
Console.ReadLine()

Struggling to code in VB a simple population model

I am trying to develop a population model in Visual Basics 2010. However, I cant do the math nor the code!! Suppose over 5 generations population growth of a fly look like this
GENERATION JUVENILES ADULTS SENILES TOTAL
0 10 10 10 30
1 20 10 10 40
2 20 20 10 50
3 40 20 20 80
4 40 40 20 100
5 80 40 40 160
In the model, Juvelines have a survival rate of 1 i.e. all juveniles survive into Adulthood. Adults have a survival rate of 1 also i.e. all adults survive into Seniles. However, Seniles have a survival rate of 0 i.e. the current population all die. Once completed, I will be able to vary the survival rates for each type of insect, but for now 1,1,0 works fine
The second most important assumption here is that the birth rate of juveniles is double the current number of adults:
New number of juveniles = Current number of adults * birth rate
My question is how do a represent this model as visual basic code?
This is all I have so far
'Assign Values of Textbox to Public Variables
BirthRate = txtBirthRate.Text
GenerationNum = txtGenerations.Text
PopulationJuveniles = txtPJ.Text
PopulationAdults = txtPA.Text
PopulationSeniles = txtPS.Text
SurvivalJuveniles = txtSJ.Text
SurvivalAdults = txtSA.Text
SurvivalSeniles = txtSS.Text
For Counter As Integer = 1 To GenerationNum
'Calculate population of adults
NewJuveniles = PopulationAdults * BirthRate
NewAdults = PopulationJuveniles * SurvivalJuveniles 'the juveniles have matured
NewSeniles = PopulationAdults * SurvivalAdults 'the adults have aged
Next
The following method calculates the population for each generation. Note that it includes code to validate the numbers entered in the TextBoxes (otherwise a bad number would cause an exception). It also displays the numbers for each generation in a ListBox.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Birthrate As Double, GenerationNum As Integer
Dim PopulationJuveniles, PopulationAdults, PopulationSeniles As Double
Dim SurvivalJuveniles, SurvivalAdults, SurvivalSeniles As Double
Dim NewJuveniles, NewAdults, NewSeniles As Double
If Not Double.TryParse(txtBirthRate.Text, Birthrate) _
OrElse Integer.TryParse(txtGenerations.Text, GenerationNum) Then
MessageBox.Show("Enter valid numbers for birthrate and generations")
Exit Sub
End If
If Not Double.TryParse(txtPJ.Text, PopulationJuveniles) _
OrElse Double.TryParse(txtPA.Text, PopulationAdults) _
OrElse Double.TryParse(txtPS.Text, PopulationSeniles) Then
MessageBox.Show("Enter valid numbers for populations")
Exit Sub
End If
If Not Double.TryParse(txtSJ.Text, SurvivalJuveniles) _
OrElse Double.TryParse(txtSA.Text, SurvivalAdults) _
OrElse Double.TryParse(txtSS.Text, SurvivalSeniles) Then
MessageBox.Show("Enter valid numbers for survival")
Exit Sub
End If
ListBox1.Items.Clear()
For Counter As Integer = 1 To GenerationNum
NewJuveniles = PopulationAdults * Birthrate
NewAdults = PopulationJuveniles * SurvivalJuveniles 'the juveniles have matured
NewSeniles = PopulationAdults * SurvivalAdults 'Add + PopulationSeniles * SurvivalSeniles if SurvivalSeniles can ever be >0
PopulationSeniles = NewSeniles
PopulationAdults = NewAdults
PopulationJuveniles = NewJuveniles
ListBox1.Items.Add(PopulationJuveniles.ToString & ", " & PopulationAdults.ToString & ", " & PopulationSeniles.ToString)
Next
End Sub

Value of type 'Boolean' cannot be converted to 'Microsoft.Vbe.Interop.Forms.ReturnBoolean'

First off, let me provide you with my code:
Private Sub txtLatMin_Exit(ByVal Cancel As ReturnBoolean)
Dim i_SelStart As Long
Dim str_Prompt As String
'Save the cursor position.
i_SelStart = txtLatMin.SelectionStart
'Remove characters that we can't use.
txtLatMin.Text = NumericPortion(txtLatMin.Text, True)
'Enforce the maximums:
If Val(txtLatMin.Text) + Val(txtLatSec.Text) / 60 >= 60 Then
str_Prompt = "The maximum number of latitude minutes is 59.999."
MsgBox(str_Prompt, vbExclamation, "Notification")
Cancel = True
ElseIf Val(Me.textbox_Latitude_Degrees.Text) + Val(txtLatMin.Text) / 60 + Val(txtLatSec.Text) / 3600 > 89.99 Then
str_Prompt = "The maximum number of latitude degrees is 89.99."
MsgBox(str_Prompt, vbExclamation, "Notification")
Cancel = True
End If
'Restore the cursor position.
txtLatMin.SelectionStart = i_SelStart
End Sub
It is flagging the error in this post title at Cancel = True
Please keep in mind that I am converting code from VB6 to VB.NET
Would there be any suggestions that someone could provide me with?
I believe that you need to change
Cancel = True
to
Cancel.Value = True