Results 1 to 5 of 5

Thread: Problem clearing an array

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2014
    Posts
    9

    Problem clearing an array

    I am using the following array and I'm having difficulty disposing of it properly according to Project Analyzer by Aivosto.

    Dim buffer As Byte() = Nothing
    Dim sLinetoInput2 As New StreamReader(Application.StartupPath & "\Muo")
    buffer = Encoding.ASCII.GetBytes(shunk & vbCrLf)
    sLine.Write(buffer, 0, buffer.Length) '''' WRITE TO THE FILE
    sLinetoInput2.Close()
    sLine.Close()

    I tried Array.Clear(buffer, 0, buffer.Length) but according to Project Analyzer, the array is not deallocated properly. How do I dispose of it accordingly?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Problem clearing an array

    The array remains, but you’ve cleared the contents. You could also use...

    Code:
    Erase buffer

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2014
    Posts
    9

    Re: Problem clearing an array

    Thanks paul, that worked

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Problem clearing an array

    Quote Originally Posted by .paul. View Post
    The array remains, but you’ve cleared the contents. You could also use...

    Code:
    Erase buffer
    That's functionally equivalent to this:
    vb.net Code:
    1. buffer = Nothing
    which is, in my opinion, better code because it's more clear what's actually happening.

    Either is pointless though, if we're talking about a local variable in a relatively short block of code. Such a variable will cease to exist at the end of that block anyway, so setting it to Nothing just before that doesn't achieve anything useful. If you don't set all your other variables to Nothing then there's no reason to do it specifically for array variables. It is definitely a good idea to consider the lifetime of all variables and whether they need to be disposed or cleared but no serious developer that I've ever seen sets all their local variables to Nothing. If this is a long-lived variable that refers to an object that you no longer need, then it would be worth setting it to Nothing. Otherwise, it serves no useful purpose.

    Maybe it's worth doing it just to clear the warning if that's how the analyser is configured, much like you need to initialise a variable to Nothing if it's passed to a method by reference in order to avoid a warning about its not being initialised, but it's not actually doing anything useful and it's important to understand that. On a similar note, this is also pointless:
    Code:
    Dim buffer As Byte() = Nothing
    Dim sLinetoInput2 As New StreamReader(Application.StartupPath & "\Muo")
    buffer = Encoding.ASCII.GetBytes(shunk & vbCrLf)
    Why initialise a variable to Nothing if you're going to assign a value to it two lines later? In fact, why declare it there at all if you're not going to use it until two lines later? Why not declare it where it's actually needed and assign the actual value to it that you want it to have at the time?
    Code:
    Dim sLinetoInput2 As New StreamReader(Application.StartupPath & "\Muo")
    Dim buffer As Byte() = Encoding.ASCII.GetBytes(shunk & vbCrLf)
    Given that the GetBytes method has a return type of Byte(), the type of the variable can even be inferred, if you're into that:
    Code:
    Dim sLinetoInput2 As New StreamReader(Application.StartupPath & "\Muo")
    Dim buffer = Encoding.ASCII.GetBytes(shunk & vbCrLf)
    On an unrelated note, I would also recommend that you acquaint yourself with the Path.Combine method for combining partial file and folder paths:
    Code:
    Dim sLinetoInput2 As New StreamReader(Path.Combine(Application.StartupPath, "Muo")
    Dim buffer = Encoding.ASCII.GetBytes(shunk & vbCrLf)
    That way, you don't have to care about whether each part has a leading or trailing slash as the method will ensure the right number of slashes regardless.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2014
    Posts
    9

    Re: Problem clearing an array

    Thanks for the tips

Tags for this Thread

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