Reputation: 3
I got this message: Conversion from string to type Double is not valid and I can't figure out what to do. Here's the code.
Dim Amount As String = TextBoxAmount.Text
If Amount = "" Or Not IsNumeric(Amount) Then
MsgBox("Incorect Format!", InfoOKOnly, AppTitle)
TextBoxAmount.Focus()
Return '- Exit Sub
End If
Dim DblAmount As Double = CType(Amount, Double)
If (DblAmount < 500) Then
MsgBox("Amount must be greater than or equal to 500", InfoOKOnly, AppTitle)
TextBoxAmount.Focus()
Return
End If
TextBoxAmount.Text = Format(DblAmount, "#,##0. 00")
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
If (MsgBox("Do you want to continue?", vbQuestion + vbYesNo, AppTitle) = MsgBoxResult.Yes) Then
If TextBoxAccountNo.Text = "" Or String.IsNullOrEmpty(TextBoxAccountNo.Text) Or
TextBoxAccountName.Text = "" Or String.IsNullOrEmpty(TextBoxAccountName.Text) Or
TextBoxPhoneNo.Text = "" Or String.IsNullOrEmpty(TextBoxPhoneNo.Text) Or
TextBoxAmount.Text = "" Or String.IsNullOrEmpty(TextBoxAmount.Text) Then
'--^_^
Return
End If
Dim cf As MyClassFiles1 = New MyClassFiles1()
Dim accNo As String = TextBoxAccountNo.Text
Dim accName As String = TextBoxAccountName.Text
Dim PhoneNo As String = TextBoxPhoneNo.Text
Dim Cash As Double = CDbl(TextBoxAmount.Text)
And the problem is here, in this line of code (first above). When I enter numbers in textbox (amount) it says that cannot be converted to double.
Dim o As MyAccountClass1 = New MyAccountClass1(accNo, accName, PhoneNo, Cash)
FileOpen(1, cf.GetMasterFile, OpenMode.Append)
'-- C stand for Create New Account
WriteLine(1, o.AccountNo, o.AccountName, o.PhoneNo, o.Amount, "Active")
FileOpen(2, cf.GetTransactionFile, OpenMode.Append)
WriteLine(2, o.AccountNo, CreatedDate, CreatedTime, "C", o.Amount)
'--Close before reopening in another mode.
FileClose(1)
FileClose(2)
MsgBox("The files have been saved!", InfoOKOnly, AppTitle)
ClearTextBoxes()
End If
End Sub
End Class
Upvotes: 0
Views: 209
Reputation: 15091
I think your problem is with this line.
TextBoxAmount.Text = Format(DblAmount, "#,##0. 00")
The spaces between 0. and 00 will not be accepted by a Double.
You can combine your validation in the Save button. Don't insult the user by asking if they want to continue.
Use the System.IO methods avaiable from .net. File.AppendAllText opens, writes to, and closes the file saving you several lines of code.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim InputCash As Double
If Not Double.TryParse(TextBoxAmount.Text, InputCash) OrElse InputCash < 500 Then
MessageBox.Show("Please enter a valid amount greater than or equal to 500.")
TextBoxAmount.Focus()
Return '- Exit Sub
End If
If String.IsNullOrWhiteSpace(TextBoxAccountNo.Text) OrElse
String.IsNullOrWhiteSpace(TextBoxAccountName.Text) OrElse
String.IsNullOrEmpty(TextBoxPhoneNo.Text) Then
MessageBox.Show("Please fill in all boxes.")
Return
End If
Dim cf As MyClassFiles1 = New MyClassFiles1(TextBoxAccountNo.Text, TextBoxAccountName.Text, TextBoxPhoneNo.Text,InputCash)
Dim message = cf.SaveFiles()
MessageBox.Show(message)
End Sub
Public Class MyClassFiles1
Public Property AccountNo As String
Public Property AccountName As String
Public Property PhoneNo As String
Public Property Amount As Double
Public Sub New(number As String, name As String, pnone As String, cash As Double)
AccountNo = number
AccountName = name
PhoneNo = pnone
Amount = cash
End Sub
Private Function GetMasterFile() As String
Return "C:\SomeUser\MasterAccountFile.txt"
End Function
Private Function GetTransactionFile() As String
Return "C:\SomeUser\TransactionFile.txt"
End Function
Public Function SaveFiles() As String
Try
Dim strForMaster = $"{AccountNo},{AccountName},{PhoneNo},{Amount},Active"
File.AppendAllText(GetMasterFile(), strForMaster)
Dim strForTransaction = $"{AccountNo},{Now.Date},{Now.ToShortTimeString},C,{Amount}"
File.AppendAllText(GetTransactionFile(), strForTransaction)
Catch ex As Exception
Return ex.Message
End Try
Return "The files have been saved!"
End Function
End Class
Upvotes: 0
Reputation: 974
I would suggest using TryParse
...
Dim Cash As Double
If Not Double.TryParse(TextBoxAmount.Text, Cash) Then
'do sth, show some msgbox that value in the textBox is incorrect
End If
...
Also, your checks String.IsNullOrEmpty
should be replaced with String.IsNullOrWhiteSpace
because you check for Empty before it TextBoxAccountNo.Text = ""
And one more thing about the code. You want to use OrElse
instead of Or
Check this question, especially this answer.
Upvotes: 0