Click here to Skip to main content
15,904,500 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: How to avoid serialization of events Pin
Bergerrudi23-Feb-07 0:36
Bergerrudi23-Feb-07 0:36 
Questionstring to DateTime Conversion Pin
Jonathan Gibbs22-Feb-07 4:17
Jonathan Gibbs22-Feb-07 4:17 
AnswerRe: string to DateTime Conversion Pin
Stefan Troschuetz22-Feb-07 7:44
Stefan Troschuetz22-Feb-07 7:44 
GeneralRe: string to DateTime Conversion Pin
Jonathan Gibbs23-Feb-07 2:57
Jonathan Gibbs23-Feb-07 2:57 
QuestionCheck fields for specific characters Pin
rhon_ridge22-Feb-07 3:15
rhon_ridge22-Feb-07 3:15 
AnswerRe: Check fields for specific characters Pin
nlarson1122-Feb-07 6:01
nlarson1122-Feb-07 6:01 
QuestionWhen to close streamreaders/writers Pin
Marcus J. Smith22-Feb-07 3:12
professionalMarcus J. Smith22-Feb-07 3:12 
AnswerRe: When to close streamreaders/writers Pin
Dave Kreskowiak22-Feb-07 5:05
mveDave Kreskowiak22-Feb-07 5:05 
Normally, you'd place the .Flush/.Close and .Dispose in the Finally block. You'd first check to see that the stream is indeed open before you called Close on it, then Dispose it.

Why? Because your code could throw an exception before or during the file open, so it's possible that the file never gets opened. Also, it's possible that your code could throw an exception during the manipulation of the file, leaving the file hung open in the Catch/Finally handlers. Finally gets executed no matter what happens in the Try block, so checking and closing the file in the Finally block is the best place to do it.
Dim sw As StreamWriter
Try
    sw = New StreamWriter(filePath)
    For x As Integer = 1 to 100
        sw.WriteLine(x)
    Next
Catch ex As Exception
    ' used a 'catch all' Exception which is not really good practice!!
Finally
    ' You'll only get a StreamWriter object if the file was successfully opened!
    If Not sw Is Nothing Then
        sw.Close()
        sw.Dispose()
    End If
End Try

Now in place of that, with VB.NET 2005, you can instead use the Using keyword to do the same thing. When the code execution leaves the Using block for any reason the object created on the Using line is automatically closed Disposed.
Try
    Using sw As StreamWriter = New StreamWriter(filePath)
        For x As Integer = 1 to 100
            sw.WriteLine(x)
        Next
    End Using
Catch ex As Exception
    ...
End Try



Dave Kreskowiak
Microsoft MVP - Visual Basic


GeneralRe: When to close streamreaders/writers Pin
Marcus J. Smith22-Feb-07 5:21
professionalMarcus J. Smith22-Feb-07 5:21 
GeneralRe: When to close streamreaders/writers Pin
Dave Kreskowiak22-Feb-07 5:25
mveDave Kreskowiak22-Feb-07 5:25 
GeneralRe: When to close streamreaders/writers Pin
Marcus J. Smith22-Feb-07 5:28
professionalMarcus J. Smith22-Feb-07 5:28 
GeneralRe: When to close streamreaders/writers Pin
Dave Kreskowiak22-Feb-07 5:56
mveDave Kreskowiak22-Feb-07 5:56 
GeneralRe: When to close streamreaders/writers Pin
Marcus J. Smith22-Feb-07 6:13
professionalMarcus J. Smith22-Feb-07 6:13 
GeneralRe: When to close streamreaders/writers Pin
Dave Kreskowiak22-Feb-07 6:28
mveDave Kreskowiak22-Feb-07 6:28 
QuestionRAR Pin
Navneet Hegde22-Feb-07 1:22
Navneet Hegde22-Feb-07 1:22 
AnswerRe: RAR Pin
Dave Kreskowiak22-Feb-07 3:17
mveDave Kreskowiak22-Feb-07 3:17 
QuestionSetup Project won't build/re-build Pin
HamCoder22-Feb-07 0:52
HamCoder22-Feb-07 0:52 
AnswerRe: Setup Project won't build/re-build Pin
Johan Hakkesteegt22-Feb-07 21:07
Johan Hakkesteegt22-Feb-07 21:07 
QuestionPInvoke Imbalance Stack function Pin
sundar_mca22-Feb-07 0:39
sundar_mca22-Feb-07 0:39 
AnswerRe: PInvoke Imbalance Stack function Pin
Guffa22-Feb-07 0:54
Guffa22-Feb-07 0:54 
GeneralRe: PInvoke Imbalance Stack function Pin
sundar_mca22-Feb-07 1:24
sundar_mca22-Feb-07 1:24 
GeneralRe: PInvoke Imbalance Stack function Pin
Dave Kreskowiak22-Feb-07 3:16
mveDave Kreskowiak22-Feb-07 3:16 
QuestionExport DatagridView to Excel Pin
Socheat.Net21-Feb-07 22:40
Socheat.Net21-Feb-07 22:40 
AnswerRe: Export DatagridView to Excel Pin
Dave Kreskowiak22-Feb-07 4:44
mveDave Kreskowiak22-Feb-07 4:44 
QuestionSet a column in datagrid to display a time. Pin
kendo1721-Feb-07 22:04
kendo1721-Feb-07 22:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.