2

I'm using Option Strict On (and sometimes wishing I wasn't!) but have a piece of code that works as I want it to without it but not with it.

It should be quite simple I think but I couldn't find an answer here.

My code that works with Option Strict Off is this:

If returnedString.Contains(".exe ") And returnvalues.Count = 0 Then
  Dim x As Integer = 0
  For Each entry In returnedString.Split(".exe ")
    If (entry.Length > 0) And x = 0 Then
      returnvalues.Add(entry & ".exe")
      x = x + 1
    End If
  Next
End If

The returnedString is, for example:

C:\Program Files (x86)\Whatever\Whatever.exe

and

C:\Program Files (x86)\Whatever\Whatever

is returned in entry if Option Strict is off, which is what I want.

However, if I use Visual Studio's suggestion of adding a cast, the following does not work:

For Each entry As String In returnedString.Split(CType(".exe ", Char()))

The first entry returned is C:\Program and this is presumably because it finds the Char ' '; I don't want it to check per character, I want it to check the whole string like it does when Option Strict is off but I can't work it out.

I tried .ToCharArray but that really does the same thing.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
shakydd
  • 61
  • 1
  • 8
  • There is an extra space after Contains(".exe ") is it really required or is a typo? – Steve May 10 '20 at 12:40
  • Yeah, your code was working by accident. It was not doing what you thought it was and it was pure luck that it didn;t matter in this specific case. What is it that you are actually trying to achieve because that code looks bizarre and I am extremely confident that there's a better way to do whatever it is that you're trying to do? Why would you use `Split` to remove any characters from the end of a `String`, let alone the extension from a file path? – jmcilhinney May 10 '20 at 13:19
  • With regards to `Option Strict On`, all it really does is force you to use values of the type that your code expects. If you even get an error relating to `Option Strict`, just look at what type your code expects and what type you're using. In this case, if you'd read the documentation for the `Split` overload you're using then you would have seen that it expects a `Char`, not a `String`. If you'd read further to see what overloads were available, you'd have seen that there were other overloads and which ones do accept `String` delimiters. – jmcilhinney May 10 '20 at 13:23

1 Answers1

3

Please continue to use Option Strict On. It is annoying but it will save your day a lot.

For your problem:

It is caused by the fact that when you enable Option Strict On, the compiler is no longer allowed to take the first char from your string and use it as separator. Because there is no overload for string.Split that takes just a string, then it complains about an attempt to do an invalid conversion.

If you want to use a string as separator then it is required to pass an array of strings as the first parameter, and a second parameter of type StringSplitOptions is required.

Fixing it is really simple. Just change the line to:

For Each entry In returnedString.Split({".exe"}, StringSplitOptions.None)
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thank you so much for all of your comments and Steve, thank you very very much as your answer worked! The space was deliberate - what I'm trying to do (actually quite successfully) is to pull back registry entries that pertain to different bits of file data. So in this case, for example, it was looking at a Run value in HKLM\SW\MS\Win\CV\Run - trouble is, on different computers, this can be terribly formatted to bring back in a consistent way. – shakydd May 10 '20 at 13:55
  • The item it is returning here is: "C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IMSS\PIconStartup.exe" "C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IMSS\PrivacyIconClient.exe" 60 - so you hopefully see why I needed the split rather than getting the file commands first. Again, I really appreciate all your help. – shakydd May 10 '20 at 13:56