Link to home
Start Free TrialLog in
Avatar of SjoerdvW
SjoerdvWFlag for Netherlands

asked on

vb.net & Left function

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 ...???
SOLUTION
Avatar of SStory
SStory
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SjoerdvW

ASKER

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......
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if the string is empty (s=""), Left will return an empty string ("")
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


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.