-
Apr 20th, 2018, 11:37 AM
#1
Thread Starter
New Member
Find Max and Min Date in Listbox and Display in Me.Text
Trying to take a group of listbox strings and perform a regex to find all dates, then find the maximum and minimum date values. After that, want to harvest the results and place them into the form title, me.text. I think I'm fairly close, but for whatever reason, when I try to add the matches to the list of string, get a failure relating to no instance of list.
Sample of results in listbox where values need to be extracted:
04/18/2018 11:12 0000000 4444444 XR XR SPINE LUMBAR AP & LAT
04/18/2018 13:00 1111111 1111111 XR C-ARM<1 HR W IMAGES
04/19/2018 17:34 2222222 3333333 XR XR PICC LINE INSERTION PNL
vb.net Code:
Dim thedates As List(Of String)
Dim sel As String
For Each str As String In LstResults.Items
sel &= str & " "
Next
Dim reg As Regex = New Regex("\d{1,2}\/\d{1,2}\/\d{1,4}")
Dim matches As MatchCollection = reg.Matches(sel.ToString)
'Getting Object Not Set to an Instance Exception
' Loop over matches.
For Each m As Match In matches
' Loop over captures.
For Each c As Capture In m.Captures
'''***BREAK POINT***
thedates.Add(c.ToString)
Next
Next
Dim lstdates As List(Of DateTime) = thedates.[Select](Function(x) DateTime.ParseExact(x, "MM/dd/yyyy", Nothing)).ToList()
Dim minDate As DateTime = lstdates.Min()
Dim maxDate As DateTime = lstdates.Max()
Me.Text = "SomeApp Title" & ": " & minDate.ToString & " - " & maxDate.ToString
Much appreciated!
-
Apr 20th, 2018, 12:07 PM
#2
Re: Find Max and Min Date in Listbox and Display in Me.Text
I give this speech a lot, everyone has to learn it.
The "for whatever reason" you get an error about 'no instance of list' is because you aren't creating an instance of a list. The best way to think about this is by analogy.
Laptops come in boxes. Without doing a little bit of work, you can't tell an empty laptop box from one that has a laptop inside, can you? If I show you two pictures, you'll probably not have enough information to tell. Now, suppose I give you an empty laptop box and say, "Turn on the laptop." You'll look at me funny, right? There's no actual laptop in the box, so you can't turn it on.
This is what's happening. You asked for an empty box, but never put a thing inside of it. Then you asked VB to do something with the thing inside the box, but the box is empty so VB can't.
Code:
Dim thedates as List(Of String)
That line of code says:
Please get an empty box that can hold a List(Of String) for me. I want to call this box 'thedates'.
An empty box isn't good for doing anything. You need to put an instance of List(Of String) inside of it. To do that, you use the New keyword. VB gives you two different ways:
Code:
Dim theDates As New List(Of String)()
-or-
Dim theDates As List(Of String) = New List(Of String)()
The second way exists in case you want to create the variable without creating the thing inside of it right away. Sometimes having an empty box that you will later put a thing inside is useful. Other times you need to grab a box because you know you'll create the thing to put in it later. Anyway, that line says:
Please get a box that can hold a List(Of String) and a new List(Of String) for me. Place the List(Of String) inside the box. I'd like to call the box 'theDates'.
So that's the problem: you created a variable but never assigned a value to it, and that results in a NullReferenceException for reference types.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Apr 20th, 2018, 12:16 PM
#3
Re: Find Max and Min Date in Listbox and Display in Me.Text
How about
Code:
Dim ie As IEnumerable(Of DateTime)
ie = From s In LstResults.Items
Let p As String() = s.ToString.Split(" "c)
Let d As DateTime = DateTime.Parse(p(0)).Add(TimeSpan.Parse(p(1)))
Select d
Dim minDT As DateTime = ie.Min
Dim maxDT As DateTime = ie.Max
-
Apr 20th, 2018, 12:37 PM
#4
Thread Starter
New Member
Re: Find Max and Min Date in Listbox and Display in Me.Text
Originally Posted by Sitten Spynne
I give this speech a lot, everyone has to learn it.
The "for whatever reason" you get an error about 'no instance of list' is because you aren't creating an instance of a list. The best way to think about this is by analogy.
Laptops come in boxes. Without doing a little bit of work, you can't tell an empty laptop box from one that has a laptop inside, can you? If I show you two pictures, you'll probably not have enough information to tell. Now, suppose I give you an empty laptop box and say, "Turn on the laptop." You'll look at me funny, right? There's no actual laptop in the box, so you can't turn it on.
This is what's happening. You asked for an empty box, but never put a thing inside of it. Then you asked VB to do something with the thing inside the box, but the box is empty so VB can't.
Code:
Dim thedates as List(Of String)
That line of code says:
An empty box isn't good for doing anything. You need to put an instance of List(Of String) inside of it. To do that, you use the New keyword. VB gives you two different ways:
Code:
Dim theDates As New List(Of String)()
-or-
Dim theDates As List(Of String) = New List(Of String)()
The second way exists in case you want to create the variable without creating the thing inside of it right away. Sometimes having an empty box that you will later put a thing inside is useful. Other times you need to grab a box because you know you'll create the thing to put in it later. Anyway, that line says:
So that's the problem: you created a variable but never assigned a value to it, and that results in a NullReferenceException for reference types.
Thank you for explaining, that was very helpful. I've resolved this problem in the past and totally forgot about that. Thank you for the explanation as to why it must be so.
-
Apr 20th, 2018, 12:39 PM
#5
Thread Starter
New Member
Re: Find Max and Min Date in Listbox and Display in Me.Text
Originally Posted by dbasnett
How about
Code:
Dim ie As IEnumerable(Of DateTime)
ie = From s In LstResults.Items
Let p As String() = s.ToString.Split(" "c)
Let d As DateTime = DateTime.Parse(p(0)).Add(TimeSpan.Parse(p(1)))
Select d
Dim minDT As DateTime = ie.Min
Dim maxDT As DateTime = ie.Max
I tried your code here, but when there is data to the right of the actual date, this will not return the dates properly.
For instance if you add the three following entries, the sort and find min max doesn't return a match:
vb.net Code:
LstResults.Items.Add("04/10/2018 09:55 12323211 3132132 MRH *INPATIENT* MRA NECK W & WO CONTRAST*HSE*") LstResults.Items.Add("04/13/2018 09:55 1241424 1232323 MRH *INPATIENT* MRA NECK W & WO CONTRAST") LstResults.Items.Add("04/19/2018 09:55 1231231 1231232 MRH *INPATIENT* MRA NECK W & WO CONTRAST") Try Dim ie As IEnumerable(Of DateTime) ie = From s In LstResults.Items Let p As String() = s.ToString.Split(" "c) Let d As DateTime = DateTime.Parse(p(0)).Add(TimeSpan.Parse(p(1))) Select d Dim minDT As DateTime = ie.Min Dim maxDT As DateTime = ie.Max Dim addstr As String = ": " & minDT.ToString _ & " - " & maxDT.ToString If addstr <> ": " & " - " Then Me.Text = "AnApp" & ": " _ & minDT.ToString & " - " & maxDT.ToString Else Me.Text = "AnApp" End If Catch ex As Exception Me.Text = "LostTranscribe" 'MsgBox(ex.Message) End Try
How can I strip out the information from the listbox entry prior to evaluating with your suggestion? Sorry, I can't completely make sense of the code, and it does work for just date entries, but doesn't when there is anything additional in the line item.
Thank you!
Last edited by norffman; Apr 20th, 2018 at 01:18 PM.
Reason: Not working with anything in LstBox that isn't dates.
-
Apr 20th, 2018, 01:18 PM
#6
Re: Find Max and Min Date in Listbox and Display in Me.Text
Originally Posted by norffman
I tried your code here, but when there is data to the right of the actual date, this will not return the dates properly.
For instance if you add the three following entries, the sort and find min max doesn't return a match:
vb.net Code:
LstResults.Items.Add("04/10/2018 09:55 12323211 3132132 MRH *INPATIENT* MRA NECK W & WO CONTRAST*HSE*") LstResults.Items.Add("04/13/2018 09:55 1241424 1232323 MRH *INPATIENT* MRA NECK W & WO CONTRAST") LstResults.Items.Add("04/19/2018 09:55 1231231 1231232 MRH *INPATIENT* MRA NECK W & WO CONTRAST") Dim ie As IEnumerable(Of DateTime) ie = From s In LstResults.Items Let p As String() = s.ToString.Split(" "c) Let d As DateTime = DateTime.Parse(p(0)).Add(TimeSpan.Parse(p(1))) Select d Dim minDT As DateTime = ie.Min Dim maxDT As DateTime = ie.Max Dim addstr As String = ": " & minDT.ToString & " - " & maxDT.ToString If addstr <> ": " & " - " Then Me.Text = "AnApp" & ": " & minDT.ToString & " - " & maxDT.ToString Else Me.Text = "AnAPp" End If
How can I strip out the information from the listbox entry prior to evaluating with your suggestion? Sorry, I can't completely make sense of the code, and it does work for just date entries, but doesn't when there is anything additional in the line item.
Thank you!
What? This worked.
Code:
LstResults.Items.Add("04/10/2018 09:55 1384290 4205234 MRH *INPATIENT* MRA NECK W & WO CONTRAST*HSE*")
LstResults.Items.Add("04/13/2018 09:55 1384290 4205234 MRH *INPATIENT* MRA NECK W & WO CONTRAST*HSE*")
LstResults.Items.Add("04/19/2018 09:55 1384290 4205234 MRH *INPATIENT* MRA NECK W & WO CONTRAST*HSE*")
Dim ie As IEnumerable(Of DateTime)
ie = From s In LstResults.Items
Let p As String() = s.ToString.Split(" "c)
Let d As DateTime = DateTime.Parse(p(0)).Add(TimeSpan.Parse(p(1)))
Select d
Dim minDT As DateTime = ie.Min
Dim maxDT As DateTime = ie.Max
Debug.WriteLine(minDT)
Debug.WriteLine(maxDT)
So I don't know what you mean. If you are changing the problem, "find the maximum and minimum date values", then explain.
Last edited by dbasnett; Apr 20th, 2018 at 01:21 PM.
-
Apr 20th, 2018, 01:27 PM
#7
Thread Starter
New Member
Re: Find Max and Min Date in Listbox and Display in Me.Text
I am trying to accomplish the original task, but when the data that I am working with is returned to the listbox, the date is to the left of information that is not a date to the right, each piece of data in the string, that represents each item in the listbox, is separated by a VbTab.
The issue I'm running into is when I use your code, and there is only a date in the listbox, with none of my other information appearing to the right of the date, everything is fine and it works great, but when there is other data in the item string for each the items in the listbox, the code doesn't evaluate the date that precedes the data in the item.
Does that make sense?
-
Apr 20th, 2018, 01:30 PM
#8
Thread Starter
New Member
Re: Find Max and Min Date in Listbox and Display in Me.Text
That I just needed to delimit by VbTab, but still not working properly... my dates are showing up totally wrong:
vb.net Code:
Dim ie As IEnumerable(Of DateTime) ie = From s In LstResults.Items Let p As String() = s.ToString.Split(vbTab) Let d As DateTime = DateTime.Parse(p(0)).Add(TimeSpan.Parse(p(1))) Select d Dim minDT As DateTime = ie.Min Dim maxDT As DateTime = ie.Max Dim addstr As String = ": " & minDT.ToString _ & " - " & maxDT.ToString If addstr <> ": " & " - " Then Me.Text = "App" & ": " _ & minDT.ToString & " - " & maxDT.ToString Else Me.Text = "App" End If
Last edited by norffman; Apr 20th, 2018 at 01:47 PM.
Reason: Getting Incorrect Dates when Delimit by VbTab
-
Apr 20th, 2018, 02:36 PM
#9
Thread Starter
New Member
Re: Find Max and Min Date in Listbox and Display in Me.Text
Got it figured out, and thank you both for all your help!
This works as I need it:
vb.net Code:
Try Dim theDates As New List(Of String)() Dim sel As String For Each str As String In LstResults.Items sel &= str & " " Next Dim reg As Regex = New Regex("\d{1,2}\/\d{1,2}\/\d{1,4}") Dim matches As MatchCollection = reg.Matches(sel.ToString) Dim lstdates As New List(Of DateTime)() For Each m As Match In matches For Each c As Capture In m.Captures lstdates.Add(DateTime.Parse(c.ToString)) Next Next Dim addstr As String = ": " _ & lstdates.Min().ToString("MM/dd/yyyy") _ & " - " & lstdates.Max().ToString("MM/dd/yyyy") If addstr <> ": " & " - " Then Me.Text = "SomeApp" & addstr Else Me.Text = "SomeApp" End If Catch ex As Exception Me.Text = "SomeApp" End Try
Last edited by norffman; Apr 20th, 2018 at 02:37 PM.
Reason: Cleaned up code presentation.
-
Apr 20th, 2018, 04:49 PM
#10
Re: Find Max and Min Date in Listbox and Display in Me.Text
Personally, I'd use a ListView for columnar data.
Then each "row" is a ListViewItem with sub-items for each column, so you don't really have to split or do other funky things to get one column's value.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|