I'm working on a school project using VIsual Studio 2015, the assignment is to create GUI "Commute Calculator" with three options for mode of transportation. I've wrote the code following the instructions in my textbook but am getting the following error:
"BC30057 Too many arguments to 'Private Function CarFindCost(intCommuteChoice As Integer, intDays As Integer) As Decimal'."
I'm a newbie to vs, but based on the error I believe the problem is with how I declared variables. I googled how to convert an integer to decimal, but haven't found anything that worked. The code is lengthy, but I included it all as an FYI. The error is in the private sub btnCommute and appears to be tied to three private functions: CarFindCost, BusFindCost and TrainFindCost. How to I fix it so I don't get errors on the variable intLength in the private sub bthCommute?
Option Strict On
Public Class frmCommuteCalc
Dim intCommuteChoice As Integer
Dim strSelectedMode As String = ""
Private _strGas As Integer
Private _strMiles As String = "Enter the total miles for a round trip: "
Private _strMilesPerGallon As Double = 2.15
Private _strDailyParking As Decimal = 10
Private _strMonthlyParking As Decimal
Private _strMonthlyUpkeep As Decimal = 112
Private _strRTBusFare As String = "Round trip bus fare is "
Private _strRTTrainFare As String = "Round trip train fare is "
Private _StrDays As String = "Enter the number of days worked per month: "
Private _intTrainFare As Integer
Private Sub frmCommuteCalc_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Threading.Thread.Sleep(5000)
End Sub
Private Sub cboCommuteMethod_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboCommuteMethod.SelectedIndexChanged
Dim intCommuteChoice As Integer
intCommuteChoice = cboCommuteMethod.SelectedIndex()
lstCommute.Items.Clear()
Select Case intCommuteChoice
Case 0
Car()
Case 1
Train()
Case 2
Bus()
End Select
lblDays.Visible = True
lblMiles.Visible = True
lblLength.Visible = True
txtDays.Visible = True
'txtMonthlyTotal.Visible = True
End Sub
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
Dim intCommuteChoice As Integer
Dim intDaysPerMonth As Integer
Dim decTotalCost As Decimal
Dim intLength As Integer = 0
Dim strSelectedMode As String = ""
Dim blnNumberInDaysIsValid As Boolean = False
Dim blnCommuteMethodIsSelected As Boolean = False
blnNumberInDaysIsValid = ValidateNumberInDays()
intCommuteChoice = ValidateCommuteSelection(blnCommuteMethodIsSelected, strSelectedMode)
If (blnNumberInDaysIsValid And blnCommuteMethodIsSelected) Then
intDaysPerMonth = Convert.ToInt32(txtDays.Text)
intCommuteChoice = cboCommuteMethod.SelectedIndex()
Select Case intCommuteChoice
Case 0
decTotalCost = CarFindCost(intCommuteChoice, intDaysPerMonth, intLength)
Case 1
decTotalCost = BusFindCost(intCommuteChoice, intDaysPerMonth, intLength)
Case 2
decTotalCost = TrainFindCost(intCommuteChoice, intDaysPerMonth, intLength)
End Select
End If
End Sub
Private Function intLength() As Object
Throw New NotImplementedException()
End Function
Function ComputeCommuteCost(ByVal decMiles As Decimal, ByVal decGallons As Decimal) As Decimal
Dim decMilage As Decimal
decMilage = decMiles / decGallons
Return decMilage
End Function
Private Sub Car()
lstCommute.Items.Add(_strMiles)
lstCommute.Items.Add(_strMilesPerGallon)
lstCommute.Items.Add(_StrDays)
lstCommute.Items.Add(_strMonthlyParking)
lstCommute.Items.Add(_strMonthlyUpkeep)
End Sub
Private Sub Bus()
lstCommute.Items.Add(_strRTBusFare)
lstCommute.Items.Add(_StrDays)
End Sub
Private Sub Train()
lstCommute.Items.Add(_StrDays)
lstCommute.Items.Add(_strRTTrainFare)
End Sub
Private Function ValidateNumberInDays() As Boolean
Dim intDays As Integer
Dim blnValidityCheck As Boolean = False
Dim strNumberInDaysMessage As String = "Please enter the No. of days per month you will be commuting "
Dim strMessageBoxTitle As String = "Error"
Try
intDays = Convert.ToInt32(txtDays.Text)
If intDays >= 1 And intDays <= 21 Then
blnValidityCheck = True
Else
MsgBox(strNumberInDaysMessage, , strMessageBoxTitle)
txtDays.Focus()
txtDays.Clear()
End If
Catch Exception As FormatException
MsgBox(strNumberInDaysMessage, , strMessageBoxTitle)
txtDays.Focus()
txtDays.Clear()
Catch Exception As OverflowException
MsgBox(strNumberInDaysMessage, , strMessageBoxTitle)
txtDays.Focus()
txtDays.Clear()
Catch Exception As SystemException
MsgBox(strNumberInDaysMessage, , strMessageBoxTitle)
txtDays.Focus()
txtDays.Clear()
End Try
Return blnValidityCheck
End Function
Private Function ValidateCommuteSelection(ByRef blnDays As Boolean, ByRef strDays As String) As Integer
Dim intCommuteChoice As Integer
Try
intCommuteChoice = Convert.ToInt32(lstCommute.SelectedIndex)
strDays = lstCommute.SelectedItem.ToString()
blnDays = True
Catch Exception As SystemException
MsgBox("Select a commute mode", , "Error")
blnDays = False
End Try
Return intCommuteChoice
End Function
Private Function CarFindCost(ByVal intCommuteChoice As Integer, ByVal intDays As Integer) As Decimal
Dim decDaysPerMonth As Decimal
Dim decMiles As Decimal
Dim decMilesPerGallon As Decimal = 2
Dim decGasTotal As Decimal
Dim decDailyParking As Decimal = 10
Dim decMonthlyParking As Decimal
Dim decMonthlyUpkeep As Decimal = 112
Dim decFinalCost As Decimal
Dim intLength As Integer = 0
decMiles = Convert.ToDecimal(txtMiles.Text)
decMilesPerGallon = Convert.ToDecimal(txtGallons.Text)
decGasTotal = decMilesPerGallon * decMiles
decMonthlyParking = Convert.ToDecimal(lblLength.Text)
decMonthlyParking = decDailyParking * decDaysPerMonth
decFinalCost = Convert.ToDecimal(lblLength.Text)
decFinalCost = decGasTotal + decMonthlyUpkeep + decMonthlyParking
Return decFinalCost
End Function
Private Function BusFindCost(ByVal intCommuteChoice As Integer, ByVal intDays As Integer) As Decimal
Dim intLength As Integer = 0
Dim decDaysPerMonth As Decimal
Dim decBusFarePerDay As Decimal = 4
Dim decFinalCost As Decimal
decBusFarePerDay = Convert.ToDecimal(txtMonthlyTotal)
decFinalCost = decBusFarePerDay * decDaysPerMonth
Return decFinalCost
End Function
Private Function TrainFindCost(ByVal intCommuteChoice As Integer, ByVal intDays As Integer) As Decimal
Dim intLength As Integer = 0
Dim decDaysPerMonth As Decimal
Dim decTrainFarePerDay As Decimal = 18
Dim decFinalCost As Decimal
decTrainFarePerDay = Convert.ToDecimal(txtMonthlyTotal)
decFinalCost = Convert.ToDecimal(txtMonthlyTotal)
decFinalCost = decDaysPerMonth * decTrainFarePerDay
Return decFinalCost
End Function
End Class
In:
Select Case intCommuteChoice ... End Select
in each case; call function CarFindCost with 3 parameters y you have declared with only 2.
Related
I am making a Math quiz game and I wondering why the question does not change to a NEW ONE when I get the right answer and HOW to make it to 10 and stop running then jump out a message box to ask the user want to PLAY AGAIN or not?
Public Class Multiplication
Dim TotalQuestion As Integer
Dim CorrectAnswer As Integer
Dim WrongAnswer As Integer
Dim R As New Random
Dim numOne As Integer = R.Next(0, 10)
Dim numTwo As Integer = R.Next(1, 10)
Dim Ans As Integer = 0
Dim Tries As Integer = 0
Private Sub Multiplication_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Generate()
End Sub
Sub Generate()
TotalQuestion = TotalQuestion + 1
Dim Show As String
Show = numOne & " x " & numTwo & " = "
lblQuestionMUL.Text = Show
End Sub
Private Function Multiply(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Return num1 * num2
Generate()
End Function
Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
Integer.TryParse(lblQuestionMUL.Text, numOne & numTwo)
Ans = Multiply(numOne, numTwo)
If Val(txtAnswer.Text) = Ans Then
CorrectAnswer = CorrectAnswer + 1
Else
WrongAnswer = WrongAnswer + 1
End If
lblCorrectAns.Text = CorrectAnswer
lblWrongAns.Text = WrongAnswer
txtAnswer.Clear()
txtAnswer.Focus()
Generate()
End Sub
End Class
In addition to previous comments you should set the numOne and numTwo variables to random numbers in Generate Sub (Keeping the declaration as Global Variables). Your code just set them in the beginning to random numbers only once. Check the code below:
Public Class Multiplication
Dim TotalQuestion As Integer = 0
Dim CorrectAnswer As Integer
Dim WrongAnswer As Integer
Dim R As New Random
Dim NumOne As Integer
Dim NumTwo As Integer
Dim QuizCompleted As Boolean = False
Dim Ans As Integer = 0
Dim Tries As Integer = 0
Private Sub Multiplication_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Generate()
End Sub
Sub Generate()
If TotalQuestion < 10 Then
NumOne = R.Next(0, 10)
NumTwo = R.Next(1, 10)
TotalQuestion = TotalQuestion + 1
Dim Show As String
Show = NumOne & " x " & NumTwo & " = "
lblQuestionMUL.Text = Show
Else
QuizCompleted = True
End If
End Sub
Private Function Multiply(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Generate()
Return num1 * num2
End Function
Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
Integer.TryParse(lblQuestionMUL.Text, NumOne & NumTwo)
Ans = Multiply(NumOne, NumTwo)
If Val(txtAnswer.Text) = Ans Then
CorrectAnswer = CorrectAnswer + 1
Else
WrongAnswer = WrongAnswer + 1
End If
lblCorrectAns.Text = CorrectAnswer
lblWrongAns.Text = WrongAnswer
txtAnswer.Clear()
txtAnswer.Focus()
If QuizCompleted Then
MsgBox("Quiz Completed")
End If
End Sub
End Class
the following code is getting the above error and i cant see why. i know the mod operator needs numbers in front and behind it so chrArray(i) is not getting a number. any help appreciated.
Public Class Form1
Private Sub Button3_Click(sender As Object, e As EventArgs)
Application.Exit()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs)
Textbox1.Text = ""
Textbox2.Text = ""
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs)
Dim sNumber As String
sNumber = TextBox1.Text
Textbox2.Text = EvenDigitMessage(sNumber)
End Sub
Private Function IsOnlyNumber(ByVal qty As String) As Boolean
Dim objRegExp As New System.Text.RegularExpressions.Regex("^\d+$")
Return objRegExp.Match(qty).Success
End Function
Private Function IsThreeDigitNumber(ByVal num As String) As Boolean
Dim objRegExp As New System.Text.RegularExpressions.Regex("^\d{3}$")
Return objRegExp.Match(num).Success
End Function
Private Function EvenDigitMessage(ByVal sNumber As String) As String
Dim bValidDigits As Boolean
Dim bValidNumber As Boolean
Dim sMessage As String
Dim iTotalEven As Integer = 0
If Not IsNumeric(sNumber) Then
sMessage = "Please enter a 3 digit number"
Else
bValidNumber = IsOnlyNumber(sNumber)
bValidDigits = IsThreeDigitNumber(sNumber)
If bValidNumber = False Or bValidDigits = False Then
sMessage = "Please enter a 3 digit number"
Else
iTotalEven = CountEvenDigits(sNumber)
sMessage = "This number contains" & iTotalEven & "even digits."
End If
End If
Return sMessage
End Function
Private Function CountEvenDigits(ByVal sNumber As String) As Integer
Dim i As Integer
Dim chrArray() As Char
Dim iTotal As Integer = 0
chrArray = sNumber.ToCharArray
For i = 0 To chrArray.Length - 1
If chrArray(i) Mod 2 = 0 Then
If iTotal = 0 Then
iTotal = 1
Else
iTotal = iTotal + 1
End If
End If
Next
Return iTotal
End Function
End Class
Base on this linke
https://msdn.microsoft.com/en-us/library/7sx7t66b.aspx
Visual Basic does not convert directly between Char and the numeric types. You can use the Asc or AscW function to convert a Char value to an Integer that represents its code point. You can use the Chr or ChrW function to convert an Integer value to a Char that has that code point.
If the type checking switch (Option Strict Statement) is on, you must append the literal type character to a single-character string literal to identify it as the Char data type. The following example illustrates this.
so you have to change your code to somthing like this
If Asc(chrArray(i)) Mod 2 = 0 Then
If iTotal = 0 Then
iTotal = 1
Else
iTotal = iTotal + 1
End If
End If
Im trying to make a program that when i put a value less than 0, in a label something like "
negative numbers cant be used i have this
Public Class Form1 'Sebastian roman. Perimeter, 10/1/2014
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Try
Dim intSide1 As Integer = txtSide1.Text
Dim intSide2 As Integer = txtSide2.Text
Dim intSide3 As Integer = txtSide3.Text
Dim intSide4 As Integer = txtSide4.Text
Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
lblMessage.Text = intTotal.ToString("#,###.##")
Catch ex As Exception
MessageBox.Show("Incorrect Input. Enter a numeric value.")
End Try
End Sub
End Class
Yes i have to use the try catch method and i need help for this
This sounds an awful lot like homework, but you really need to convert your integers properly and then perform actual comparisons.
Public Class Form1 'Sebastian roman. Perimeter, 10/1/2014
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Try
' Relies on the GetIntegerInput method to throw exceptions
' for invalid entries
Dim intSide1 As Integer = GetIntegerInput(txtSide1.Text)
Dim intSide2 As Integer = GetIntegerInput(txtSide2.Text)
Dim intSide3 As Integer = GetIntegerInput(txtSide3.Text)
Dim intSide4 As Integer = GetIntegerInput(txtSide4.Text)
Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
lblMessage.Text = intTotal.ToString("#,###.##")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Function GetIntegerInput(ByVal input as String) As Integer)
Dim returnValue as Integer
' Will attempt a proper try parse. AndAlso will short circuit
' the comparison so a failure in TryParse will not perform the
' the second evaluation. In either case, an actual exception is
' thrown with your invalid numeric message
If (Not Int32.TryParse(input, returnValue) AndAlso returnValue < 0) Then
Throw New ArgumentException("Incorrect input. Enter a proper numeric value.")
End If
Return returnValue
End Function
End Class
Well a integer can be negative...
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Try
Dim intSide1 As Integer = txtSide1.Text
Dim intSide2 As Integer = txtSide2.Text
Dim intSide3 As Integer = txtSide3.Text
Dim intSide4 As Integer = txtSide4.Text
Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
lblMessage.Text = intTotal.ToString("#,###.##")
//New code
if intSide1 < 0 or intSide2 < 0 or intSide3 < 0 intSide4 < 0 Then
MessageBox.Show("Incorrect Input. Negative number not valid")
end if
Catch ex As Exception
MessageBox.Show("Incorrect Input. Enter a numeric value.")
End Try
End Sub
Ever since I added the CheckForCollision_Enemy method, I've been getting a stack overflow exception every time I run my code.
Here is the CheckForCollision_Enemy method, in the Main class.
Public Sub CheckForCollision_Enemy()
Dim ship1 As New Enemy(Nothing, Nothing)
Dim ship2 As New Enemy(Nothing, Nothing)
Debug.Print("")
Dim ships = {acc_e, bs_e, sb_e, ds_e, pb_e}
For i As Integer = 0 To ships.Length - 1
ship1 = ships(i)
For j As Integer = 0 To ships.Length - 1
ship2 = ships(j)
If ship1.name <> ship2.name Then
For l As Integer = 0 To ship1.length - 1
For t As Integer = 0 To ship2.length - 1
If ship1.space_filled(l, 0) = ship2.space_filled(t, 0) And ship1.space_filled(l, 1) = ship2.space_filled(t, 1) Then
Debug.Print("Collision at {" & ship1.space_filled(l, 0) & ", " & ship1.space_filled(l, 1) & "} " & ship1.name & " VS " & ship2.name)
End If
Next
Next
End If
Next
Next
End Sub
Here is the Enemy class. This is the class where the error is shown. I marked exactly where as a comment.
Shared gen As New Random()
Dim x As Integer = 0
Dim y As Integer = 0
Public Sub New(ByVal namep As String, ByVal lengthp As Integer)
name = namep
length = lengthp
ReDim _start_point(2)
ReDim _space_filled(length, 2)
GenerateDirection()
If direction = "horizontal" Then
x = gen.Next(0, 11 - length)
y = gen.Next(0, 10)
ElseIf direction = "vertical" Then
x = gen.Next(0, 10)
y = gen.Next(0, 11 - length)
End If
GenerateStartPoint()
ExtendStartPoint()
DefineFilled()
ColorFilled()
Main.CheckForCollision_Enemy() 'If this is taken out, it will work fine.
End Sub
Public Sub GenerateStartPoint()
start_point = {x, y}
End Sub
Public Sub GenerateDirection()
If gen.Next(0, 2) = 0 Then
direction = "horizontal"
Else
direction = "vertical"
End If
End Sub
Public Sub ExtendStartPoint()
If direction = "horizontal" Then
For i As Integer = 0 To length - 1
space_filled(i, 0) = start_point(0) + i
space_filled(i, 1) = start_point(1)
Next
ElseIf direction = "vertical" Then
For i As Integer = 0 To length - 1
space_filled(i, 0) = start_point(0)
space_filled(i, 1) = start_point(1) + i
Next
End If
End Sub
Public Sub DefineFilled()
For i As Integer = 0 To length - 1
x = space_filled(i, 0)
y = space_filled(i, 1)
Try
generate = False
Main.TrackerBoard.box_list(x, y).full = True
Catch
End Try
Next
End Sub
Private Sub ColorFilled()
For y As Integer = 0 To 9
For x As Integer = 0 To 9
'Debug.Print(Main.PlayerBoard.box_list(x, y).full)
If Main.TrackerBoard.box_list(x, y).full = True Then 'New error: "InvalidOperationException"
Main.TrackerBoard.box_list(x, y).image.BackColor = System.Drawing.Color.Red
Else
Main.TrackerBoard.box_list(x, y).image.BackColor = System.Drawing.Color.Silver 'Most often, the error appears here.
End If
Next
Next
End Sub
Here is the ship class. I've taken out most of the methods to save space; if you want to see anything, I'll add it in for you.
Public Class Ship
Dim _name As String
Public Property name() As String ...
Dim WithEvents _image As PictureBox
Public Property image() As PictureBox ...
Dim _length As Integer
Public Property length() As Integer ...
Dim _direction As String
Public Property direction() As String ...
Dim _selected As Boolean
Public Property selected() As Boolean ...
Dim _placed As Boolean
Public Property placed() As Boolean ...
Dim _location() As Integer = {0, 0}
Public Property location() As Integer() ...
Dim _has_moved As Boolean
Public Property has_moved() As Boolean ...
Dim _space_filled(,) As Integer
Public Property space_filled() As Integer(,) ...
Public rect As System.Drawing.Rectangle
Dim mouse_up As Boolean = False
Dim tile_size As Integer = 25
Public Sub New(ByVal namep As String, ByVal imagep As PictureBox, ByVal lengthp As Integer, ByVal directionp As String, ByVal selectedp As Boolean, ByVal placedp As Boolean)
name = namep
image = imagep
length = lengthp
direction = directionp
selected = selectedp
placed = placedp
location(0) = 0
location(1) = 0
ReDim space_filled(length, 2)
rect = New System.Drawing.Rectangle(location(0), location(1), length * tile_size, 1 * tile_size)
End Sub
Private Sub Ship_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _image.MouseMove ...
Private Sub Ships_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _image.MouseClick ...
Private Sub Ship_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _image.MouseUp ...
Public Sub Update() ...
Public Sub SnapToBox() ...
Private Sub DefineSpaceFilled() ...
Private Sub ColorFilled() ...
End Class
Your CheckForCollision_Enemy calls New Enemy, and New Enemy calls CheckForCollision_Enemy. You are recursing until the stack overflows.
I'm supposed to make a score calculator for a Programming assignment, for a football game.
it has 4 textboxes and a button, the function NEEDS to be there for full credit, I'm just not sure what I'm doing wrong.
Public Class Form1
Dim intTotal = 0
Dim intFirst = 0
Dim intSecond = 0
Dim intThird = 0
Dim intFourth = 0
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Try
Dim intFirst As Integer = Convert.ToInt32(txtFirst.Text)
Dim intSecond As Integer = Convert.ToInt32(txtSecond.Text)
Dim intThird As Integer = Convert.ToInt32(txtThird.Text)
Dim intFourth As Integer = Convert.ToInt32(txtFourth.Text)
Catch ex As Exception
MessageBox.Show("Enter in Digits!")
End Try
intTotal = calcTotal(intFirst, intSecond, intThird, intFourth, intTotal)
Me.lblTotal.Text = intTotal 'Shows as 0 at run-time
End Sub
Function calcTotal(ByVal intFirst As Integer, ByVal intSecond As Integer, ByVal intThird As Integer, ByVal intFourth As Integer, ByVal intTotal As Integer) As Integer
intTotal = intFirst + intSecond + intThird + intFourth
Return intTotal
End Function
End Class
lblTotal ends up displaying 0.
Your variables are declared at the block-level inside the try catch. Move the declarations out of the block and remove the class-level declarations (since they're not necesarry).
Like this:
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim intFirst As Integer = 0
Dim intSecond As Integer = 0
Dim intThird As Integer = 0
Dim intFourth As Integer = 0
Try
intFirst = Convert.ToInt32(txtFirst.Text)
intSecond = Convert.ToInt32(txtSecond.Text)
intThird = Convert.ToInt32(txtThird.Text)
intFourth = Convert.ToInt32(txtFourth.Text)
Catch ex As Exception
MessageBox.Show("Enter in Digits!")
End Try
Dim intTotal as Integer = calcTotal(intFirst, intSecond, intThird, intFourth, intTotal)
Me.lblTotal.Text = intTotal 'Shows as 0 at run-time
End Sub
Function calcTotal(ByVal intFirst As Integer, ByVal intSecond As Integer, ByVal intThird As Integer, ByVal intFourth As Integer, ByVal intTotal As Integer) As Integer
Return intFirst + intSecond + intThird + intFourth
End Function
End Class
I am not sure what your problem is but your variable declaration are outside of your function . You should initialize them inside.