Results 1 to 10 of 10

Thread: Find Max and Min Date in Listbox and Display in Me.Text

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    6

    Question 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:
    1. Dim thedates As List(Of String)
    2.             Dim sel As String
    3.             For Each str As String In LstResults.Items
    4.                 sel &= str & " "
    5.             Next
    6.  
    7.             Dim reg As Regex = New Regex("\d{1,2}\/\d{1,2}\/\d{1,4}")
    8.             Dim matches As MatchCollection = reg.Matches(sel.ToString)
    9.  
    10.  
    11.             'Getting Object Not Set to an Instance Exception
    12.             ' Loop over matches.
    13.             For Each m As Match In matches
    14.                 ' Loop over captures.
    15.                 For Each c As Capture In m.Captures
    16.  
    17.                     '''***BREAK POINT***
    18.                     thedates.Add(c.ToString)
    19.  
    20.                 Next
    21.             Next
    22.  
    23.  
    24.             Dim lstdates As List(Of DateTime) = thedates.[Select](Function(x) DateTime.ParseExact(x, "MM/dd/yyyy", Nothing)).ToList()
    25.  
    26.             Dim minDate As DateTime = lstdates.Min()
    27.  
    28.             Dim maxDate As DateTime = lstdates.Max()
    29.  
    30.             Me.Text = "SomeApp Title" & ":  " & minDate.ToString & " - " & maxDate.ToString

    Much appreciated!

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    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.

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    6

    Resolved Re: Find Max and Min Date in Listbox and Display in Me.Text

    Quote Originally Posted by Sitten Spynne View Post
    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    6

    Re: Find Max and Min Date in Listbox and Display in Me.Text

    Quote Originally Posted by dbasnett View Post
    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:
    1. LstResults.Items.Add("04/10/2018 09:55  12323211    3132132 MRH *INPATIENT* MRA NECK W & WO CONTRAST*HSE*")
    2.         LstResults.Items.Add("04/13/2018 09:55  1241424 1232323 MRH *INPATIENT* MRA NECK W & WO CONTRAST")
    3.         LstResults.Items.Add("04/19/2018 09:55  1231231 1231232 MRH *INPATIENT* MRA NECK W & WO CONTRAST")
    4.  
    5. Try
    6.  
    7.  
    8.             Dim ie As IEnumerable(Of DateTime)
    9.  
    10.             ie = From s In LstResults.Items
    11.                  Let p As String() = s.ToString.Split(" "c)
    12.                  Let d As DateTime =
    13.                      DateTime.Parse(p(0)).Add(TimeSpan.Parse(p(1)))
    14.                  Select d
    15.  
    16.             Dim minDT As DateTime = ie.Min
    17.             Dim maxDT As DateTime = ie.Max
    18.  
    19.             Dim addstr As String = ":  " & minDT.ToString _
    20.                 & " - " & maxDT.ToString
    21.  
    22.             If addstr <> ":  " & " - " Then
    23.                 Me.Text = "AnApp" & ":  " _
    24.                     & minDT.ToString & " - " & maxDT.ToString
    25.             Else
    26.                 Me.Text = "AnApp"
    27.             End If
    28.  
    29.  
    30.  
    31.         Catch ex As Exception
    32.             Me.Text = "LostTranscribe"
    33.             'MsgBox(ex.Message)
    34.         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.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Find Max and Min Date in Listbox and Display in Me.Text

    Quote Originally Posted by norffman View Post
    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:
    1. LstResults.Items.Add("04/10/2018 09:55  12323211    3132132 MRH *INPATIENT* MRA NECK W & WO CONTRAST*HSE*")
    2.         LstResults.Items.Add("04/13/2018 09:55  1241424 1232323 MRH *INPATIENT* MRA NECK W & WO CONTRAST")
    3.         LstResults.Items.Add("04/19/2018 09:55  1231231 1231232 MRH *INPATIENT* MRA NECK W & WO CONTRAST")
    4.  
    5.   Dim ie As IEnumerable(Of DateTime)
    6.  
    7.             ie = From s In LstResults.Items
    8.                  Let p As String() = s.ToString.Split(" "c)
    9.                  Let d As DateTime = DateTime.Parse(p(0)).Add(TimeSpan.Parse(p(1)))
    10.                  Select d
    11.  
    12.             Dim minDT As DateTime = ie.Min
    13.             Dim maxDT As DateTime = ie.Max
    14.  
    15.             Dim addstr As String = ":  " & minDT.ToString & " - " & maxDT.ToString
    16.  
    17.             If addstr <> ":  " & " - " Then
    18.                 Me.Text = "AnApp" & ":  " & minDT.ToString & " - " & maxDT.ToString
    19.             Else
    20.                 Me.Text = "AnAPp"
    21.             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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    6

    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?

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    6

    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:

    Name:  lterror.jpg
Views: 912
Size:  28.4 KB


    vb.net Code:
    1. Dim ie As IEnumerable(Of DateTime)
    2.  
    3.             ie = From s In LstResults.Items
    4.                  Let p As String() = s.ToString.Split(vbTab)
    5.                  Let d As DateTime =
    6.                      DateTime.Parse(p(0)).Add(TimeSpan.Parse(p(1)))
    7.                  Select d
    8.  
    9.             Dim minDT As DateTime = ie.Min
    10.             Dim maxDT As DateTime = ie.Max
    11.  
    12.             Dim addstr As String = ":  " & minDT.ToString _
    13.                 & " - " & maxDT.ToString
    14.  
    15.             If addstr <> ":  " & " - " Then
    16.                 Me.Text = "App" & ":  " _
    17.                     & minDT.ToString & " - " & maxDT.ToString
    18.             Else
    19.                 Me.Text = "App"
    20.             End If
    Last edited by norffman; Apr 20th, 2018 at 01:47 PM. Reason: Getting Incorrect Dates when Delimit by VbTab

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    6

    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:
    1. Try
    2.             Dim theDates As New List(Of String)()
    3.             Dim sel As String
    4.             For Each str As String In LstResults.Items
    5.                 sel &= str & " "
    6.             Next
    7.  
    8.             Dim reg As Regex = New Regex("\d{1,2}\/\d{1,2}\/\d{1,4}")
    9.             Dim matches As MatchCollection = reg.Matches(sel.ToString)
    10.  
    11.             Dim lstdates As New List(Of DateTime)()
    12.  
    13.  
    14.             For Each m As Match In matches
    15.                 For Each c As Capture In m.Captures
    16.  
    17.                     lstdates.Add(DateTime.Parse(c.ToString))
    18.  
    19.                 Next
    20.             Next
    21.  
    22.             Dim addstr As String = ":  " _
    23.                 & lstdates.Min().ToString("MM/dd/yyyy") _
    24.                 & " - " & lstdates.Max().ToString("MM/dd/yyyy")
    25.  
    26.             If addstr <> ":  " & " - " Then
    27.                 Me.Text = "SomeApp" & addstr
    28.             Else
    29.                 Me.Text = "SomeApp"
    30.             End If
    31.         Catch ex As Exception
    32.             Me.Text = "SomeApp"
    33.         End Try
    Last edited by norffman; Apr 20th, 2018 at 02:37 PM. Reason: Cleaned up code presentation.

  10. #10
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    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
  •  



Click Here to Expand Forum to Full Width