1

I have to port some old VB code (cannot post for security reasons) over to a C# .Net Core application and I added the Microsoft.VisualBasic to my using's but I still get errors saying Cannot resolve symbol Asc/Mid/Chr/Len

Are these methods not available in .net core?

I have read that aside from using the Microsoft.VisualBasic package there really isn't a replacement for these methods.

Do I have any options here?

tyczj
  • 71,600
  • 54
  • 194
  • 296

5 Answers5

4

Try using the methods provided to you by the framework.

Asc to Convert.ToInt32

Chr to Convert.ToChar

Len to String.Length

Mid to String.Substring

Picardia
  • 171
  • 1
  • 8
2

That stuff is all VB-specific, so not part of .NET Core. Don't use it in VB either. C# developers never use them anyway. Use the .NET equivalents, e.g. Convert.ToChar and String.Substring.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Tried using those and they were not working as expected – tyczj Aug 17 '19 at 05:32
  • What "didn't work" about them? There's slight differences that you must account for, for example [`Convert.ToInt32(string)`](https://github.com/microsoft/referencesource/blob/e56279724ddb2715ab3c18c47ecd3b78eb90d3c7/mscorlib/system/convert.cs#L1118) is expecting the entire string to be a number, whereas `Asc(string)` [only looks at the first character](https://github.com/microsoft/referencesource/blob/e56279724ddb2715ab3c18c47ecd3b78eb90d3c7/Microsoft.VisualBasic/runtime/msvbalib/Strings.vb#L243) ... (cont'd) – pinkfloydx33 Aug 17 '19 at 09:00
  • 1
    ... `Asc` also does some calculations for multi byte chars.. you probably really need `Convert.ToInt32(char)` or just `(int)("Apple"[0])`. Also, `Mid` is [1-based](https://github.com/microsoft/referencesource/blob/e56279724ddb2715ab3c18c47ecd3b78eb90d3c7/Microsoft.VisualBasic/runtime/msvbalib/Strings.vb#L2004) whereas `Substring` is 0-based. – pinkfloydx33 Aug 17 '19 at 09:00
2

The Visual Basic Runtime will be shipped with .NET core 3, so those functions will work then.

You can verify this by trying it in preview SDK: the following worked as expected for me (using Microsoft.VisualBasic 10.3.0 package on Nuget)

using static Microsoft.VisualBasic.Strings;

class Program { 
   static void Main() { 
      Asc("Apple");
      Mid("Apple", 2, 3);
      Chr(100);
      Len("Apple");
   }
}
Jimmy
  • 89,068
  • 17
  • 119
  • 137
1

I have tried it now (15 June 2020) and the methods are available. 1) Add a reference to Microsoft.VisualBasic, version 10.4.0-preview.18571.3. In 10.3.0 (the latest stable as of now) the methods are missing 2) Use netstandard2.0 or netstandard2.1

mihails.kuzmins
  • 1,140
  • 1
  • 11
  • 19
0

You need to add a reference to the Microsoft.Visualbasic.dll, not just specifying the using. See the following post: [How to add reference to Microsoft.VisualBasic.dll?

Destek
  • 154
  • 1
  • 10
  • Shouldn't the dll get added automatically when installing the nuget package? I dont think I have ever had to manually add a dll after installing something from nuget – tyczj Aug 17 '19 at 02:29
  • Try adding it manually anyway (takes about 5 seconds and can easily be undone) and see if it works afterwards - otherwise I am just wrong about the possible solution. It's been my experience that it might miss a dependency. – Destek Aug 17 '19 at 14:31