VB.NET – Char from String with Option Strict

G04B0079 So here’s the question:

I’m using String.Split() and need to pass in a Char or a Char array as the parameter.  If I pass in a string String.Split(“/”) I get an error “Option Strict On disallows implicit conversions from ‘String’ to ‘Char’.”

Obviously, the easiest way to fix this would be to turn off Option Strict, but I would prefer to keep it on.  So how do I pass in the Char instead of a String in this situation?”


There are actually several ways to accomplish what you are trying to do.

The first and most general solution would be to call the ToCharArray() method off the string.

Dim strSplit() As String = myString.Split("/".ToCharArray())

The advantage to this method is that it will work regardless of what size the string is and it will use each character in the string as a delimiter.

But what if you only have one character in your array?  Surely there is a shorter, cleaner statement we can use.

As a matter of fact, there are several other options.  You could use Convert.ToChar() or Char.Parse()


Dim strSplit() As String = _
   myString.Split(Convert.ToChar("/"))

or

Dim strSplit() As String = _
   myString.Split(Char.Parse("/"))

But the easiest way to convert a single character string to a Char is simply to put a “c” after the closing quote:

Dim strSplit() As String = myString.Split("/"c)


Other Related Items:

Landing Page Optimization: The Definitive Guide to Testing and Tuning for ConversionsLanding Page Optimization: The Definitive Guide to Testing and Tuning for ConversionsHow much money are you losing because of poor landing page design? In this comprehensive, step-by-step guide, you’ll learn all the skills necessa... Read More >
The Sound of MusicThe Sound of MusicThe Sound of Music is a Broadway musical and movie based on the book The Von Trapp Family Singers by Maria von Trapp. It contains many hit songs, incl... Read More >
Frank Patterson - Ireland In SongFrank Patterson - Ireland In SongNo Description Available.
Genre: Performing Arts - Concerts
Rating: NR
Release Date: 28-FEB-2006
Media Type: DVD

Related Post

One Response to “VB.NET – Char from String with Option Strict”

DotNetNuke Sponsor

 

Most Valuable Blogger
Sponsor