Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. data type error

data type error

Scheduled Pinned Locked Moved Visual Basic
helpquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GaryKoh
    wrote on last edited by
    #1

    hello the problem i'm having is that i cant get the value i wanted from a .txt file value in .txt file 123.445 value after call .readline 123445 the value in the .txt is a double value but when i call it to the application it become integer. did i type the wrong coding? the declaration and coding is as below Dim num1 As Double Dim sr As IO.StreamReader = IO.File.OpenText("info.txt") Do While (sr.Peek <> -1) num1 = CDbl(sr.ReadLine) Loop Text1.Text = num1 i even try almost everything i know but the output is still 123445 and not 123.445 please help me thank you Gary the amatuer

    D 1 Reply Last reply
    0
    • G GaryKoh

      hello the problem i'm having is that i cant get the value i wanted from a .txt file value in .txt file 123.445 value after call .readline 123445 the value in the .txt is a double value but when i call it to the application it become integer. did i type the wrong coding? the declaration and coding is as below Dim num1 As Double Dim sr As IO.StreamReader = IO.File.OpenText("info.txt") Do While (sr.Peek <> -1) num1 = CDbl(sr.ReadLine) Loop Text1.Text = num1 i even try almost everything i know but the output is still 123445 and not 123.445 please help me thank you Gary the amatuer

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You don't use CDbl for this. Use Double.Parse instead. CDbl is for converting between number types, not string to number:

      Imports System.IO
      .
      .
      .
      Dim num1 As Double
      Dim sr As StreamReader = File.OpenText("info.txt")
      Do While (sr <> -1)
      num1 = Double.Parse(sr.ReadLine())
      Loop
      Text1.Text = num1.ToString()

      RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      G 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You don't use CDbl for this. Use Double.Parse instead. CDbl is for converting between number types, not string to number:

        Imports System.IO
        .
        .
        .
        Dim num1 As Double
        Dim sr As StreamReader = File.OpenText("info.txt")
        Do While (sr <> -1)
        num1 = Double.Parse(sr.ReadLine())
        Loop
        Text1.Text = num1.ToString()

        RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        G Offline
        G Offline
        GaryKoh
        wrote on last edited by
        #3

        hello dave im sorry but the result is still the same im still getting 12345 instead of 123.45 i double check everything and the value in the notepad is 123.45 i try using label instead of textbox but the same thing is shown i try changing the value in notepad to 345,232.45 and this error comes out

        An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

        Additional information: Input string was not in a correct format.

        but if i change it to 123.455,67 it just show me 123455,67 is the problem coming from the notepad? i change most of the coding as you recommended

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim name, display As String
        Dim total, amount, charges As Double
        Dim num1 As Double
        Dim sr As StreamReader = File.OpenText("info.txt")

            Do While (sr.Peek <> -1)
                num1 = Double.Parse(sr.ReadLine())
            Loop
            Text1.Text = num1.ToString()
            Label2.Text = num1.ToString()
        

        i'm so confuse on this problem cause i try reading book and it say that this two method should provide me with the answer i was looking for but it seem like it is not please help me thank you and sorry for the trouble Gary the amatuer

        D 1 Reply Last reply
        0
        • G GaryKoh

          hello dave im sorry but the result is still the same im still getting 12345 instead of 123.45 i double check everything and the value in the notepad is 123.45 i try using label instead of textbox but the same thing is shown i try changing the value in notepad to 345,232.45 and this error comes out

          An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

          Additional information: Input string was not in a correct format.

          but if i change it to 123.455,67 it just show me 123455,67 is the problem coming from the notepad? i change most of the coding as you recommended

          Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          Dim name, display As String
          Dim total, amount, charges As Double
          Dim num1 As Double
          Dim sr As StreamReader = File.OpenText("info.txt")

              Do While (sr.Peek <> -1)
                  num1 = Double.Parse(sr.ReadLine())
              Loop
              Text1.Text = num1.ToString()
              Label2.Text = num1.ToString()
          

          i'm so confuse on this problem cause i try reading book and it say that this two method should provide me with the answer i was looking for but it seem like it is not please help me thank you and sorry for the trouble Gary the amatuer

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          This problem has nothing to do with Notepad what-so-ever. These are the possibilities: 1) This is not the code your running. 2) The code is not opening the file you think it is. 3) The info.txt file has more to it than you're telling me. When I tested this code, the only thing in my text file was a single line, "123.45", without the quotes. This code is GARANTEED to return the correct results:

              Dim num1 As Double
              Dim sr As StreamReader = File.OpenText("D:\\test.txt")
              num1 = Double.Parse(sr.ReadLine())
              TextBox1.Text = num1.ToString()
          

          They way your code is setup, your reading the ENTIRE file first, then returning the last value that was read. Your code can only work if the only thing on each line is a single decimal number without any punctuation except for one period. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          G 1 Reply Last reply
          0
          • D Dave Kreskowiak

            This problem has nothing to do with Notepad what-so-ever. These are the possibilities: 1) This is not the code your running. 2) The code is not opening the file you think it is. 3) The info.txt file has more to it than you're telling me. When I tested this code, the only thing in my text file was a single line, "123.45", without the quotes. This code is GARANTEED to return the correct results:

                Dim num1 As Double
                Dim sr As StreamReader = File.OpenText("D:\\test.txt")
                num1 = Double.Parse(sr.ReadLine())
                TextBox1.Text = num1.ToString()
            

            They way your code is setup, your reading the ENTIRE file first, then returning the last value that was read. Your code can only work if the only thing on each line is a single decimal number without any punctuation except for one period. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            G Offline
            G Offline
            GaryKoh
            wrote on last edited by
            #5

            thanks dave for your help i think i know what is causing all this problem that i have been having so far i took most of my application build on my PC to my friends PC to test and it all work prefectly as i expected.but when i try at my own PC anouynomous error pop out. i cant even do the coding below while my friends PC can

            formatnumber(1234,2)
            formatcurrency(1234,0)

            i think this happen cause some of the file needed is missing the solution that you provide me work well in my friends PC after i send my application to him. the "123.45" value is able to be shown and calculated as i expected you might think that im pulling your leg but it is really the truth and im sorry to cause you such trouble i have to delete my vb.net program and then reinstall it again and this time i hope no problem will occur to my future programs:-D again thanks for your help Gary

            D 1 Reply Last reply
            0
            • G GaryKoh

              thanks dave for your help i think i know what is causing all this problem that i have been having so far i took most of my application build on my PC to my friends PC to test and it all work prefectly as i expected.but when i try at my own PC anouynomous error pop out. i cant even do the coding below while my friends PC can

              formatnumber(1234,2)
              formatcurrency(1234,0)

              i think this happen cause some of the file needed is missing the solution that you provide me work well in my friends PC after i send my application to him. the "123.45" value is able to be shown and calculated as i expected you might think that im pulling your leg but it is really the truth and im sorry to cause you such trouble i have to delete my vb.net program and then reinstall it again and this time i hope no problem will occur to my future programs:-D again thanks for your help Gary

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              GaryKoh wrote: formatnumber(1234,2)formatcurrency(1234,0) ?????? What's with the comma's in the place where the decimal point is supposed to go? Is this the format of the numbers in the file? If so, the Parse method will not work unless you go into the Regional and Language Settings control panel and change the Country to the one where this data is coming from. For instance, in Germany, the number 1,000,000.00 (US-English) is represented by 1.000.000,00 The decimals and commas are interchanged. What I'm trying to get at is the Parse method is locale sensitive. It will parse numbers based on the settings found in the Regional and Language Settings control panel. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              G 1 Reply Last reply
              0
              • D Dave Kreskowiak

                GaryKoh wrote: formatnumber(1234,2)formatcurrency(1234,0) ?????? What's with the comma's in the place where the decimal point is supposed to go? Is this the format of the numbers in the file? If so, the Parse method will not work unless you go into the Regional and Language Settings control panel and change the Country to the one where this data is coming from. For instance, in Germany, the number 1,000,000.00 (US-English) is represented by 1.000.000,00 The decimals and commas are interchanged. What I'm trying to get at is the Parse method is locale sensitive. It will parse numbers based on the settings found in the Regional and Language Settings control panel. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                G Offline
                G Offline
                GaryKoh
                wrote on last edited by
                #7

                hahaha thanks dave the problem i have is just the Regional and Language Setting in my computer it is set to my country setting but now i have set it to US-English everything is working well and now i understand it perfectly the parse method is always base on each computer regional setting thanks you really help me thank you Gary

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups