8

I would like to use the String method IndexOfAny to check if a character exists in a specified string.

Examples I've found online, of using the IndexOfAny method include a "c" after each character in the character array when using VB.NET. However, when I look at examples of simple character arrays in VB.NET, I dont see any such "c" after each character. What does the "c" do? Is it optional?

Dim s1 As String = "Darth is not my father."
' Find the first index of either "x" or "n"
Dim i1 As Integer = s1.IndexOfAny(New Char() {"x"c, "n"c})
vcsjones
  • 138,677
  • 31
  • 291
  • 286
n00b
  • 4,341
  • 5
  • 31
  • 57

1 Answers1

11

That is a suffix for a literal of type System.Char. So

Dim foo As Char = "x"c

Will compile (when Option Strict is set to either On or Off). Without the c, it would be interpreted as a string. For more information about literal suffixes in VB.NET, take a look at the MSDN page, "Constant and Literal Data Types".

vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • So how come this works (or what is it doing); Dim myArray() As Char = New Char() {"s", "a", "m"} – n00b Oct 22 '13 at 16:05
  • @n00b Because of have [Option Strict](http://stackoverflow.com/questions/2454552/whats-an-option-strict-and-explicit) set to Off. It won't compile if you have it set to On. When option strict is Off, the VB.NET compiler tries to "fix" things so they automagically work. Most VB.NET developers set Option Strict to On, so that's why you see them using it this way in examples. – vcsjones Oct 22 '13 at 16:07