|
-
Jun 11th, 2006, 12:22 AM
#1
Thread Starter
Frenzied Member
[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
Last edited by thegreatone; Jun 11th, 2006 at 01:01 AM.
Zeegnahtuer?
-
Jun 11th, 2006, 12:55 AM
#2
Re: [serious] My first VB.Net App!
Congratulations, now learn C#
-
Jun 11th, 2006, 12:58 AM
#3
Thread Starter
Frenzied Member
Re: [serious] My first VB.Net App!
 Originally Posted by penagate
Congratulations, now learn C# 
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
-
Jun 11th, 2006, 01:04 AM
#4
Fanatic Member
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.
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Jun 11th, 2006, 01:06 AM
#5
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.
-
Jun 11th, 2006, 01:09 AM
#6
Thread Starter
Frenzied Member
Re: [serious] My first VB.Net App!
 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
?
-
Jun 11th, 2006, 01:14 AM
#7
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.
-
Jun 11th, 2006, 01:21 AM
#8
Thread Starter
Frenzied Member
Re: [serious] My first VB.Net App!
 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.
-
Jun 11th, 2006, 05:23 AM
#9
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).
I don't live here any more.
-
Jun 11th, 2006, 09:09 AM
#10
Re: [serious] My first VB.Net App!
I can't help you here. But,
Congratulations !
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
|