Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Redim and Redim Preserve

knetcozdknetcozd Posts: 4Member
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

  • knetcozdknetcozd Posts: 4Member

    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, USAPosts: 684Member ✭✭✭
    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, USAPosts: 684Member ✭✭✭
    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.