Click here to Skip to main content
15,892,768 members
Home / Discussions / Visual Basic
   

Visual Basic

 
QuestionVBA text problem Pin
KORCARI28-Jun-11 10:51
KORCARI28-Jun-11 10:51 
AnswerRe: VBA text problem Pin
ChandraRam29-Jun-11 3:44
ChandraRam29-Jun-11 3:44 
AnswerRe: VBA text problem Pin
Dalek Dave29-Jun-11 5:44
professionalDalek Dave29-Jun-11 5:44 
QuestionInstaller [modified] Pin
KenBonny27-Jun-11 4:17
KenBonny27-Jun-11 4:17 
AnswerRe: Installer Pin
KenBonny27-Jun-11 21:22
KenBonny27-Jun-11 21:22 
QuestionDeviceID from CD Pin
JR21225-Jun-11 20:15
JR21225-Jun-11 20:15 
AnswerRe: DeviceID from CD Pin
JR21225-Jun-11 23:43
JR21225-Jun-11 23:43 
QuestionUsing LINQ to Objects to generate a frequency table - returned as a Dictionary [modified] Pin
Steven St. John25-Jun-11 5:28
Steven St. John25-Jun-11 5:28 
I'm hesitant to ask this question since I may not understand the answer. My VB.NET (2008) skills are weak and my use of LINQ has so far been non-existent. I believe that I could get the result I want using other methods than LINQ, but this may be an opportunity for me to learn it a bit.

I have a List(Of Integer) that holds a bunch of numbers that represent durations of events in milliseconds. I need to generate a frequency table from these durations. For example, the list (usually containing about 500 elements) might be:

67, 85, 110, 112, 83, 130, 99, 105

And I might want a frequency table that looks something like this:

Duration --> Count
61-80 --> 1
81-100 --> 3
101-120 --> 3
121-140 --> 1

Here, the BinSize is 20, but this would be a user-specified value, so I don't know what my "bins" are at design-time. Also, the MaxDur might be 140 in this example, but that is also a user-specified value, so I don't know how many bins there will be at design-time. (The minimum will always be 1, so there should also be bins above for 1-20, 21-40, and 41-60 which have counts of 0).

I think it would be most useful to store the results in a Dictionary(Of Integer, Integer) where the key is the upper limit of the bin (20, 40, 60, 80, 100, 120, and 140 in this example) and the value is the counts.

Consider the following code (I'm writing off of the top of my head here):

Sub TheCaller()
   'Assume we have a exampleData = List(Of Integer) with the above 8 elements
   Dim count As Integer = GetFrequencies(exampleData)
End Sub

Function GetFrequencies(data As List(Of Integer)) As Integer
   Return (From i In data Where i>60 And i<=80).Count()
End Function


I'm not sure I'm right, but would this return the frequency count of bin 61 to 80 milliseconds? I presume I could make 61 and 80 variables and send those as parameters to the function, but what I really want to learn how to do is generate a single function that returns a Dictionary(Of Integer, Integer) with the entire table generated. I'm thinking this requires a Group By clause. I've seen examples out there of frequency tables for a known category (e.g., count the number of orders from a particular company or the number of times a particular word appears), but I'm struggling with the fact that the number and size of my categories would be unknown.

Thanks in advance for any leads.

EDIT: In fact, I think I've got a good compromise working now. I have the caller running a Do... Loop Until iteration, retrieving the Count each time from the GetFrequencies function, which is called in the Add method of a Dictionary(Of Integer, Integer) instance. It's working well.

However, I'm still intrigued by the method ToDictionary of Enumerable. I think there is probably a more efficient (or at least concisely written) way to do this, but I'm not sure.

modified on Saturday, June 25, 2011 3:07 PM

QuestionAdd Reference At Runtime Pin
Milad.Biroonvand23-Jun-11 8:47
Milad.Biroonvand23-Jun-11 8:47 
AnswerRe: Add Reference At Runtime Pin
Eddy Vluggen23-Jun-11 9:12
professionalEddy Vluggen23-Jun-11 9:12 
QuestionVB and MS Excel Pin
geekgautam23-Jun-11 3:07
geekgautam23-Jun-11 3:07 
AnswerRe: VB and MS Excel Pin
David Mujica23-Jun-11 3:28
David Mujica23-Jun-11 3:28 
GeneralRe: VB and MS Excel Pin
geekgautam23-Jun-11 18:26
geekgautam23-Jun-11 18:26 
GeneralRefresh Data Pin
David Mujica24-Jun-11 6:25
David Mujica24-Jun-11 6:25 
GeneralRe: Refresh Data Pin
geekgautam24-Jun-11 8:01
geekgautam24-Jun-11 8:01 
AnswerRe: VB and MS Excel Pin
Dalek Dave23-Jun-11 3:39
professionalDalek Dave23-Jun-11 3:39 
GeneralRe: VB and MS Excel Pin
geekgautam23-Jun-11 18:29
geekgautam23-Jun-11 18:29 
GeneralRe: VB and MS Excel Pin
geekgautam4-Jul-11 22:44
geekgautam4-Jul-11 22:44 
AnswerRe: VB and MS Excel Pin
Mycroft Holmes23-Jun-11 22:53
professionalMycroft Holmes23-Jun-11 22:53 
GeneralRe: VB and MS Excel Pin
geekgautam24-Jun-11 8:04
geekgautam24-Jun-11 8:04 
GeneralRe: VB and MS Excel Pin
Dalek Dave26-Jun-11 1:59
professionalDalek Dave26-Jun-11 1:59 
QuestionList(Of Integer) vs. an array (VB.NET 2008) Pin
Steven St. John21-Jun-11 5:45
Steven St. John21-Jun-11 5:45 
AnswerRe: List(Of Integer) vs. an array (VB.NET 2008) => Use List PinPopular
MicroVirus21-Jun-11 7:12
MicroVirus21-Jun-11 7:12 
GeneralRe: List(Of Integer) vs. an array (VB.NET 2008) => Use List Pin
Thomas Krojer21-Jun-11 20:46
Thomas Krojer21-Jun-11 20:46 
GeneralRe: List(Of Integer) vs. an array (VB.NET 2008) => Use List Pin
MicroVirus21-Jun-11 22:06
MicroVirus21-Jun-11 22:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.