3

I'm a long time VB.NET developer and has recently switched to C#. I found out that some of the built-in VB.NET functions (which predates .NET back to 6.0 and BASIC itself) such as the String.Left, or Right, or advanced functions like saving to the registry (SaveSettings and GetSettings) are noticeably absent.

What I did was create a new project in the same solution with VB.NET as its language and recreate basically all the functions I need that are available in VB.NET. And then I just call that to the C# code I'm writing.

Since compiling the code in .NET pretty much boils down to the same CIL, it shouldn't matter performance-wise what language I wrote the code in or whether I mix C# with VB.

Am I wrong or right?

Thanks

apdevelop
  • 598
  • 6
  • 19
Devmonster
  • 699
  • 1
  • 10
  • 28
  • 3
    @Devmonster take a look at this topic. I think you will find it will help you. http://stackoverflow.com/questions/1133265/why-arent-more-applications-written-in-multiple-languages/1194192#1194192 – David Anderson Apr 28 '12 at 02:34
  • @David This is an awesome read. thanks! I realize that you need to have resources for both c# and vb.net if someone else has to fix my code. Although i'm thinking of basic functions like the ones I've mentioned shouldn't be a challenge for a seasoned developer – Devmonster Apr 28 '12 at 02:57
  • @MitchWheat That's the ones I want to use, instead of writing my own C# code (that may be inefficient for all I know) – Devmonster Apr 28 '12 at 02:58
  • @Devmonster I understand. The main ideal is just that it is taxing managing multiple languages in project, whether it be on your own or with others. I'm glad you enjoyed the read and hope it helped. – David Anderson Apr 28 '12 at 03:13

4 Answers4

12

There is a namespace named Microsoft.VisualBasic, you can use it in C# projects also:

string test = Microsoft.VisualBasic.Strings.Left("abc", 2);

Don't forget to add Microsoft.VisualBasic into References of your project.

Hailei
  • 42,163
  • 6
  • 44
  • 69
  • This also crossed my mind, since almost all functions in VB6 are in this namespace, it makes sense to use this. However, I guess I want to keep my C# code all C#, and just call objects in other projects such as VB, to keep it clean. Thanks for the suggestions, though – Devmonster Apr 28 '12 at 03:00
  • 2
    IMO using `Microsoft.VisualBasic` namespace in C# has same or better readability and maintainability than mixing VB.NET and C# in your whole project. C# developers are able to easily understand what these methods are doing by MSDN, but not likely to read VB.NET module as fast. Besides, this assembly is a part of the whole .NET Framework rather than a part of VB.NET. Please refer to: http://stackoverflow.com/questions/226517/is-the-microsoft-visualbasic-namespace-true-net-code. – Hailei Apr 28 '12 at 04:02
1

I'd recommend building it, then using reflector/ilspy/whatever to decompile back to C# :)

James Manning
  • 13,429
  • 2
  • 40
  • 64
0

In my experience, it's been almost entirely a matter of preference and experience one has with a language. You're right to guess that it doesn't matter performance-wise -- both languages are built with the purpose of generating IL and to target the CLR.

There are other debatable considerations to make when choosing a language as some features that are fully supported in one language are not supported by another. Also, some languages are just more expressive for certain purposes such as APL for mathematical calculations.

phixed
  • 478
  • 2
  • 7
0

While using the Microsoft.VisualBasic namespace is a good solution, VB.NET has the equivalent functionality (although the string functions are zero-based rather than 1-based), e.g.

        string str = "abcdefg";

        string left  = str.Substring(0, 2);                // Left(str, 2)
        string right = str.Substring(str.Length - 2, 2);   // Right(str, 2)

BTW, the registry access methods are in the Microsoft.Win32 Namespace: see RegistryKey etc.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • One vb.net "string function" which is not usable in C# is the use of `mid` to the left half of `=`. For example, one can say `mid(myString,3,2)="OK"` to change the third and fourth characters of a string. The actual behavior is essentially `myString = myString.SubString(0,2) & "OK" & myString.SubsString(4)` but it's easier to read. – supercat Nov 17 '12 at 19:29