-
Oct 15th, 2014, 07:02 PM
#1
Thread Starter
Junior Member
[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 !
-
Oct 15th, 2014, 07:19 PM
#2
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
-
Oct 15th, 2014, 07:23 PM
#3
Thread Starter
Junior Member
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 !
-
Oct 15th, 2014, 07:33 PM
#4
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
-
Oct 15th, 2014, 07:38 PM
#5
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
-
Oct 15th, 2014, 07:49 PM
#6
Thread Starter
Junior Member
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 !
-
Oct 15th, 2014, 07:50 PM
#7
Re: Trying to specify location results in String to Double error
Originally Posted by ryanguy426
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
-
Oct 15th, 2014, 07:52 PM
#8
Thread Starter
Junior Member
Re: Trying to specify location results in String to Double error
Originally Posted by jayinthe813
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 !
-
Oct 15th, 2014, 07:55 PM
#9
Re: Trying to specify location results in String to Double error
Originally Posted by kebo
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")
-
Oct 15th, 2014, 07:56 PM
#10
Re: Trying to specify location results in String to Double error
Originally Posted by ryanguy426
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
-
Oct 15th, 2014, 08:04 PM
#11
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
-
Oct 15th, 2014, 08:09 PM
#12
Re: Trying to specify location results in String to Double error
Originally Posted by kebo
the op had a bit more wrong with it that the combobox selection...
Ah, yes indeed. Good catch.
-
Oct 15th, 2014, 08:14 PM
#13
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
-
Oct 15th, 2014, 08:29 PM
#14
Re: Trying to specify location results in String to Double error
Originally Posted by kebo
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
-
Oct 16th, 2014, 11:14 AM
#15
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
-
Oct 16th, 2014, 04:51 PM
#16
Re: [RESOLVED] Trying to specify location results in String to Double error
Originally Posted by Shaggy Hiker
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.
-
Oct 16th, 2014, 04:54 PM
#17
Re: [RESOLVED] Trying to specify location results in String to Double error
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|