array questions for a VB Learner?
i am having problems putting text from 8 tb's on a dialog form into an array and passing the data back to a mainform as a single object, i then want to put this object into an array of like objects and be able to display each of the values/references from a selected object in the mainform array of objects.
NE help would be greatly appreciated as i have read all the articles on arrays i can find and am still having trouble understanding them.
eg
df = dialogform
on dialogform OK click
dialogform.stringMsg() = an array of 8 textbox values
add stringMsg to mainform.objarray
mainform.list.add objarray
mainform.label.text = list.objarray.selectedValue
''''each of the values in the original stringMsg go into the label or to seperate textboxes''''
this is the basic structure of what i want to accomplish
[858 byte] By [
Luke_82] at [2008-2-22]
You need to create an array, then put each value in.
Dim names(8) As Stringnames(0) = textBox1.Text
names(1) = textBox2.Text
names(2) = textBox3.Text
names(3) = textBox4.Text
names(4) = textBox5.Text
names(5) = textBox6.Text
names(6) = textBox7.Text
names(7) = textBox8.Text
helps to add to thefirst array but wati want is
eg
Persons Array on mainform
| Id(0) | Name(0) | address(0) | Phone(0) |
| Id(1) | Name(1) | address(1) | Phone(1) |
| Id(2) | Name(2) | address(2) | Phone(2) |
string array on dialog form
| id(i) | Name(i) | address(i) | Phone(i) |
add string array to Persons array
then display a selected person from Persons array
ok so i add a propertie for each of the textbox values to my class
then add the class to the mainform array is that right, like i said still learning,
so bear with me please, then i can call array.person.name or id or address orany other propertie of the person/class in the array
Yes, that's exactly right. The idea is if you have a number of arrays, how can you be sure that a certain array index points to the same set of data across all the arrays ? Putting them all in an object ties the data belonging to one person to the one spot.
thank u very much is great help, just to clarify
'will it display name
me.label.text = person(name) 'name of person in people array
'and will this display all of a person 'person created from class
for i = 0 to 7
me.label.text = person(i) & vbcrlf
'and this will display all people ' group or array of person(s)
for each obj in people
me.label.text = me.label.text & obj & vbcrlf
me.label.text = person(name)
No - person.name is what you want.me.label.text = person(i) & vbcrlf
No, the easiest thing to do is define ToString() so person(i).ToString prints out all of the details in the format you want
me.label.text = me.label.text & obj & vbcrlf
No, again, you need to use ToString, or some other method to format the data
ok, i have seen reference to i think it is called overloading the .tostring method but dont understand, have seen en example in a code block before but dont understand how the string.format(?) works/what the values are
this is in the class that creates the object to format
Public Overrides Function ToString() As String
' Normally ToString returns the fully qualified typename. ' In this example it would be VBNET.HowTo.Arrays.Customer ' We are overriding it so that we can return a simple ' display string when we're added to a list box. Return String.Format("Id={0}, Name={1}", Me.Id, Me.Name) End Functionthis displays the data
Private Sub DisplayArrayData(ByVal arr As Array) Dim i As Integer Dim u As Integer = (arr.Length - 1) Me.listbox.Items.Clear() For i = 0 To u Me.listbox.Items.Add(String.Format("{0} = {1}", i, arr.GetValue(i).ToString())) Next End Sub