-2

I have a double() and once I'm done using it I want to use it again by setting the array to null. Apparently that's a terrible practice to do in .net so what's the right way to reinitialize an array back to nothing?

Array.clear leaves me with an array of zeros. This variable can be used by the entire class so creating a new array doesn't answer the question either. The code below shows how the variable is used. It's used in multiple methods. I hope this example explains what I'm trying to ask.

class foo
    private dim myArray as new double()

    public sub createArrayFromFile()
        If(not isNothing(myArray))
           //set myarray to null. what's this code?
        End If
        //Add data to myArray by parsing a file

    end sub

    public sub createArrayFromMachine()
        If(not isNothing(myArray))
           //set myarray to null. what's this code?
        End If
        //Add data to the array from a machine
    end sub

    public sub sendToDB()
        if(not isNothing(myarray)
           //code that puts the data into the database
        end if
    end sub
end class
7
  • 1
    It would really help if you could post your code. Perhaps you should just create a new array? What are you really trying to achieve?
    – Jon Skeet
    Jun 11, 2014 at 14:34
  • 1
    An array is written as double[], not double(); the latter is a method. Give us a source for this "terrible practice", I find that a very curious statement. Jun 11, 2014 at 14:35
  • 2
    @JeroenVannevel OP might be talking about VB.Net, where () is used for arrays (and methods, and properties).
    – sloth
    Jun 11, 2014 at 14:39
  • @sloth That's correct.
    – Luminous
    Jun 11, 2014 at 14:42
  • Then you might want to mark what language dialect you're using, in the question, so there aren't any misunderstandings. :)
    – J. Steen
    Jun 11, 2014 at 14:45

3 Answers 3

2

You can either simply create a new array and let the garbage collector do its work:

my_array = {} ' create a new, empty array '

or, if you want to reuse and clear your array, use the Array.Clear method:

Array.Clear Method

Sets a range of elements in an array to the default value of each element type.

or change its size:

ReDim my_array(-1) ' delete all elements and set size to 0 or whatever '
8
  • I did try that at first, but that only set all of the elements to zero.
    – Luminous
    Jun 11, 2014 at 14:47
  • double is a value type so the array values can't be null. When you create a new array of double, it's created with zeros. If you want to be able to set the values to null, you can use an array of double? instead.
    – Adi Lester
    Jun 11, 2014 at 14:51
  • 1
    @user2567273 Then why not simply create a new array?
    – sloth
    Jun 11, 2014 at 14:54
  • So "?" would allow you to set a double() (or whatever array of primitive type I guess) to null?
    – Luminous
    Jun 11, 2014 at 14:55
  • @user2567273 Adi Lester talks about creating an array of double? instead of double. double? is a nullable type, hence a value of type double? can be Nothing. I still don't get your problem. You can of course set an array variable to Nothing and you can also just change the size of an already existing array with ReDim.
    – sloth
    Jun 11, 2014 at 14:59
1

You do not need to worry about managing memory in .NET in this case (and in most other cases as well). If you leave an array full of data when your program ends, it will be released automatically by the garbage collector.

Learn about managing the scope of your variables. Always declare your variables at the right level, to let garbage collector release the associated memory as soon as possible.

If you need to reuse the same variable for a different purpose, think again. In most cases it's best to declare a different variable instead. If you are looping, and need a new array every time, declare an array inside the loop, so when it goes to next iteration, it's cleaned up automatically.

EDIT: Regarding your question edit, here is how you can do it:

class foo
  private dim myArray as new double()

  public function foobar() as new double()
    Dim myArrayLocal1 As new double()
    'Add some stuff to the myArrayLocal1 
    If smth then Return myArrayLocal1

    Dim myArrayLocal2 As new double()
    'Add some stuff to the myArrayLocal2 
    Return myArrayLocal2 
  end sub

  'then do this myArray = foobar
end class

The function can also probably be declared Shared. Although it really looks like you need two separate functions in this case. For me to give you a more concrete example, please add more detail about the actual "some stuff" code.

7
  • I come from the world of Java so maybe I'm missing something completely that I can't do in VB.net. Given the scope of the variable in question (check out the example I added) creating a new variable won't work.
    – Luminous
    Jun 11, 2014 at 15:20
  • @user2567273: You can do a lot of things in VB.NET, but there is only a subset of those you should use, if you want to keep your code clean. Check my edit. Jun 11, 2014 at 15:26
  • Oh I absolutely agree with you there. So many more things you can do "out of the box" in VB.NET. I hope my modified code example gives more insight on what's going on.
    – Luminous
    Jun 11, 2014 at 15:37
  • 1
    @user2567273: Right, so this is exactly what I was talking about. You even got the naming of methods correctly (self-explanatory). All you need to do is convert public sub createArrayFromMachine() to a function, returning an array you need. Then in caller code do an If branch, and set myarray = createArrayFromFile() or myarray = createArrayFromMachine(). In each create you would declare a local variable which is already "empty", use it & return. This will replace a reference of myarray. When you reassign myarray, its old data will become subject of garbage collection (automatically). Jun 11, 2014 at 15:46
  • 1
    You're comment is at last upvoted @Neolisk. ♥
    – Luminous
    Oct 7, 2021 at 19:07
0

This was exactly what I was looking for

myArray = nothing
2
  • Statements like this lead to unmaintainable code, and headache for future developers supporting your application. Jun 11, 2014 at 22:09
  • 1
    True. In spirit of answering the question. This is it. After reading your comment though, I refactored and created a new object so now I'm passing around and referencing only this local variable. It still leaves me with a global variable, but now I'm only assigning to it and referencing one time.
    – Luminous
    Jun 12, 2014 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.