Redim and Redim Preserve

Can some please explain how to use these,i have an assignment to read data and write data to a sequential file using an array with 100 values,i am displaying the results in a listbox.

The problem is when i want to edit the sequential file,in order to edit the sequential file,i have to load what ever data in the file into the array,and then delete/move and recreate the file,when writing the array contents back into sequential file,the whole array is written including blanks,this is a problem when i populate the listbox ,it shows these blanks,i do not want this,just want to show existing data only.

I have tried the redim,but it just clears all the data in the arrary which defeats the point of the program. see below the edit code:

If WriteMode = 2 Then


Call Write()
Me.writedata.Close()
Dim index As Integer
Dim read As StreamReader = File.OpenText("members.txt")
Do While read.Peek <> -1 And index < 100
'load paired arrays
members(index) = read.ReadLine()
PhoneNum(index) = read.ReadLine()
index = index + 1
Loop
index= Me.membersListBox.SelectedIndex
members(index) = Me.MemberTextbox.Text
PhoneNum(index)= Me.PhoneTextbox.Text


read.Close()

File.Delete("members.txt")




Call Write()
writedata.Close()

Call Write()

Me.membersListBox.Items.Clear()
Dim membersCount As Integer = members.Length
For index = 1 To members.Length - 1

writedata.WriteLine(members(index))
writedata.WriteLine(PhoneNum(index))

Next index

writedata.Close()


End If

Call LoadMemberList()


End Sub

Comments


  • this is the loadmemberlist sub called above

    Private Sub LoadMemberList()
    ' error detection just in case an unforseen error occurs

    'clear listbox to add new values
    Me.membersListBox.Items.Clear()
    Dim index As Integer
    'intialize read object
    Dim read As StreamReader = File.OpenText("members.txt")

    ' check file for end and make sure that array index does noot exceed 100
    index = 0
    Do While read.Peek <> -1 And index < 100
    'load paired arrays

    members(index) = read.ReadLine()
    PhoneNum(index) = read.ReadLine()
    'load listbox
    Me.membersListBox.Items.Add(members(index) & Space(5) & PhoneNum(index))

    index = index + 1

    Loop

    read.Close()


  • seancampbellseancampbell Pennsylvania, USA
    First order on the list: Explaining Redim and Redim Preserve

    Redim is used to change the amount of memory allocated to arrays.

    So when I do
    [code]
    Dim Arr(100) as Integer
    'I have 101 cells set aside for my Array. (0-100)
    Redim Arr(10) as Integer
    'I now have 11 cells set aside for my Array. (0-10)
    [/code]

    When you do a ReDim, it will erase all of the memory in those cells. Resetting all values to 0 (or "" for string, or Nothing in the case of a class object).

    The Preserve keyword is used to save the memory that was in the array. This way you can dynamically set the size of your array, and increase the size later if you need to. If you shrink the size of an array with the Preserve keyword, the data in the cells removed (from the end) is lost forever but the rest is fine.

    [code]
    Dim Arr(10) as Integer
    'This for loop will go through all of the
    'cells in the Array regardless of size
    For i as Integer = 0 to Arr.Length - 1
    Arr(i) = i + 1
    Next i

    ReDim Preserve Arr(12) as Integer
    'We just increased the size of the Array without
    'losing the data we already populated
    MsgBox("Arr(10) = " & Arr(10).toString & " Arr(11) = " & Arr(11))
    [/code]

    -firesickle.com
  • seancampbellseancampbell Pennsylvania, USA
    I see that you are doing this:

    [code]
    For index = 1 To members.Length - 1

    writedata.WriteLine(members(index))
    writedata.WriteLine(PhoneNum(index))

    Next index
    [/code]

    This appears that it would dynamically write the data into your textfile, based on how many values you have in Members. In actuality, Members.Length will always be the same if you never do a Redim. So regardless of how many cells you had populated with data (10,20,30, etc) it will always run the loop for ever cell that exists in the array.

    Example:
    [code]
    Dim Arr(100) as Integer

    For i as integer = 0 to 10
    Arr(i) = i + 1
    Next i
    [/code]

    Arr.Length would be 101, Arr(0) - Arr(100). So when I do this:
    [code]
    For i as integer = 0 to Arr.Length
    Msgbox(Arr(i))
    Next i
    [/code]
    It would popup 101 message boxes (please dont do that one, its annoying trust me).

    So in order to get around this, you can simply designate a Form Level Variable to tally the amount of members you have added or loaded.

    After the line that appears like this:
    Public Class Form1

    And after the collapsable Region called [Windows Designer Generated Code](i think this is only in 1.1 framework) Dim a variable.

    It will look as follows:
    [code]
    Public Class Form1

    Dim MemberCount as Integer = 0
    Private Sub New(...)
    ...
    End Sub

    End Class
    [/code]

    When you load all of your members out of the file, set MemberCount equal to the final index value. You can then use this variable. Keep in mind that when the form first loads the value of MemberCount is 0.

    I hope this helps you! If you have any more questions on your problem, please be a bit more clear about what your looking for. It took me reading your request several times to realize that your issue wasn't with ReDim.

    -firesickle.com
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories