Question Array problem - missing element

dave72

New member
Joined
Apr 30, 2009
Messages
4
Programming Experience
Beginner
First off, apologies if my syntax is not correct, as I am new to programming. I have created an array with 7 elements. The array is populated by reading from a text file, where all 7 elements reside in a specific order. It is important that these elements stay in the correct order in the array. However, I have noticed that when one of the elements in the text file is deleted, and the array is then populated, the array does not know what to do when the expected element is not there. All array elements then shift up a place, and my application does not behave as intended. Can someone point me in the right direction to fix this problem? Basically I need to either insert some sort of placeholder for when an element is blank (if this is possible), or find some other solution to keep all the remaining array elements in their proper place. Thanks very much in advance for any responses.
 
Strict correspondence between the data in the text file and in the array index must be maintained. If a data item from the text file must be deleted, it must be immediately replaced with an empty string and carriage return.
 
Dave, I am new to this forum and hope my reply is in the expected format.

My assumption here is you have a text file that has data in comma seperated values(CSV) format or simply using lilne ending to seperate the values. Since this is a free format input you should probably verify that all 7 elements exist before processing the data. In free format text there is nothing to enforce that the user or another application has specified the correct data.

Some other solutions are write the program that creates the text file to enforce your rules. So the application would enforce the user enters all 7 elements or gives default values where the user did not specify a value.
 
Thank you for the responses. I should explain my problem more clearly here...the text file is derived from fields in a Windows form. The form contains fields that are normally all populated, and the resulting array works fine. However, when a user deletes the contents of a field, the array is re-populated, and it doesn't know what to do when an expected field is not represented in the text file. So I would be happy to just replace the expected element with a blank string (if that is the correct terminology), but I just don't know how to do that.
 
Let me understand here

Windows Form Application

OnLoad or FileOpen?? the Form reads information from Text File
User enters or changes information in Textbox Fields
On End the Form the re-creates the Text file

, Also what is the format of the Text file

Is it csv, tab or line delimited?
 
The text file is csv.

Text file's contents are pulled from fields in the form. Then an array is populated from the text file. The contents of the array are then used to format a document suitable for printing. Basically the text file is a "middleman" between the form and the array (for reasons unbeknownst to me; someone before me created this application). When a field is edited in the form, the text file is re-created. However, if a field is deleted from the form (and subsequently missing from the text file), the array does not just leave a blank space for the missing field. It slides all other fields up one spot, causing the array to have elements in the wrong spot. Is it possible to create an array with one element blank, depending on which field is deleted from the form?

Sorry for the confusion in my explanations, this is all new to me. Thanks!
 
Every time I had a comma separated text file, if there was nothing B/T the commas it gave me an empty value in that array element. I may be the way you are populating the text file. Could you show some code so we could help you.
 
Dave,

I would assume that your code would look something like this

If Textbox1.Text <> String.Empty Then
arr.Add(Textbox1.Text)
End If

This logic would cause you to miss an array element if the Textbox was empty.

Dont check for blank values in the Textbox fields, just set the array elements to the values regardless of its value.

So it would be
arr.Add(Textbox1.Text)
arr.Add(Textbox2.Text)
etc...
 
My example was using an arraylist so if it was an array it would be like this

Dim arr(6)
arr(0) = Textbox1.Text
arr(1) = Textbox2.Text
arr(3) = Textbox3.Text
etc..

Also, I am not sure why we are using an array here anyways. If the Form is used to create the text file then, just simply write the textbox values to the text file similar to this

Dim sb as New System.Text.StringBuilder
sb.Append(Textbox1.Text & ",")
sb.Append(Textbox2.Text & ",")
sb.Append(Textbox3.Text & ",")
sb.Append(Textbox4.Text & ",")
sb.Append(Textbox5.Text & ",")
sb.Append(Textbox6.Text & ",")
sb.Append(Textbox7.Text)

' Write text to textfile
 
I concur, the array only need to be present on the reading of the file.
VB.NET:
Dim str() As String
Using sr As New StreamReader("yourTextFile")
   str = sr.ReadToEnd.Split(",")
End Using
 
Back
Top