Problems with file I/O

kimberley35640

New member
Joined
Jun 20, 2005
Messages
3
Programming Experience
Beginner
Problems with file I/O "Resolved"

I'm having some problems with a program I'm writing. I need to put in code to ensure that when I open a file dialog to select an input file that there is a guard against the user just hitting cancel instead of selecting a file. Then, in the save file dialog, I need to make sure that the user puts in a new file name to save the information to instead of the original file. I thought I'd figured it out but it still doesn't work. I'll post the code below.
I'm new to VB and programming so there may be some obvious solution to my problem that I'm just not seeing. I did a search and have been reading posts in forums and searching internet sites but it could be that I didn't recognise the answer when I saw it.
whoops.gif


Here goes....
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim InFileName As String
Dim OutFileName As String

'prompt for the existing input file path
InFileName = GetTheFileName(cdlOpenFile, "Where is the Phone File?", FileFilter)
'prompt for the output file path
OutFileName = GetTheFileName(cdlSaveFile, "Specify Filename to Save", FileFilter)
'guard against user pressing cancel
If InFileName = DialogResult.OK Then
'open phone file to read
ContactReader = New System.IO.StreamReader(InFileName)
End If
'make sure save file is saved under differen name than input file
If OutFileName = InFileName Then
MsgBox("Save file must have a different name than the original file.")
Else
'open output file to write
ContactWriter = New System.IO.StreamWriter(OutFileName)
End If


I just don't know what I'm doing wrong. I'm getting an error each time I test the program by pressing cancel or putting in the same save name as the input name.
This is the error...
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from string "" to type 'Double' is not valid.

Please help me if you can. I'm sooo frustrated and I feel like I have no clue what I'm doing.
Thanks in advance. Kimberley
 
Last edited:
if you could post the exact line the debugger hits the error on that would be useful because this code you posted doesnt show anything about converting an empty string to double

my guess is that somewhere you're reading a blank line and trying to convert it to a double but that section isnt posted
 
ok that's because: Dim InFileName As String you should have another variable declared as DialogResult

VB.NET:
 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
 Dim InFileName As String
 Dim OutFileName As String
Dim dlgResult As DialogResult
 
 'prompt for the existing input file path
 InFileName = GetTheFileName(cdlOpenFile, "Where is the Phone File?", FileFilter)
 'prompt for the output file path 
 OutFileName = GetTheFileName(cdlSaveFile, "Specify Filename to Save", FileFilter)
 'guard against user pressing cancel
dlgResult = cdlOpenFile.ShowDialog
 [color=red]If dlgResult <> DialogResult.Cancel Then[/color]
 
If you want to make sure a user selects a file in an OpenFileDialog then put it inside a While loop:
VB.NET:
[color=Blue]Dim[/color] ofd [color=Blue]As New[/color] OpenFileDialog

[color=Green] 'Set dialogue properties here.[/color]

[color=Blue] While[/color] ofd.ShowDialog() <> DialogResult.OK
	MessageBox.Show("Please select a file.")
[color=Blue] End While[/color]

[color=Green] 'Use selected file name here.[/color]
 
Back
Top