Char Array to String in VB.NET 2.0

Written by Zealus on April 22, 2009 – 3:29 pm -


You will be surprised, but the obvious

Dim arrChars(10) As Char
Dim strString As String
strString = "ABCDEFGHIK"
arrChars = strString.ToCharArray
strString = arrChars.ToString

Will not yield the same string as there was before. It will, rather, return “Char[]” response. To get your string back from Char Array (at least in VB.NET 2.0) you will need to call CStr on array:

Dim arrChars(10) As Char
Dim strString As String
strString = "ABCDEFGHIK"
arrChars = strString.ToCharArray
strString = Cstr(arrChars)

Funny, isn’t it? And totally counter-intuitive :)


Tags: , , ,
Posted in annoyances, programming | 2 Comments »

2 Comments to “Char Array to String in VB.NET 2.0”

  1. Paul Steinberg Says:

    This unexplained behavior may be due to the different way in which C and C++ handle string arrays.

  2. Zealus Says:

    What does it have to do with VB.NET?