none
port vba scriipting.dictionary to visual basic RRS feed

  • Question

  • I can't seem to find a way to store strDayName, intDayOfWeekNumber  for an entire week.

    Set dicDayNum = CreateObject("scripting.Dictionary")
     dicDayNum.Add "sun", 1
     dicDayNum.Add "mon", 2
     dicDayNum.Add "tue", 3
     dicDayNum.Add "wed", 4
     dicDayNum.Add "thu", 5
     dicDayNum.Add "fri", 6
     dicDayNum.Add "sat", 7

    My need for this is determining the 3rd Wednesday of each month.

    Thanks from an old old codger...

    Tuesday, June 12, 2018 9:56 PM

Answers

  • Hi

    Here is code to get the 3rd (or any index) Wednesday(or any dayof the week) for any Month (chosen via  DateTimPicker1)

    The Fuction GetDOW(d As Date, dow As DayOfWeek, q As Integer) takes a starting date (d) and gets the first day of the Month, DayOfWeek needed (dow) and which one of them (q) and returns the date of that choice.

    NOTE: no error checking included (but, the 5th Wednesday of Jun falls on 4th July and doesn't crash)

    ' Form1 with DateTimePicker1,
    ' Label1 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim DayToFind As DayOfWeek = DayOfWeek.Wednesday
      Dim WhichOneOfThem As Integer = 5
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Label1.Text = "The (" & WhichOneOfThem.ToString & ") " & DayToFind.ToString & " of " & DateTimePicker1.Value.ToString("MMMM") & " falls on " & GetDOW(DateTimePicker1.Value, DayToFind, WhichOneOfThem).ToString("dddd, dd MMM yyyy")
      End Sub
      Function GetDOW(d As Date, dow As DayOfWeek, q As Integer) As Date
        Dim d2 As Date = New Date(d.Year, d.Month, 1)
    
        Dim day As Integer = 1
        Dim found As Integer = 0
        Do
          Dim dw As DayOfWeek = d2.DayOfWeek
          If dw = dow Then found += 1
          d2 = d2.AddDays(1)
        Loop Until found = q
        Return d2.AddDays(-1)
      End Function
    End Class


    Regards Les, Livingston, Scotland

    Tuesday, June 12, 2018 11:19 PM

All replies

  • Hi

    Here is a Dictionary called 'scripting' containing the data as you show.

        Dim scripting As New Dictionary(Of String, Integer)
    
        scripting.Add("Sun", 1)
        scripting.Add("Mon", 2)
        scripting.Add("Tue", 3)
        scripting.Add("Wed", 4)
        scripting.Add("Thu", 5)
        scripting.Add("Fri", 6)
        scripting.Add("Sat", 7)
    


    Regards Les, Livingston, Scotland

    Tuesday, June 12, 2018 10:06 PM
  • If you need culture aware you can get a dictionary as follows.

    Dim dict As Dictionary(Of String, Integer) = CultureInfo.
            CurrentCulture.
            DateTimeFormat.
            AbbreviatedDayNames.
            Select(Function(m, i) New With {.Name = m, .Index = i}).
            ToDictionary(Function(x) x.Name, Function(x) x.Index + 1)
    
    For Each kvp As KeyValuePair(Of String, Integer) In dict
        Console.WriteLine($"Name: {kvp.Key} Id: {kvp.Value}")
    Next

    Outputs to Visual Studio output window as follows

    Name: Sun Id: 1
    Name: Mon Id: 2
    Name: Tue Id: 3
    Name: Wed Id: 4
    Name: Thu Id: 5
    Name: Fri Id: 6
    Name: Sat Id: 7

    Or hard coded in the initializer

    Dim days = New Dictionary(Of String, Integer) From
            {
                {"sun", 1}, 
                {"mon", 2}, 
                {"tue", 3}, 
                {"wed", 4}, 
                {"thu", 5}, 
                {"fri", 6}, 
                {"sat", 7}
            }

    Third Wednesday of each month in a year

    Create a new code module as follows

    Public Module Extensions
        <Runtime.CompilerServices.Extension>
        Public Function ThirdWednesday(ByVal pDateTime As Date) As Date
            Dim result As New Date(pDateTime.Year, pDateTime.Month, 15)
    
            Do While result.DayOfWeek <> DayOfWeek.Wednesday
                result = result.AddDays(1)
            Loop
    
            Return result
        End Function
        Public Function ThirdWednesdays() As List(Of Date)
            Return Enumerable.Range(1, 12).Select(Function(index) New Date(Now.Year, index, 1)).ToList()
        End Function
    End Module

    Usage

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each d As Date In ThirdWednesdays()
            Console.WriteLine(d.ThirdWednesday().ToShortDateString())
        Next
    End Sub

    Output

    1/17/2018
    2/21/2018
    3/21/2018
    4/18/2018
    5/16/2018
    6/20/2018
    7/18/2018
    8/15/2018
    9/19/2018
    10/17/2018
    11/21/2018
    12/19/2018
    

    Now we could expand the above to allow for any year.

    Public Module Extensions
        <Runtime.CompilerServices.Extension>
        Public Function ThirdWednesday(ByVal pDateTime As Date) As Date
            Dim result As New Date(pDateTime.Year, pDateTime.Month, 15)
    
            Do While result.DayOfWeek <> DayOfWeek.Wednesday
                result = result.AddDays(1)
            Loop
    
            Return result
        End Function
        Public Function ThirdWednesdays() As List(Of Date)
            Return Enumerable.Range(1, 12).Select(Function(index) New Date(Now.Year, index, 1)).ToList()
        End Function
        Public Function ThirdWednesdays(pYear As Integer) As List(Of Date)
            Return Enumerable.Range(1, 12).Select(Function(index) New Date(pYear, index, 1)).ToList()
        End Function
    End Module
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each d As Date In ThirdWednesdays(2017)
            Console.WriteLine(d.ThirdWednesday().ToShortDateString())
        Next
    End Sub

    1/18/2017
    2/15/2017
    3/15/2017
    4/19/2017
    5/17/2017
    6/21/2017
    7/19/2017
    8/16/2017
    9/20/2017
    10/18/2017
    11/15/2017
    12/20/2017
    



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Tuesday, June 12, 2018 10:43 PM
    Moderator
  • Hi

    Here is code to get the 3rd (or any index) Wednesday(or any dayof the week) for any Month (chosen via  DateTimPicker1)

    The Fuction GetDOW(d As Date, dow As DayOfWeek, q As Integer) takes a starting date (d) and gets the first day of the Month, DayOfWeek needed (dow) and which one of them (q) and returns the date of that choice.

    NOTE: no error checking included (but, the 5th Wednesday of Jun falls on 4th July and doesn't crash)

    ' Form1 with DateTimePicker1,
    ' Label1 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim DayToFind As DayOfWeek = DayOfWeek.Wednesday
      Dim WhichOneOfThem As Integer = 5
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Label1.Text = "The (" & WhichOneOfThem.ToString & ") " & DayToFind.ToString & " of " & DateTimePicker1.Value.ToString("MMMM") & " falls on " & GetDOW(DateTimePicker1.Value, DayToFind, WhichOneOfThem).ToString("dddd, dd MMM yyyy")
      End Sub
      Function GetDOW(d As Date, dow As DayOfWeek, q As Integer) As Date
        Dim d2 As Date = New Date(d.Year, d.Month, 1)
    
        Dim day As Integer = 1
        Dim found As Integer = 0
        Do
          Dim dw As DayOfWeek = d2.DayOfWeek
          If dw = dow Then found += 1
          d2 = d2.AddDays(1)
        Loop Until found = q
        Return d2.AddDays(-1)
      End Function
    End Class


    Regards Les, Livingston, Scotland

    Tuesday, June 12, 2018 11:19 PM
  • If you just need the third Wednesday of the month the following should work:

        Function ThirdWednesdayOfMonth(ByVal inputDate As Date) As Date
    
            Dim startDate As New Date(Year(inputDate), Month(inputDate), 8)
            Dim subtractDate As New Date(Year(inputDate), Month(inputDate), 4)
    
            Dim thirdWednesdayDate As Date = startDate.AddDays(-Weekday(subtractDate)).AddDays(14)
    
            Return thirdWednesdayDate
    
        End Function


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, June 13, 2018 1:58 PM
  • Thank you!  Simple and elegant!

    Marc Miller

    Harveyville, Pennsylvania

    Wednesday, June 13, 2018 2:29 PM