Jonesopolis Jonesopolis - 1 year ago 197 Question

C# to VB - TrimStart Behavior

I have the simplest VB code:

Dim test As String = "DDN8057"

gives me


Why? Converting this to C# (which I'm far more familiar with), made me realize that
actually expects a
params char[]
, but running


gives me my expected


So, I guess VB is capable of treating a string as a char array internally (is this true?), but why the discrepancy in my output?

Answer Source

You do not have Option Strict switched On in your VB project.

I can tell because test.TrimStart("DDN") does not compile when this is on. This is because as you correctly pointed out TrimStart expects an explicit char array (or a single char)

What happens when you run this with Option Strict Off is the compiler coerces the String (DDN) into a single char (D) (this is an implicit narrowing conversion which Option Strict expressly forbids) which is why you get N8057 as your output.

You would think that as a string is just a char array it would convert it to an array but it doesn't - it effectively performs CChar("DDN")!


Option Strict On = Good. Here is how to switch it on by default: Option Strict on by default in VB.NET

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download