|
-
Feb 26th, 2011, 01:12 PM
#1
Thread Starter
New Member
Loop Through An Array?
Hi,
I am designing a prisoner system
On the design i have different text boxes that display the prisoner name, birth date, number etc.
I then set all of the the different prisoners up in an array.
I want to know how to loop though this array so that when i start the program up it will start at the beginning of the array with the first prisoners details and then when i click "Next" the next prisoners details will be displayed in all of the boxes.
Can anyone help?
-
Feb 26th, 2011, 03:12 PM
#2
Hyperactive Member
Re: Loop Through An Array?
Where will the array get the data from?
Each time the program start or the form is called, the array will have no data in.
You will need some type of Database system to store the data in.
Then using an array would not be needed, as the textboxes would be filled from the database.
-
Feb 26th, 2011, 03:19 PM
#3
Thread Starter
New Member
Re: Loop Through An Array?
I have already put the data in.
I used a with statement:
with prisoners(0)
.forename = "John"
.surname = "Smith"
Etc.
and then i told it where to put this information:
textbox1.text = .forename
textbox2.text = .surname
Etc.
and then i did the same but for prisoners(1), then prisoners(2) Etc.
-
Feb 26th, 2011, 03:24 PM
#4
Hyperactive Member
Re: Loop Through An Array?
I do not understand your code, can you please post this, but make use of the CODE tags,
-
Feb 26th, 2011, 03:31 PM
#5
Thread Starter
New Member
Re: Loop Through An Array?
Right Ok.
In the module i have:
Structure prisonertype
Public forename As String
Public surname As String
Public birth As String
Public gen As Boolean
Public prisnum As Integer
Public sentence As Integer
Public cat As Integer
Public yearrel As Integer
End Structure
This is the structure to store all of the prisoners details.
Also in the module:
Dim prisoners(1) as prisonertype
This is the declaration of the array. (I am just using the two prisoners at the minute until i get it going properly)
Then in the main code i have:
With prisoners(0)
.forename = "John"
.surname = "Smith"
.birth = "14/3/83"
.prisnum = 9287202
.sentence = 6
.cat = 5
.yearrel = 2011
TextBox1.Text = .forename
TextBox2.Text = .surname
TextBox3.Text = .birth
RadioButton1.Checked = True
TextBox4.Text = .prisnum
TextBox5.Text = .sentence
TextBox6.Text = .cat
TextBox7.Text = .yearrel
End With
The first part is the prisoners information and the second part is where to put this information.
I have done the same thing for the other prisoner but instead of prisoner(0) it is prisoner(1).
now what i want to do is to be able to loop through these.
When the prgram starts up i want the first prisoner displayed and then when the next button is clicked, it will move onto the second prisoner and display their details.
-
Feb 26th, 2011, 03:46 PM
#6
Hyperactive Member
Re: Loop Through An Array?
This is NOT going to work. You will need a database to contain the data. You are placing your data in an array, which is a variable. Variables will only contain data while the program is running. Variables contain datas in the RAM (Random Access Memory). This means that it is volitail. When you exit the program or switch of the computer, the data will be lost.
When you want to add names to the array later, you are going to have problems again. By using a Database, you can store information on the harddrive (Secondary device). This why your data can be accessed easier later.
See if you can learn more about database designs. I think you need a LOT more knowledge on databases and WHY we need them.
-
Feb 26th, 2011, 04:06 PM
#7
Thread Starter
New Member
Re: Loop Through An Array?
This is a university assignment and we have been told not to use databases of any sort
-
Feb 26th, 2011, 04:06 PM
#8
Hyperactive Member
Re: Loop Through An Array?
Look at the code below, it is just an extract of code, but you will see that the Textboxes are connected to the database.
Code:
Public Class Form1
Dim dt As New DataTable()
Dim rowIndex As Integer = 0
'' This part connects the database to the form:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Get data from the database, put it into the DataTable object dt,
'and display the initial record's data in text boxes.
Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=CLASS LIST 20081.mdb"
Dim sqlStr As String = "SELECT * FROM CLASS2008"
Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)
dataAdapter.Fill(dt)
dataAdapter.Dispose()
UpdateTextBoxes()
End Sub
Sub UpdateTextBoxes()
'Display the contents of the row specified by the rowIndex variable.
txtStudentName.Text = CStr(dt.Rows(rowIndex)("student_name"))
txtSurname.Text = CStr(dt.Rows(rowIndex)("surname"))
txtSubject.Text = CStr(dt.Rows(rowIndex)("subject1"))
txtGroup.Text = CStr(dt.Rows(rowIndex)("group"))
End Sub
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
'Search through each row looking for the requested city.
'Update the fields if that city is found.
'Otherwise display a message box.
Dim StudentName As String
Dim cityFound As Boolean = False
StudentName = InputBox("Enter the name of the Student to search for.")
For i As Integer = 0 To (dt.Rows.Count - 1)
If CStr(dt.Rows(i)("student_name")) = UCase(StudentName) Then
cityFound = True
rowIndex = i
UpdateTextBoxes()
End If
Next
If (Not cityFound) Then
MsgBox("Cannot find the requested city", 0, "Not in Table")
End If
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
'Show the next record if the current one is not the last.
If (rowIndex < dt.Rows.Count) Then
rowIndex += 1 'Increase the rowIndex by 1.
UpdateTextBoxes()
End If
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
'Show the previous record if the current one is not the first.
If (rowIndex > 0) Then
rowIndex = rowIndex - 1
UpdateTextBoxes()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fmtStr As String = "{0,-15}{1,-10}{2,7:N1}{3,7:N1}{4,7:P0}"
'Place contents of Cities table into a DataTable.
Dim dt As New DataTable()
Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=CLASS LIST 20081.mdb"
Dim sqlStr As String = "SELECT * FROM CLASS2008"
Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)
dataAdapter.Fill(dt)
dataAdapter.Dispose()
'Fill the list box.
lstDisplay.Items.Add(("Student Name " & vbTab & "Surname" & vbTab & vbTab & "Group"))
For i As Integer = 0 To dt.Rows.Count - 1
lstDisplay.Items.Add(dt.Rows(i)(1) & vbTab & dt.Rows(i)(2) & vbTab & vbTab & dt.Rows(i)(3))
Next
End Sub
End Class
-
Feb 26th, 2011, 04:11 PM
#9
Hyperactive Member
Re: Loop Through An Array?
Ok, try this:
Code:
Public Structure Student
Public Name As String
Public Surname As String
Public Age As Integer
End Structure
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Mystudent As New Student
Mystudent.Name = TextBox1.Text
Mystudent.Surname = TextBox2.Text
Mystudent.Age = TextBox3.Text
' Call sub
RealStudent(Mystudent)
' Now use changed values in rest of program
MsgBox(Mystudent.Name & " " & Mystudent.Surname & " " & Mystudent.Age)
End Sub
Function RealStudent(ByRef ThisStudent As Student)
If ThisStudent.Name = "Marius" Then
ThisStudent.Name = "NO, it is Piet"
End If
If ThisStudent.Surname = "Smit" Then
ThisStudent.Surname = "NO, it is Steyn"
End If
If ThisStudent.Age < 20 Or ThisStudent.Age > 30 Then
ThisStudent.Age = 99
End If
End Function
End Class
-
Feb 26th, 2011, 04:17 PM
#10
Hyperactive Member
Re: Loop Through An Array?
Try this
Code:
Dim result As New System.Text.StringBuilder
Dim wordCollection As New Collection
' ----- Start with a basic collection.
wordCollection.Add("This")
wordCollection.Add("is")
wordCollection.Add("a")
wordCollection.Add("collection")
wordCollection.Add("of")
wordCollection.Add("words")
' ----- Insert a word after item 3.
wordCollection.Add("slightly", , , 3)
' ----- Insert a word before item 5.
wordCollection.Add("longer", , 5)
' ----- Display the collection.
For Each word As String In wordCollection
result.Append(word)
result.Append(Space(1))
Next word
MsgBox(result.ToString( ))
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|