Solved

Help with a VB.NET Dictionnary

Posted on 2015-02-15
4
81 Views
Last Modified: 2015-02-17
Hello EE,

I got a dictionnary(Of Integer, Class1)
the class1 is made of a date and a number.

Lets say my dictionar is as follow :

0 - 2015/2/13, 5
1 - 2015/2/13, 11
2 - 2015/2/13, 30
3 - 2015/2/14, 1
4 - 2015/2/14, 11
5 - 2015/2/14, 44
6 - 2015/2/15, 30


what I would like to accomplish.. is to calculate for each number, what is the number of days that it was not taken.
For example, the number 30 would be 0 because i got that number today. But the number 5 would be 2
for the number 44 it would be 1.

any help?
Thanks!
0
Comment
Question by:PhilippeRenaud
  • 2
4 Comments
 
LVL 23

Expert Comment

by:Michael74
ID: 40611552
I am not getting the question. Could you please rephrase your question.

If you are looking for the number of days each number was not used then from your example above the number 30 was only only used one day of three and so would return 2. Not sure how it could return 0. If I am on the right track then we would also need to number range being used so we can check for numbers that have not been used at all.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40611608
Hi PhilippeRenaud;

Assuming I made the right assumptions the snippet below should give you what you need.

Dim myList As New Dictionary(Of Integer, Class1)()

' Test data
myList.Add(0, New Class1() With {.MyDate = New DateTime(2015, 2, 13, 0, 0, 0), .MyNum = 5} )
myList.Add(1, New Class1() With {.MyDate = New DateTime(2015, 2, 13, 0, 0, 0), .MyNum = 11} )
myList.Add(2, New Class1() With {.MyDate = New DateTime(2015, 2, 13, 0, 0, 0), .MyNum = 30} )
myList.Add(3, New Class1() With {.MyDate = New DateTime(2015, 2, 14, 0, 0, 0), .MyNum = 1} )
myList.Add(4, New Class1() With {.MyDate = New DateTime(2015, 2, 14, 0, 0, 0), .MyNum = 11} )
myList.Add(5, New Class1() With {.MyDate = New DateTime(2015, 2, 14, 0, 0, 0), .MyNum = 44} )
myList.Add(6, New Class1() With {.MyDate = New DateTime(2015, 2, 15, 0, 0, 0), .MyNum = 30} )

' Calculate the number of days
For Each obj In myList.Values
    Dim days As Timespan = DateTime.Today.Subtract(obj.MyDate)
    Console.WriteLine("Number of Days = {0} Number = {1}", days.TotalDays, obj.MyNum)
Next


' Assuming you class looks like this
Public Class Class1
    Public Property MyDate As DateTime
    Public Property MyNum As Integer
End Class

Open in new window

0
 
LVL 1

Author Closing Comment

by:PhilippeRenaud
ID: 40614392
thanks
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40614446
Not a problem Philippe, glad to help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now