Results 1 to 17 of 17

Thread: [RESOLVED] Trying to specify location results in String to Double error

  1. #1

    Thread Starter
    Junior Member ryanguy426's Avatar
    Join Date
    May 2013
    Posts
    22

    Resolved [RESOLVED] Trying to specify location results in String to Double error

    I'm trying to point my program to read a file from a specific directory. Everytime I try to fix this problem to stop the error, I keeps coming back, and I just can't figure out what's happening...

    Code:
    Try
                'Navigate to 
                fileReader = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments) + "\sandwich\" + sandwichtypes.SelectedIndex + "\desc.txt")
            Catch ex As Exception
                'Change text in "infoText" Textbox to show an error
                infoText.Text = "A desc.text file is not supplied, or an error has occured."
                'If error occurs, show in msgbox
                MsgBox(ex.ToString)
            End Try
        End Sub
    It will tell me "Option Strict On disallows implict conversions from 'String' to 'Double' "
    I've tried everything to my knowledge. Can any of you beautiful (lol) people help me?
    Staying up until 6:00am staring at a computer screen, typing code non-stop? Count me in!

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Trying to specify location results in String to Double error

    fileReader = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments) + "\sandwich\" + sandwichtypes.SelectedIndex + "\desc.txt")
    What are you trying to achieve with this?
    The ReadAllText method will return the contents of the file given by the file path you provide. However My.Computer.FileSystem.SpecialDirectories.MyDocuments will return a directory.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    Junior Member ryanguy426's Avatar
    Join Date
    May 2013
    Posts
    22

    Re: Trying to specify location results in String to Double error

    I want to read all the text from a text file in a specific directory, to a textbox.
    Staying up until 6:00am staring at a computer screen, typing code non-stop? Count me in!

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Trying to specify location results in String to Double error

    Start by creating a new variable; call it filePath and set it equal to the path of the file you want to read....

    Code:
    dim filePath as string = My.Computer.FileSystem.SpecialDirectories.MyDocuments + "\sandwich\" + sandwichtypes.SelectedIndex + "\desc.txt")
    Now set a break point on that line and run your code. When the debugger stops at the break point, hover your mouse over the variable. When you see it is not what you think it should be, change your until it is what it should be. Once it is, pass it to the ReadAllText method.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  5. #5
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Trying to specify location results in String to Double error

    Well did you add a watch to this line?

    Code:
    My.Computer.FileSystem.SpecialDirectories.MyDocuments) + "\sandwich\" + sandwichtypes.SelectedIndex + "\desc.txt"
    sandwichtypes.SelectedIndex is going to return a number, not a text. So if you are trying to get a combobox selection by name, you can use combobox.text instead. I think the issue is that you should not be using + to concatenate your strings, at the very least use "&". This is because if you use a number type (like selected index) it will literally try to add the number + the string. Also, even using "&" should be avoided where possible because there is a method to concatenate strings String.Concat():

    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx
    Last edited by jayinthe813; Oct 15th, 2014 at 07:44 PM. Reason: forgot how to english

  6. #6

    Thread Starter
    Junior Member ryanguy426's Avatar
    Join Date
    May 2013
    Posts
    22

    Re: Trying to specify location results in String to Double error

    @kebo

    When I try that code it says "End of statement expected". I tried modifying it myself once again, but I can't see what went wrong with it...
    Staying up until 6:00am staring at a computer screen, typing code non-stop? Count me in!

  7. #7
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Trying to specify location results in String to Double error

    Quote Originally Posted by ryanguy426 View Post
    When I try that code it says "End of statement expected". I tried modifying it myself once again, but I can't see what went wrong with it...
    We can't either.
    You need to post the code.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  8. #8

    Thread Starter
    Junior Member ryanguy426's Avatar
    Join Date
    May 2013
    Posts
    22

    Re: Trying to specify location results in String to Double error

    Quote Originally Posted by jayinthe813 View Post
    Well did you add a watch to this line?

    Code:
    My.Computer.FileSystem.SpecialDirectories.MyDocuments) + "\sandwich\" + sandwichtypes.SelectedIndex + "\desc.txt"
    sandwichtypes.SelectedIndex is going to return a number, not a text. So if you are trying to get a combobox selection by name, you can use combobox.text instead. I think the issue is that you should not be using + to concatenate your strings, at the very least use "&". This is because if you use a number type (like selected index) it will literally try to add the number + the string. Also, even using "&" should be avoided where possible because there is a method to concatenate strings String.Concat():

    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx
    This worked perfectly! I can't believe I didn't realize that I was using "SelectedIndex" instead of ".text"... wow. Thanks a bunch! It's working like a charm now!
    Staying up until 6:00am staring at a computer screen, typing code non-stop? Count me in!

  9. #9
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Trying to specify location results in String to Double error

    Quote Originally Posted by kebo View Post
    We can't either.
    You need to post the code.
    He already did, and I completely told him the issue, what his problem is, and what he should do about it:

    Code:
          Dim filePath As String = String.Concat(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "\sandwich\", sandwichtypes.Text, "\desc.txt")

  10. #10
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Trying to specify location results in String to Double error

    Quote Originally Posted by ryanguy426 View Post
    This worked perfectly! I can't believe I didn't realize that I was using "SelectedIndex" instead of ".text"... wow. Thanks a bunch! It's working like a charm now!
    I hope you also changed those "+" to "&" at least or else you'll be back soon :P

  11. #11
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Trying to specify location results in String to Double error

    He already did, and I completely told him the issue, what his problem is, and what he should do about it:
    the op had a bit more wrong with it that the combobox selection...

    fileReader = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments) + "\sandwich\"....
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  12. #12
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Trying to specify location results in String to Double error

    Quote Originally Posted by kebo View Post
    the op had a bit more wrong with it that the combobox selection...
    Ah, yes indeed. Good catch.

  13. #13
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Trying to specify location results in String to Double error

    I would probably use this also....

    Code:
    Dim filePath as string = Io.Path.Combine({My.Computer.FileSystem.SpecialDirectories.MyDocuments, "sandwich", sandwichtypesText, "desc.txt"})
    (freehand, beware of syntax)
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  14. #14
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Trying to specify location results in String to Double error

    Quote Originally Posted by kebo View Post
    I would probably use this also....

    Code:
    Dim filePath as string = Io.Path.Combine({My.Computer.FileSystem.SpecialDirectories.MyDocuments, "sandwich", sandwichtypesText, "desc.txt"})
    (freehand, beware of syntax)
    If I were him, I would use that instead as it is meant to handle file paths specifically. Looks like I have some coding changes to make in my project tomorrow

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: [RESOLVED] Trying to specify location results in String to Double error

    I'm curious as to why you are opposed to & in favor of the laborious String.Concat? This sounds like the difference between Integer.Parse and CInt(). They both do the same thing. The former is more in an object oriented style, while the latter comes from VB6. Even MS suggests using the latter, though.
    My usual boring signature: Nothing

  16. #16
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: [RESOLVED] Trying to specify location results in String to Double error

    Quote Originally Posted by Shaggy Hiker View Post
    I'm curious as to why you are opposed to & in favor of the laborious String.Concat? This sounds like the difference between Integer.Parse and CInt(). They both do the same thing. The former is more in an object oriented style, while the latter comes from VB6. Even MS suggests using the latter, though.
    I wouldn't use the term laborious, its about the same code (and utilizes similar amount of memory?). I remember reading something that said that "&" uses the regional settings when it converts (so you could get 96,95 instead of 96.95) and also disregards "option strict" since it always makes an implicit conversion to string (although that last part may be a moot point). Also, if he is going to use IO.Path.Combine, String.Concat isn't far off either. Maybe just my preference, but if he is going to concatenate with "&" he should always use it and not jump between IO.Path.Combine and then use "&" on his strings.

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: [RESOLVED] Trying to specify location results in String to Double error

    Fair enough.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width