-
Dec 31st, 2019, 06:09 PM
#1
Thread Starter
New Member
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?
-
Dec 31st, 2019, 07:47 PM
#2
Re: Problem clearing an array
The array remains, but you’ve cleared the contents. You could also use...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 31st, 2019, 07:58 PM
#3
Thread Starter
New Member
Re: Problem clearing an array
-
Dec 31st, 2019, 08:53 PM
#4
Re: Problem clearing an array
Originally Posted by .paul.
The array remains, but you’ve cleared the contents. You could also use...
That's functionally equivalent to this:
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.
-
Jan 1st, 2020, 01:35 PM
#5
Thread Starter
New Member
Re: Problem clearing an array
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|