(Re-edit to correct my previous error of using VBA instead of VB.net)
Yes. Noting that you only have 5 Char in your array (specialcharacters()
) I assume you only want to mark Monday to Friday. However, the answer to this question can be extended to cover all week. Using your original code as the base:
Dim txtRandomCharacter As Char = ""
Dim specialcharacters() As Char = "★☆☽☾☁"
Dim today As Date = Date.Today
Dim dayIndex As Integer = today.DayOfWeek
If (dayIndex - 1) <= UBound(specialcharacters) Then
txtRandomCharacter = specialcharacters(dayIndex - 1)
End If
However, note the mental gymnastics required in dealing with 0-based arrays.
Dim dayIndex As Integer = Weekday(today,vbMonday)
is also valid code.
Explanation for Weekday
can be found at http://www.excelfunctions.net/vba-weekday-function.html.
specialcharacters
is an array, not a string, so that you can access the array element directly. I have used the UBound
function so that you don't accidently get an array out of bounds error by calling a subscript (dayIndex
) that is higher than your array is long.
Another option is to use the Mid
or Substr
function with strings. In this example I have also concatenated some code for brevity.
Dim txtRandomCharacter As String = ""
Const specialcharacters As String = "★☆☽☾☁"
Dim dayIndex As Integer = Date.Today.DayOfWeek ' - DayOfWeek.vbMonday + 1
txtRandomCharacter = If(dayIndex >= 0 And dayIndex <= Len(specialcharacters), specialcharacters.Substring(dayIndex - 1, 1), "X")
The 'X' option in the IIF
statement was my addition for testing. You can also use "". Unfortunately, the Substr
function in VB.Net is a little less tolerant than the Mid function, hence the additional checks on valid values for dayIndex
. And Substr
is 0-based.
Using the Mid
function, which is tolerant of indexes > length of the string but must be > 0 (hence the If statement):
Dim txtRandomCharacter As String = ""
Const specialcharacters As String = "★☆☽☾☁"
Dim dayIndex As Integer = Date.Today.DayOfWeek ' - DayOfWeek.vbMonday + 1
txtRandomCharacter = Mid(specialcharacters, If(dayIndex > 0, dayIndex, Len(specialcharacters) + 1), 1)
Check your Option Base
to see if your arrays start by default at 0 or 1. specialcharacters()
could be ranging from 0-4 or 1-5 depending on this setting - which means is may or may not align with the Weekday
function which is always in the range 1-7 (or the DayofWeek
function which ranges from 0 to 6).
The key point is to understand the difference between a string and an array of characters.
- With an array of characters - use array subscripts to select the right member. Check to ensure you are not passing an index that is out of bounds for the array.
- With a string, use a
Mid
or Substr
function to get the character from the string. Check to ensure you are not passing an index that is out of bounds.
The other point to recognise is to understand how the days of the week are enumerated. The MSDN reference site only notes the enumeration and does not provide an equivalent integer - the IDE provides some more information. This is important to understand if your counting starts from 0
or 1
, and whether you must adjust your index to address this.