Results 1 to 10 of 10

Thread: [serious] My first VB.Net App!

  1. #1

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    [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:
    1. Imports System.IO
    2. Public Class Form1
    3.  
    4.     Private Sub CMDOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMDOpenFile.Click
    5.         Dim FileName As String
    6.         CDopen.Filter = "M3U Playlist|*.m3u|All Files|*.*"
    7.         CDopen.ShowDialog()
    8.         FileName = CDopen.FileName.ToString
    9.         Dim s As String
    10.         If FileName <> "" Then
    11.             Dim sr As New StreamReader(FileName)
    12.             While sr.Peek <> -1
    13.                 s = sr.ReadLine.ToString
    14.                 List1.Items.Add(s)
    15.             End While
    16.             sr.Close()
    17.         End If
    18.         Label1.Text = "Line Count: " & List1.Items.Count()
    19.     End Sub
    20.  
    21.     Private Sub CMDparse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMDparse.Click
    22.         Dim i As Long
    23.         For i = List1.Items.Count() - 1 To 0 Step -1
    24.             Me.List1.SelectedIndex = i
    25.             If List1.Text.StartsWith("#EXT") Or List1.Text.EndsWith(".wma") Then
    26.                 List1.Items.Remove(List1.Text)
    27.             End If
    28.             Label1.Text = "Line Count: " & List1.Items.Count()
    29.         Next
    30.     End Sub
    31.  
    32.     Private Sub CMDsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMDsave.Click
    33.         Dim i As Integer
    34.         Dim FileName As String
    35.         Dim sw As StreamWriter
    36.         CDsave.Filter = "M3U Playlist|*.m3u|All Files|*.*"
    37.         CDsave.ShowDialog()
    38.         FileName = CDsave.FileName
    39.         If FileName <> "" Then
    40.             sw = New StreamWriter(FileName)
    41.             For i = 0 To List1.Items.Count - 1
    42.                 sw.WriteLine(List1.Items.Item(i))
    43.             Next
    44.             sw.Close()
    45.         End If
    46.     End Sub
    47. End Class
    Last edited by thegreatone; Jun 11th, 2006 at 01:01 AM.
    Zeegnahtuer?

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [serious] My first VB.Net App!

    Congratulations, now learn C#

  3. #3

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: [serious] My first VB.Net App!

    Quote 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
    Zeegnahtuer?

  4. #4
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: [serious] My first VB.Net App!

    Error handling in .NET is done with TRY/CATCH blocks.
    VB Code:
    1. Try
    2. 'code that may produce an error
    3. Catch (e As Exception)
    4. 'code that will be executed if there is an error
    5. Finally
    6. 'code that will be executed in any case
    7. 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

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  6. #6

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: [serious] My first VB.Net App!

    Quote Originally Posted by grilkip
    Error handling in .NET is done with TRY/CATCH blocks.
    VB Code:
    1. Try
    2. 'code that may produce an error
    3. Catch (e As Exception)
    4. 'code that will be executed if there is an error
    5. Finally
    6. 'code that will be executed in any case
    7. 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:
    1. Private Sub CMDparse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMDparse.Click
    2.         Dim i As Long
    3.         For i = List1.Items.Count() - 1 To 0 Step -1
    4.             Me.List1.SelectedIndex = i
    5.             If List1.Text.StartsWith("#EXT") Or List1.Text.EndsWith(".wma") Then
    6.                 List1.Items.Remove(List1.Text)
    7.             End If
    8.             Label1.Text = "Line Count: " & List1.Items.Count()
    9.         Next
    10.     End Sub

    Would become:
    VB Code:
    1. Private Sub CMDparse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMDparse.Click
    2.         Dim i As Long
    3. Try
    4.         For i = List1.Items.Count() - 1 To 0 Step -1
    5.             Me.List1.SelectedIndex = i
    6.             If List1.Text.StartsWith("#EXT") Or List1.Text.EndsWith(".wma") Then
    7.                 List1.Items.Remove(List1.Text)
    8.             End If
    9.         Next
    10. Catch (e as Exception)
    11.     MsgBox("There has been an error!")
    12. Finally
    13.     Label1.Text = "Line Count: " & List1.Items.Count()
    14. End Try
    15.     End Sub

    ?
    Zeegnahtuer?

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  8. #8

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    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.
    Zeegnahtuer?

  9. #9
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  10. #10
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: [serious] My first VB.Net App!

    I can't help you here. But,
    Congratulations !
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width