Left and Right Functions

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is really silly question.

Dim s As String
s = "the"
s = Right(s, 2)

Why this gives me compilation error? (This works in VB6 .. what do I have to
do to make it work in VB.Net)

I have imported this:

imports system.string
 
Dim s As String
s = s.SubString(1, 2)

SubString() is like the SUB() function in VB 6. You can create your own
Right() function using SubString():

Private Function strRight(ByVal s As String, ByVal i As Integer) As String
If Not ((i >= s.Length) Or (s.Length < 1)) Then
s = s.Substring(s.Length - i, i)
End If
Return (s)
End Function
 
patang,

Strange, this give for me no compiling error, what version are you using and
when it is 2003 did you install the SP1.

Cor
 
If you are coding this in a form, then you are bumping into a namespace
issue. Left and Right in forms pertain to x-axis distances (there is no name
collision with Mid). Try
Microsoft.VisualBasic.Right(s, 2)
which is a fully qualified reference to the string function you want.

I find this irritating too, but you get used to it fairly quickly. In this
particular case, I use mid() more often and use right() and left() less often.
 
patang said:
This is really silly question.

Dim s As String
s = "the"
s = Right(s, 2)

Why this gives me compilation error? (This works in VB6 .. what do I have
to
do to make it work in VB.Net)

'Right' is in conflict with a property of your form. Use 'Strings.Right'
instead.
 
Herfried,

Funny normally I just take a piece of a form to test this, now I did it in a
seperated class.
(As you know am I never using this kind of commands because of the First
indexer)

:-)

Cor
 
Patang,
As the others suggests. Left & Right are properties of Forms. If you attempt
to use the Left & Right functions on a form you will receive this error.

What I normally do is use an import alias in my form class files.

Something like:

Imports VB = Microsoft.VisualBasic

Public Class MainForm
Inherits System.Windows.Forms.Form

...

Public Sub SomeMethod()
Dim s As String
s = "the"
s = VB.Right(s, 2)
End Sub

End Class

The advantage of the import alias "VB = Microsoft.VisualBasic" is that it
will work with all types (classes, modules, enums, ...) in the
Microsoft.VisualBasic namespace.

Hope this helps
Jay
 
Back
Top