Function LEFT, RIGHT in VB.Net
I cannot use function LEFT, RIGHT in VB.Net. Anyone know what function use instead LEFT, RIGHT
Qualify the calls with the VB class name:
If there is nothing in the global namespace with these names, VB doesn't need them to be qualified, but in a form, for instance, you'll get conflict with the form's 'Left' property.
Those functions were actually VBA functions that were supported in Visual Basic up to version 6. Support for them was dropped in .NET with the class names mentioned by David Anton.
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
Function LEFT, RIGHT in VB.Net
Left and Right functions can be used to truncate strings after a certain number of characters from the left and right of the string, respectively. Each of these functions requires two arguments:
# The string from which the substring should be extracted.
# The length of the string to return.
The 3 VB functions Left, Right and Mid have been combined into a single one in .NET: String.Substring.
It is pretty straighforward to use for Left and Mid, but need some fidling for the equivalent or Right:
Dim x As String = "I am me"
'This is the Left : Result "I"
'This is the Mid : Result "am"
'This is the Right : Result "me"
Debug.WriteLine(x.Substring(x.Length - 2, 2))
Just to make things clear.
When you talk about the Left and Right functions, you are talking of functions in the Microsoft.VisualBasic namespace. This namespace has been designed to simulate the commands of the old Visual Basic commands. You do not have to specify the namespace when you call those functions, because by default, the environment activate an Imports on it (you can see it under the list of references in the project's properties).
You can thus still use the old functions that you were used to if you used to program in VB6, and end up with the same problems, such as the conflict between the Right and Left properties of a form and the Right and Left functions of the Microsoft.VisualBasic namespace.
However, all those functions have been replaced by something else in .NET. An object oriented language does not use functions, it uses methods on the variables. Thus calling Substring on a String object (a method in the String class) is the most appropriate way to go.
Under the hood, it makes no difference. When you use the Right and Left functions, the compiler compiles a call to Substring.
The same thing applies to a lot of things. Instead of calling MsgBox, you might be better calling the MessageBox.Show method.
Using the framework class methods over those in the Microsoft.VisualBasic namespace has 2 advantages.
The most important is that you learn to code in .NET, and will thus be able to more easily understand code written in other langages. By the same token, learning another .NET language such as C# will be a lot easier, since you will continue to use the same methods.
The .NET methods are also usually more versatile, often offering more ways to do the same thing, or parameters that are not available in the Microsoft.VisualBasic functions.
Last Post: 11-27-2001, 07:53 AM
By Tim Manos in forum VB Classic
Last Post: 10-19-2001, 07:06 AM
By chandra in forum VB Classic
Last Post: 06-22-2000, 08:36 AM
By Kunal Sharma in forum VB Classic
Last Post: 04-25-2000, 04:45 PM
By Dan in forum VB Classic
Last Post: 03-17-2000, 06:14 AM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL