Results 1 to 5 of 5

Thread: Using a collection for the first time and need help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    28

    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:
    1. '
    2.         Dim iDays As Integer = NumberDaysPerMonth(Date.Now)
    3.         Dim DayNumber(iDays) As Integer
    4.         Dim EmployeeRecords As List(Of String) = New List(Of String)
    5.  
    6.         For i = 1 To DayNumber.Count - 1
    7.             EmployeeRecords.Add(i)
    8.             ' I want to actual call a function that will supply
    9.             ' me with three strings (or fields). The three strings
    10.             ' will be [EmployeeName], [Hours] and [Amount]. I
    11.             ' don't need help with the function. I only need help
    12.             ' with getting my collection to store the three values.
    13.         Next
    14.  
    15.         For Each s In EmployeeRecords
    16.             Console.WriteLine(s)
    17.         Next

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    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)

  3. #3
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    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!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Using a collection for the first time and need help

    Quote Originally Posted by Grimfort View Post
    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"
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    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
  •  



Click Here to Expand Forum to Full Width