title: vb.net - How to get the day number from a weekday name
body: <h2>Long story short:</h2>
  
  <p>I need the inverted function of <code>WeekdayName()</code>, in the following terms:</p>
  
  <ol>
  <li>You provide a week day <strong>name</strong> (e.g. Sunday)</li>
  <li>You get the correspondent week day <strong>number</strong> (that depends which is the first week day as returned in <code>WeekdayName</code>, e.g., you get 1 for Sunday if Sunday is the first day of the week)</li>
  </ol>
  
  <h2>Background:</h2>
  
  <p>I'm using date functions in vb.net. So far so good. I'm using the function <a href="http://msdn.microsoft.com/en-us/library/t8dc1aee%28v=vs.80%29.aspx" rel="nofollow"><code>WeekdayName()</code></a> in order to set the titles for tab pages in a <code>TabControl</code>. My custom control (kind of calendar control) holds the mentioned <code>TabControl</code> and it has the option of choosing which day of the week you want to diplay.</p>
  
  <p>If you want to display ALL days, the <code>TabControl</code> will have <code>TabPage</code>s filled by iterating upon the <code>WeekdayName()</code> function mentioned above (localized and managed by the system calendar and capitalized by me).</p>
  
  <p>If you set only Tuesday and Friday, you will have two tab pages with those titles and sorted as <code>[Tuesday | Friday]</code>. If you decide to add Wednesday, now you should have 3 tab pages: <code>[Tuesday | Wednesday | Friday]</code>, and so on...</p>
  
  <p>In order to keep tabs sorted, when I need to insert a tab to be shown I want to provide the tab's title to check against the new-to-insert weekday name and in that way have the order resolved.</p>
  
  <p>I believe one workaround is to create a fake date object with the tab's title string in it in order to have a, for instance, "Sunday" date and then use: <a href="http://msdn.microsoft.com/en-us/library/82yfs2zh%28v=vs.80%29.aspx" rel="nofollow"><code>Weekday()</code></a> but I was hoping an API solution.</p>
  
  <p><strong>NOTE:</strong> This is <strong>not</strong>: <em>how to get the day number from a date</em>.</p>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>As we disscused with @jmshapland, a custom code approach using the default localization and system settings could be:</p>
  
  <pre><code>Function WeekdayNumber(day as String)
      ' The following initialization does not necessary go in this function.
      ' The idea is to have a mapping between key day name and index in the
      ' same order as the WeekdayName() method with the default system settings
      Dim weekdayNamesCollection as Collection = New Collection()
      For i = 1 to WEEKDAYS
          Dim wDay as String = WeekdayName(i)
          ' add the day with a key with the same name
          weekdayNamesCollection.add(wDay, wDay)
      Next i
  
      If weekdayNamesCollection.ContainsKey(day) Then
          Return weekdayNamesCollection.IndexOfKey(day) + 1
      End If
  
      ' Raise an error as the string 'day' is not in the collection
  End Sub
  </code></pre>
  
  <p>I didn't test this out as I am in a computer without any framework and/or IDE. But basically, the idea is to return the index where the weekday name is stored in the collection (previously, it was stored in the system settings order as <code>WeekdayName()</code>.</p>