Groups | Blog | Home
all groups > vb.net > march 2005 >

vb.net : Left and Right Functions


patang
3/18/2005 8:15:02 PM
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

Michael C#
3/18/2005 11:31:40 PM
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


[quoted text, click to view]

Michael C#
3/18/2005 11:33:51 PM
Oops. Sorry, been too long. SubString() is like MID(), not SUB. DOH!

[quoted text, click to view]

AMercer
3/19/2005 2:29:03 AM
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.

[quoted text, click to view]
Cor Ligthert
3/19/2005 9:27:50 AM
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

Jay B. Harlow [MVP - Outlook]
3/19/2005 12:19:19 PM
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


[quoted text, click to view]

Herfried K. Wagner [MVP]
3/19/2005 12:42:16 PM
"patang" <patang@discussions.microsoft.com> schrieb:
[quoted text, click to view]

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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Cor Ligthert
3/19/2005 3:33:20 PM
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

AddThis Social Bookmark Button