In Visual Basic/VB.net, I am trying to convert strings to doubles. I am getting
ID: 3706443 • Letter: I
Question
In Visual Basic/VB.net, I am trying to convert strings to doubles. I am getting data and converting it. However, sometimes the data comes in as dashes "-----" which is expected for some cases. The problem is that the dashes can't be converted to doubles. My error message is: System.InvalidCastException: 'Conversion from string "------" to type 'Double' is not valid.' How can I convert the dashes to double? Or, can I make the code ignore the dashes? This is a portion of my code.
If (newCommand.Substring(0, 1) = "x") Then
CrestFtValue_Label.Text = " "
value = CDbl(newCommand.Substring(1, (newCommand.Length - 1)))
CrestFtValue_Label.Text = newCommand.Substring(1, (newCommand.Length - 1))
End If
Explanation / Answer
Hi .... please try to follow below:
consider mystring to be your input which you want to convert to double
EXPLANATION:
Please note that
1. We have a variable isitadash = False that means checking for a dash in the input
2. Now we loop through the entire string and check for the dashes if dash is found, update the variable isitadash to True
3. Outside the loop check for isitadash; If isitadash = True , you can ignore it otherwise convert the input to a double.
I hope you understood the logic behind this. If you have more queries. Please comment. I woiuld be happy to help.
All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.