[serious] My first VB.Net App!
It just took me 20minutes to write my first VB.Net App (.Net FW 2)
There is no error handling or anything, but, it works!
Any tips in error handling? Or even how to do it...
Simply it strips Extended info and wma files from a winamp playlist.
VB Code:
Imports System.IO
Public Class Form1
Private Sub CMDOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMDOpenFile.Click
Dim FileName As String
CDopen.Filter = "M3U Playlist|*.m3u|All Files|*.*"
CDopen.ShowDialog()
FileName = CDopen.FileName.ToString
Dim s As String
If FileName <> "" Then
Dim sr As New StreamReader(FileName)
While sr.Peek <> -1
s = sr.ReadLine.ToString
List1.Items.Add(s)
End While
sr.Close()
End If
Label1.Text = "Line Count: " & List1.Items.Count()
End Sub
Private Sub CMDparse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMDparse.Click
Dim i As Long
For i = List1.Items.Count() - 1 To 0 Step -1
Me.List1.SelectedIndex = i
If List1.Text.StartsWith("#EXT") Or List1.Text.EndsWith(".wma") Then
List1.Items.Remove(List1.Text)
End If
Label1.Text = "Line Count: " & List1.Items.Count()
Next
End Sub
Private Sub CMDsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMDsave.Click
Dim i As Integer
Dim FileName As String
Dim sw As StreamWriter
CDsave.Filter = "M3U Playlist|*.m3u|All Files|*.*"
CDsave.ShowDialog()
FileName = CDsave.FileName
If FileName <> "" Then
sw = New StreamWriter(FileName)
For i = 0 To List1.Items.Count - 1
sw.WriteLine(List1.Items.Item(i))
Next
sw.Close()
End If
End Sub
End Class
Re: [serious] My first VB.Net App!
Congratulations, now learn C# :D
Re: [serious] My first VB.Net App!
Quote:
Originally Posted by penagate
Congratulations, now learn C# :D
Psst, that is what i was learning... I thought it was taking a bit too long, so i whipped up an app to do this is minutes.
P.S. Updated the code :)
Re: [serious] My first VB.Net App!
Error handling in .NET is done with TRY/CATCH blocks.
VB Code:
Try
'code that may produce an error
Catch (e As Exception)
'code that will be executed if there is an error
Finally
'code that will be executed in any case
End Try
The Finally block can be left out if you don't need it.
Could seem less convenient then ON ERROR GOTO from vb6 (which still works btw), but it is much better. I will personally murder you if you use ON ERROR GOTO.
Furthermore, perform a few sanity checks, like does the file exist to begin with, etc.
Re: [serious] My first VB.Net App!
Use Try/Catch as an absolute last resort - if an exception is the only way to handle a condition.
Re: [serious] My first VB.Net App!
Quote:
Originally Posted by grilkip
Error handling in .NET is done with TRY/CATCH blocks.
VB Code:
Try
'code that may produce an error
Catch (e As Exception)
'code that will be executed if there is an error
Finally
'code that will be executed in any case
End Try
The Finally block can be left out if you don't need it.
Could seem less convenient then ON ERROR GOTO from vb6 (which still works btw), but it is much better. I will personally murder you if you use ON ERROR GOTO.
Furthermore, perform a few sanity checks, like does the file exist to begin with, etc.
So
VB Code:
Private Sub CMDparse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMDparse.Click
Dim i As Long
For i = List1.Items.Count() - 1 To 0 Step -1
Me.List1.SelectedIndex = i
If List1.Text.StartsWith("#EXT") Or List1.Text.EndsWith(".wma") Then
List1.Items.Remove(List1.Text)
End If
Label1.Text = "Line Count: " & List1.Items.Count()
Next
End Sub
Would become:
VB Code:
Private Sub CMDparse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMDparse.Click
Dim i As Long
Try
For i = List1.Items.Count() - 1 To 0 Step -1
Me.List1.SelectedIndex = i
If List1.Text.StartsWith("#EXT") Or List1.Text.EndsWith(".wma") Then
List1.Items.Remove(List1.Text)
End If
Next
Catch (e as Exception)
MsgBox("There has been an error!")
Finally
Label1.Text = "Line Count: " & List1.Items.Count()
End Try
End Sub
?
Re: [serious] My first VB.Net App!
No... that would be acase for murder, there will never be an exception generated in that bit of code unless you have a logic error. In which case you don't want to handle the exception, you want it to crash so you can debug it.
Use sanity checks like grilkip said, like File.Exists(FileName) before opening the file, etc. Although, I can't remember, but the dialog might not even let you specify a non-existant file in the first place. If that's the case then you wouldn't need to check it.
Re: [serious] My first VB.Net App!
Quote:
Originally Posted by penagate
No... that would be acase for murder, there will never be an exception generated in that bit of code unless you have a logic error. In which case you don't want to handle the exception, you want it to crash so you can debug it.
Use sanity checks like grilkip said, like File.Exists(FileName) before opening the file, etc. Although, I can't remember, but the dialog might not even let you specify a non-existant file in the first place. If that's the case then you wouldn't need to check it.
Ah, thankyou. So basically go as safe as i can then :)
Cheers.
Re: [serious] My first VB.Net App!
I'm with Penagate on this one. You should always be able to avoid TryCatch if you write code well, however it is sometimes unavoidable when using some libraries that only give you feedback by the kind of exception thaty they throw (some network and database libs behave this way and its a real ball-ache).
Re: [serious] My first VB.Net App!
I can't help you here. But,
Congratulations !