SjoerdvW
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.Contr ol.Left
Replacing left with its full path (Microsoft.VisualBasic.Str ings.left) works fine ...???
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.Contr
Replacing left with its full path (Microsoft.VisualBasic.Str
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
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.
ASKER
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......