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

4
  • 3
    @Devmonster take a look at this topic. I think you will find it will help you. stackoverflow.com/questions/1133265/… Commented Apr 28, 2012 at 2: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 Commented Apr 28, 2012 at 2: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) Commented Apr 28, 2012 at 2: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. Commented Apr 28, 2012 at 3:13

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
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: stackoverflow.com/questions/226517/….
1

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

Comments

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.

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.