-1

i have a string containing special characters ('★☆☽☾☁') and i would like to have ★ printed out for monday, ☆ for tuesday, ☽ for wednesday, ☾ for thursday, and ☁ for friday. i apologize since i am very new to vb.net so i have only very basic knowledge about it. i have already tried this:

Dim today As Date = Date.Today
    Dim dayIndex As Integer = today.DayOfWeek
    Dim specialcharacters() As Char = "★☆☽☾☁"
    If dayIndex < DayOfWeek.Monday Then
        txtRandomCharacter.Text = specialcharacters
    End If

i would be extremely grateful if anyone could help, thank you!

rx_
  • 1
  • You can use `Select Case` and use `txtRandomCharacter.Text = specialcharacters(n)` to print character based from cases (n = character index from 0) – Tetsuya Yamamoto Jan 18 '18 at 06:05

2 Answers2

0

(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.

AJD
  • 2,400
  • 2
  • 12
  • 22
  • I get a compile error with Option Strict on Dim specialcharacters() As Char = {"★","☆","☽","☾","☁"} The OP's original string with .ToCharArray will suffice. Yes, DayOfWeek is an enumeration but all enums are numbered either by their creator of .net. The days of week are Sunday 0, Monday 1 etc, Since this vb.net we are working in, please no vba or vb6 functions. There are very good methods available in the String class to replace mid – Mary Jan 18 '18 at 08:49
  • @Mary : Good pick up, I guess this is what can happen when answering a VBA question then following up with a VB.Net question :-). – AJD Jan 18 '18 at 18:51
0

How's this?

Const specialcharacters() As Char = "★☆☽☾☁"
Dim today As Date = Date.Today
Dim dayIndex As Integer = today.DayOfWeek 

If dayIndex >= DayOfWeek.Monday andalso dayIndex <= DayOfWeek.Friday Then
    txtRandomCharacter.Text = specialcharacters(dayIndex - dayOfWeek.Monday)
End If

It works because the value of DayOfWeek.Monday through DayOfWeek.Friday are sequential.

Darryl
  • 5,907
  • 1
  • 25
  • 36