Saving input to an array

for printing or displaying on another form

Page 1 of 1

2 Replies - 2884 Views - Last Post: 11 December 2008 - 08:06 PM Rate Topic: -----

#1 ddollar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 16-January 08

Saving input to an array

Posted 09 December 2008 - 02:50 PM

I don't see much on writing to an array as the user inputs data and clicks a button to save the information. Here is what i need to do. I have two textboxes and each value will be saved to an array. I was thinking I would just initialize the array at index 0,0, write in a value, increment the column subscript by one and write in the other value.

Does that sould like best practice or is there a more efficient approach?

The form has text boxes to hold the input and if stored in an array can be printed.

 ' module-level variables
 Private RowIndexInteger, ColumnIndexInteger as Integer ' zero by default
 Private StudentInfo(9,1) ' size is ten and holds StudentName and StudentID

Private Sub AddStudentButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddStudentButton.Click

 ' my idea was simply to store the text box text in position 0,0
 StudentInfo(RowIndexInteger, ColumnInteger) = StudentNameTextBox.Text

 ' then increment the column and store the next text box text in position 0,1
 StudentInfo(RowIndexInteger, ColumnInteger + 1) = StudentIDTextBox.Text

 ' then increment the Row and wait for next record.
 RowIndexInteger += 1
End Sub


*edit: Fixed closing tag. Just /code in square brackets are all you need.



Anyone ever write to an array as input is collected one record at a time?

Thanks for any advice.

ddollar

This post has been edited by Martyr2: 09 December 2008 - 03:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Saving input to an array

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,179
  • Joined: 18-April 07

Re: Saving input to an array

Posted 09 December 2008 - 03:23 PM

Well I am sure you see that there is going to be a problem once the user clicks the add button enough times. It is going to overflow the size of your array and cause errors. So you have three main choices here...

1) Not so good - Put in an if statement that checks to make sure that they have not added more student names than the size of the array. You would do this by keeping a counter.

2) Good - You can setup an arraylist which is like an array but is dynamic. Meaning you can add one by one and it will grow or you can delete from it at will.

3) Better - Create a student class which you can then create instances of students, give them a name and then stick the student classes into the arraylist.

You might have trouble finding examples of dumping input straight into an array because you should be checking the input, perhaps creating some objects with that input, and then storing a more refined and validated object into the array. Very rarely will you dump what a user gives you straight into an array.

So I suggest you do a little reading on the arraylist object and how that works and if you want to go with a student class object you can read up on creating objects using a constructor.

Hope this helps!


"At DIC we be student storing code ninjas.... yes we have a group of the world's top students locked up writing code for us.... No Ralphelo, you can't have more food until you finish that paper on B-tree structures!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 ddollar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 16-January 08

Re: Saving input to an array

Posted 11 December 2008 - 08:06 PM

Thanks for the reply. I did create a student class with properties and constructor, used it to create an object and stored the input in an arraylist. worked out nicely compared to my original approach. but my instructions specify saving the information to an array of size ten (correct defination would have been StudentInfoArray(4,1)). I thought if I used the arraylist to start it would be easy enough to add or remove information from the array as necessary. Maybe then I could use the information and stick it in a fixed size array. But they sounds like a lot of gyrations just to keep from working with a fixed array.

The user inputs student information that is added to a listbox and saved to the array. ArrayList works well for this because it will grow or shrink as needed.

If I try a two-dimensional array, StudentInfoArray(4,1) adding to it is one matter, but how about removing information from the array? Is that even possible? If it is its not a 'remove' or 'delete' is it?

I've searched quite a bit today and see postings stating that you can't remove from a multi-dimensional array.

If the user clicks a button to remove a selected entry from the listbox it should also be removed from the array. To acheive such a thing, would I loop to clear the array and use the listbox collection to populate it again and call that a delete? Is that how fixed size arrays are updated?

Is there a more efficient method?

Thanks again.
ddollar
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1