vb.net & Left function

Posted on 2006-05-04
Medium Priority
Last Modified: 2011-08-18
In some strange way i'm not able to use the vb left function in my application.

  dim test as string = left("TEST",1)

Results in an error:
Public Property Left() As Integer' has no parameters and its return type cannot be indexed.
right clicking the function, go to definition: opens the object browser and takes me to System.Windows.Forms.Control.Left

Replacing left with its full path (Microsoft.VisualBasic.Strings.left) works fine ...???
Question by:SjoerdvW
LVL 25

Assisted Solution

SStory earned 75 total points
ID: 16606128
Well, there is a left property on a form. So the default is Me.Left which is the left property of the form.

I suggest just using something like:

Dim s As String = "TEST"
Dim x As String = s.Substring(1, 1)

I imagine that "TEST" would probably come from a variable anyway--otherwise, just put a T in it to start with, right?



Author Comment

ID: 16606203
Yep. TEST is comming from a variable.

First of all: I should use Substring(0,1) to get the first char.
Scnd: What if the variable is empty? Substring would result in an error......
LVL 55

Accepted Solution

Jaime Olivares earned 75 total points
ID: 16608024
According to .net documentation, if you are using forms, you must fully qualify Microsoft.VisualBasic.Left function, that is:

Dim x As String = Microsoft.VisualBasic.Left(s,1)

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 55

Expert Comment

by:Jaime Olivares
ID: 16608037
if the string is empty (s=""), Left will return an empty string ("")
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16608173
If you want the FIRST letter with Substring() then you start at 0.  You can check the length of the string before attempting to parser it:

        Dim s As String = "TEST"
        If s.Length >= 1 Then
            Dim x As String = s.Substring(0, 1)
            MessageBox.Show(x, "First Letter", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

LVL 25

Expert Comment

ID: 16608667
oops...my bad...
The point is the same. You can't do it. period, if you use it inside of a windows form because it has the Left property.
Why not use the substring?

Of course you can and should test it before making assumptions.  That is good programming.  LEFT() wouldn't be so great either if it was empty.

The vb library is there  to keep us from being totally alienated as vb devs, and I use quite a bit of it still, such as CSTR()
but in this case substring works just fine.

I do question why Microsoft didn't just add a left and right method to the string as that would have been simpler in many cases and read better.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Simple Linear Regression
Screencast - Getting to Know the Pipeline

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question