Results 1 to 9 of 9

Thread: [RESOLVED] [2005] in need of a good idea

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    Resolved [RESOLVED] [2005] in need of a good idea

    Greetings,

    i have a structure element in my code which looks like

    Code:
        Public Structure EventItem
            Dim Start As Date
            Dim Finish As Date
            Dim Duration As Date
            Dim Subject As String
            Dim Tag As String
            Dim EventColor As Color
            Dim EventNo As Integer
            Dim TotalEventsOnDay As Integer
        End Structure
    which will be stored in a list

    Code:
        Public EventList As New List(Of EventItem)
    in runtime the structure will be filled like this

    Code:
            Dim ItemE As New MonthCalendar.MonthView.EventItem
    
            ItemE.Subject = "Test1"
            ItemE.Tag = "Test1"
            ItemE.Start = CDate("27.03.2007 02:15")
            ItemE.Finish = DateAdd(DateInterval.Hour, 1, ItemE.Start)
            ItemE.Duration = CDate("27.03.2007 02:15")
            ItemE.EventColor = Color.Red
            ItemE.EventNo = ??????????????        
            ItemE.TotalEventsOnDay = ??????????
            cstMonthView.EventList.Add(ItemE)
    my problem now ist
    1. How do i know how many events with the same day are allready in the list

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] in need of a good idea

    you would have to write a method that would enumerate the list, and count that information.

    If you would need to do this often, I would suggest writing a class that inherits from list, so that you can encapsulate that method in the class itself, and call it whenever needed from your code.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    Re: [2005] in need of a good idea

    Quote Originally Posted by kleinma
    you would have to write a method that would enumerate the list, and count that information.

    If you would need to do this often, I would suggest writing a class that inherits from list, so that you can encapsulate that method in the class itself, and call it whenever needed from your code.
    Thanks kleinma

    do you know any code sample where i can see how i archive this?
    Last edited by Bongo; Mar 8th, 2007 at 01:55 AM.

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

    Re: [2005] in need of a good idea

    First of all I would suggest using a class rather than a structure. I would also suggest making all those fields private and exposing them through public properties. Properties are preferable to fields for various reasons, not least of which is the fact that you cannot use fields in data binding.

    Whether you choose to accept that advice or not, your collection could look something like this:
    vb Code:
    1. Public Class EventItemCollection
    2.     Inherits System.Collections.ObjectModel.Collection(Of EventItem)
    3.  
    4.     ''' <summary>
    5.     ''' Gets a count of the number of EventItems in the collection with the specified Start value.
    6.     ''' </summary>
    7.     ''' <param name="start">
    8.     ''' The start date to match.
    9.     ''' </param>
    10.     Public Function GetCountWithStart(ByVal start As Date) As Integer
    11.         Dim count As Integer = 0
    12.  
    13.         For Each item As EventItem In Me.Items
    14.             If item.Start = start Then
    15.                 count += 1
    16.             End If
    17.         Next item
    18.  
    19.         Return count
    20.     End Function
    21.  
    22. End Class
    The Collection(Of T) class provides all the standard collection behaviour for you so you only have to provide any custom behaviour.
    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

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    Re: [2005] in need of a good idea

    @jmcilhinney

    I tried to follow your suggestion and I messed it up majorly. Would you please have a look on the attached test.zip.

    thanks in advance

    Manfred
    Attached Files Attached Files

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

    Re: [2005] in need of a good idea

    There's not much to mess up. You've already got the code for the collection. If you want to take my advice with regards to the EventItem type then it would look like this:
    vb Code:
    1. Public Class EventItem
    2.  
    3.     Private _start As Date
    4.  
    5.     Public Property Start() As Date
    6.         Get
    7.             Return _start
    8.         End Get
    9.         Set(ByVal value As Date)
    10.             _start = value
    11.         End Set
    12.     End Property
    13.  
    14. End Class
    That's for changing the Structure to a Class and the Start field to a property. All the other fields would be handled similarly.
    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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    Re: [2005] in need of a good idea

    I did this and I created a class DayItem, EventItem and EventItemCollection. And Impotet them into the Monthview class, but it looks like now the list is not holding the different items only the last

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

    Re: [2005] in need of a good idea

    I haven't looked at your attachment and I'm not going to. Just note that structures are value types and classes are reference types. If you create a structure instance and add it to a collection 10 times, changing the property values each time then you'll end up with 10 different structure instances in the collection. That's because each time you are adding a copy of the structure to the collection. If you do the same thing with a class you'll get a totally different result. The collection will contain 10 references all to the same class instance and it will only contain the last property values you set. If you want 10 class instances then you have to create 10 distinct instances, i.e. use the New keyword 10 times.
    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

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    Re: [2005] in need of a good idea

    OK now I understand my mistake - i declared on top of the methode
    Dim ItemSquare as new DayItem - but I needed it to do in the loop where the 42 different ItemSquares are been filled and putet into the list.
    Ok one major Problem solved many thanks - Manfred

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