Resetting an array

adi_uk

Member
Joined
Mar 22, 2006
Messages
12
Programming Experience
1-3
I have a listbox, that im reading information into from an array. When the user hits the load button, i want the information to be cleared from the listbox (which is easy), although it isnt working, since the information is still in the array. How do i clear the array and set it back to empty.
 
delete the yourArray
VB.NET:
yourArray = Nothing
or if you only want to clear the items in array (and keep the array elements)
VB.NET:
[SIZE=2][COLOR=black]Array.Clear(yourArray, 0, yourArray.Length)
[/COLOR][/SIZE]
 
Im still having problems with this. Below is the code I have, which prompts the user to save the current file, before opening a new one. However, instead of clearing the listbox and displaying the new information from the file just opened, it also displays the old data since it is in the array. Where within the following code, do i have to add the option above, or have a i got it totally wrong?

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] infile [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] StreamReader
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] count [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] aCD [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] CatalogueCD
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] catalogueNumber [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] title [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] artist [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] text [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "hello"
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] tracks [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] runningTime [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double
[/COLOR][/SIZE][SIZE=2]SaveFileDialog1.Filter = "Text Files|*.txt"
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] (SaveFileDialog1.ShowDialog() = DialogResult.OK) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] outfile [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] StreamWriter
outfile = File.CreateText(SaveFileDialog1.FileName)
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] count = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] (index - 1)
aCD = CDList(count)
outfile.WriteLine(aCD.CDTitle)
outfile.WriteLine(aCD.CDArtist)
outfile.WriteLine([/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2](aCD.CDTracks))
outfile.WriteLine([/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2](aCD.CDRunningTime))
outfile.WriteLine(aCD.CDCatalogueNumber)
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2]outfile.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
MessageBox.Show("Save File Error.", "CD Catalogue Application.")
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]ListBox1.Items.Clear()
OpenFileDialog1.Filter = "Text Files|*.txt"
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] OpenFileDialog1.ShowDialog() = DialogResult.OK [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2]infile = File.OpenText(OpenFileDialog1.FileName)
text = infile.ReadLine
[/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] (text <> [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2])
title = text
text = infile.ReadLine()
artist = text
text = infile.ReadLine()
tracks = [/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](text)
text = infile.ReadLine()
runningTime = [/SIZE][SIZE=2][COLOR=#0000ff]CDbl[/COLOR][/SIZE][SIZE=2](text)
text = infile.ReadLine()
catalogueNumber = text
text = infile.ReadLine()
aCD = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] CatalogueCD(title, artist, tracks, runningTime, catalogueNumber)
CDList(index) = aCD
index += 1
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While
[/COLOR][/SIZE][SIZE=2]infile.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] count = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] (index - 1)
aCD = CDList(count)
ListBox1.Items.Add("CD " & [/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2](count) & " " & aCD.CDTitle)
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
MessageBox.Show("Load File Error.", "CD Catalogue Application.")
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]
 
Last edited:
Help me out here.... you code doesn't make sense.....
I see where you save the array... OK... get that... you then clear out the listbox and open the OpenDialog... OKaaay....

You then open the file, loop through it and add items to the array (which hasn't been cleared out yet BTW)....

But it's after that I get confused...
You clear the list box again... and then seemingly clear the array, then use it to re-load the list box.....

-tg
 
Opps. Sorry, i posted my half edited "messing" around code. Hvae edited it now. All i need to know is where to put the array.clear option. :)
 
Well, I imagine that you would call it at the same time you clear the list box BEFORE you open and read in the file selected by the user.

-tg
 
Back
Top