|
-
Aug 25th, 2010, 01:59 PM
#1
Thread Starter
Junior Member
Using a collection for the first time and need help
I am trying to come up with a way to store 3 pieces of information for each employee in the company on a daily basis. So for instance
Day 1: Joe, 8, 80
Day 2: Sam, 10, 90
Day 3: Bill, 5, 40
Day 4: Bob, 8, 40
Day 5: Sue, 2, 50
I am very new to collections and most of the applications that I build are Create, Read, Update and Delete type applications that don't really need arrays so I have almost no experience with either.
This is what I have so far. Am I on the right track here or is there a better way to do what I am trying to do?
VB Code:
'
Dim iDays As Integer = NumberDaysPerMonth(Date.Now)
Dim DayNumber(iDays) As Integer
Dim EmployeeRecords As List(Of String) = New List(Of String)
For i = 1 To DayNumber.Count - 1
EmployeeRecords.Add(i)
' I want to actual call a function that will supply
' me with three strings (or fields). The three strings
' will be [EmployeeName], [Hours] and [Amount]. I
' don't need help with the function. I only need help
' with getting my collection to store the three values.
Next
For Each s In EmployeeRecords
Console.WriteLine(s)
Next
-
Aug 25th, 2010, 02:07 PM
#2
Re: Using a collection for the first time and need help
The collections allow you to put in a type of X, in your case you have used a String. However, you can create a class of your own, and put that in instead.
Code:
Friend Class MyClass
Friend UserName As String
Friend Ref As Integer
End Class
Dim Records As New List(Of MyClass)
Dim Thing As New MyClass
Thing.UserName = "John"
Thing.Ref = 5
Records.Add(Thing)
-
Aug 25th, 2010, 02:09 PM
#3
Re: Using a collection for the first time and need help
To extend this you can add your own constructor to the class that takes the parameters you require when you create it.
Code:
Sub New(TheUserName As String, TheRef As Integer)
Me.UserName = TheUserName
Me.Ref = TheRef
End Sub
Or you have a With command as well
Code:
Dim Thing As New MyClass With {.Name = "John", .Ref = 10} 'guessing at syntax!
-
Aug 25th, 2010, 10:30 PM
#4
Re: Using a collection for the first time and need help
 Originally Posted by Grimfort
Or you have a With command as well
Code:
Dim Thing As New MyClass With {.Name = "John", .Ref = 10} 'guessing at syntax!
Syntax correct, proper name is "object initialiser"
-
Aug 26th, 2010, 03:40 AM
#5
Re: Using a collection for the first time and need help
Heh, I was at home, but I didn't know that anyway, only used it a few times meself.
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
|