[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?:p
Re: Trying to specify location results in String to Double error
Quote:
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.
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.
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.
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
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...
Re: Trying to specify location results in String to Double error
Quote:
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.
Re: Trying to specify location results in String to Double error
Quote:
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!
Re: Trying to specify location results in String to Double error
Quote:
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")
Re: Trying to specify location results in String to Double error
Quote:
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
Re: Trying to specify location results in String to Double error
Quote:
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...
Quote:
fileReader = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments) + "\sandwich\"....
Re: Trying to specify location results in String to Double error
Quote:
Originally Posted by
kebo
the op had a bit more wrong with it that the combobox selection...
Ah, yes indeed. Good catch.
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)
Re: Trying to specify location results in String to Double error
Quote:
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 ;)
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.
Re: [RESOLVED] Trying to specify location results in String to Double error
Quote:
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.
Re: [RESOLVED] Trying to specify location results in String to Double error