[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
- How do i know how many events with the same day are allready in the list
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.
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?
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:
Public Class EventItemCollection
Inherits System.Collections.ObjectModel.Collection(Of EventItem)
''' <summary>
''' Gets a count of the number of EventItems in the collection with the specified Start value.
''' </summary>
''' <param name="start">
''' The start date to match.
''' </param>
Public Function GetCountWithStart(ByVal start As Date) As Integer
Dim count As Integer = 0
For Each item As EventItem In Me.Items
If item.Start = start Then
count += 1
End If
Next item
Return count
End Function
End Class
The Collection(Of T) class provides all the standard collection behaviour for you so you only have to provide any custom behaviour.
1 Attachment(s)
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
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:
Public Class EventItem
Private _start As Date
Public Property Start() As Date
Get
Return _start
End Get
Set(ByVal value As Date)
_start = value
End Set
End Property
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.
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
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.
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