The conversion of the string "" to type 'Double' is not valid

Solved
kikou93 Posted messages 438 Status Member -  
kikou93 Posted messages 438 Status Member -
Hello,
it's been a while since I asked questions (I was very busy with my studies)
I’m using Visual Basic Express 2010
let me explain my problem and I hope someone can help me
I have, for example, the following condition:
If TextBox1.Text > 0 Then
.....
End if
but it displays the following error:
The conversion of the string "" to type 'Double' is not valid
I tried with:
If CInt(TextBox.Text) > 0
If Val(TextBox.Text) > 0
it works, no error is displayed
but the problem is that if textBox1.text = 0.009 the value that is taken into account is 0 and not 0.009
normally the condition is checked
I also tried with:
If CDbl(TextBox.Text) > 0
but the same error is displayed (The conversion of the string "" to type 'Double' is not valid)
please help me and thank you in advance for your help
--
How it works is cool!
      1000 Thanks friends

3 answers

  1. Anonymous user
     
    Good evening,

    If your pic is set up to use the point as the decimal symbol, then 0,009 is not a number, 0.009 is.

    Instead of Csomething, use the Convert class; you'll be able to choose which number format to use.

    --
    When I was little, the Dead Sea was just sick.
    George Burns
    1
    1. Anonymous user
       
      Dim toto As Double = Convert.ToDouble("0.009", CultureInfo.GetCultureInfo("en-US"))
      0
    2. kikou93 Posted messages 438 Status Member 1
       
      Thank you for replying so quickly.
      I configured the textBox as follows:
      Dim desNuméro() As Char = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
      Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
      If Not desNuméro.Contains(e.KeyChar) And Not Asc(e.KeyChar) = 8 Then
      e.Handled = True
      End If
      If e.KeyChar = "."c Or e.KeyChar = ","c Then
      e.Handled = True
      If Not (TextBox1.Text.Contains(",")) Then TextBox1.AppendText(",")
      End If
      End Sub
      so that the textBox only accepts numbers (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) and the comma
      so the decimal symbol is the comma and not the point
      even if I press the point key, in the textBox it is the comma that appears.
      0
    3. kikou93 Posted messages 438 Status Member 1
       
      Dim toto As Double = Convert.ToDouble("0.009", CultureInfo.GetCultureInfo("en-US"))

      I don't know if this method works, but it doesn't make my job easier because the condition I have involves a lot of textBox:
      If textBox1.text >0 and textBox2.text>0 and textBox3.text>0 .... and textBox30.text>0 then
      .........
      End if
      0
    4. NHenry Posted messages 15235 Registration date   Status Moderator Last intervention   387
       
      Enable Option Strict and Option Explicit in your VB.NET configuration (or in your project options) and correct the conversion errors that will appear.
      Also, remove the automatic import of the namespace "Microsoft.VisualBasic".
      0
    5. kikou93 Posted messages 438 Status Member 1
       
      if I add Active Option Strict and Option Explicit, it creates hundreds of errors,
      I decided to apply Whismeril's method
      thank you for taking the time for me, thank you very much
      0
  2. Anonymous user
     
    And in addition to NHenry's advice (hello), do not use accents or any letter modifiers in variable or function names.
    If you have to open your project on an English computer, it will bug everywhere.

    I don't know if this method works but it doesn't make my job easier because the condition I have contains a lot of textBoxes


    I showed you the way, not the solution perfectly tailored to your case.

    You could write a function that you would call for each TextBox, you could also create an extension method.

    But if you apply exactly this code, "0.009" would not be accepted
    There would be this
     Dim texte as String = "0,009" Dim toto As Double = Convert.ToDouble(texte.Replace(",","."), CultureInfo.GetCultureInfo("fr-FR")) 

    Which would allow accepting "0,009" or "0.009", but a large number with American thousand separators "1,000,000.001" would not work.

    At some point, you will have to lock the input format using validation for example
    --
    When I was little, the Dead Sea was just sick.
    George Burns
    1
    1. kikou93 Posted messages 438 Status Member 1
       
      Thank you Whismeril, I used your method and applied it to each TextBox
      even though it's long, the main thing is that it works very well and there are no issues
      Thanks for your help, you really helped me
      0
  3. NHenry Posted messages 15235 Registration date   Status Moderator Last intervention   387
     
    Well, does "0.009" work or not?

    With .NET, there is double.Parse which allows for accepting different cultures than the system's for decimals.

    --
    I mainly work in VB6 and VB.NET, with a bit of C#, but moderation often brings me to other languages.
    In VB.NET, remember to enable "Option Explicit" and "Option Strict."
    0
    1. kikou93 Posted messages 438 Status Member 1
       
      Thank you for getting back to me so quickly
      In truth, the value is 0.975, normally that should work, but that is not the case.
      0