Home » VB.Net

How to use the Left() function in VB 2010...

Hi Everyone.  I have a very small VS 2010 app that basically reads the records of a text file, adds some additional information to the end of the record read, and writes the new record to a new text file.

The issue I’m having is with the Left() function.  I need to take all chars left of position 10 of a text box and add it to the end of the new record before I write it. When I use the Left() the “Error List” shows an error related to this function.

The error says:  ‘Public Property Left As Integer’ has no parameters and its return type can’t be indexed.

It gives two solutions: 1) Generate Method Stub ‘Left’ in Project1.Form1 and 2) Generate Property Stub ‘Left’ in Project1.Form1

When I select solution 1) that creates a warning indicating: Function ‘Left’ conflicts with property ‘Left’ in the base class ‘Control’ and should be declared ‘Shadows’.  When I select solution 2) that creates a warning indicating: Property ‘Left’ shadows an overloadable member declared in the base class ‘Control’.  If you want to overload the base method, this method must be declared ‘Overloads’.

If anyone could help me out I’d appreciated it. 

Here is the code:

Public Class frmProcessFile

   Private Sub cmdProcessFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


        Dim szInRow As String

        Dim szOutRow As String

        Dim szEmpGroup As String

        Using MyFile As New FileIO.TextFieldParser(txtInFile.Text)

            MyFile.TextFieldType = FileIO.FieldType.Delimited

            While Not MyFile.EndOfData

                szInRow = MyFile.ReadLine

                szEmpGroup = Left(txtEmp.Text, 10)

                szOutRow = szInRow & "PROCESSES COMPLETED ON " & CDate(Today) & vbCrLf

                My.Computer.FileSystem.WriteAllText("EXP-" & txtInFile.Text, szOutRow, True)

            End While

        End Using

    End Sub

End Class


Here is the code generated when I select solution 1:


    Private Function Left(ByVal p1 As String, ByVal p2 As Integer) As String

        Throw New NotImplementedException

    End Function



Here is the code generated when I select solution 2:


   Private _left As String


   Private Property Left(ByVal p1 As String, ByVal p2 As Integer) As String


            Return _left

        End Get

        Set(ByVal value As String)

            _left = value

        End Set

   End Property



4 Answers Found


Answer 1

Imports VB = Microsoft.VisualBasic

Public Class Form1

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load    
    ' Forms have a Left property, so how to use the Left function?
    ' - use the fully qualified name.
    Console.WriteLine(Microsoft.VisualBasic.Left("abc123", 3))
    ' This also uses the fully qualified name, but via the alias we declared
    ' with the imports statement above.
    Console.WriteLine(VB.Left("abc123", 3))
    ' Left is Pretty old syntax. Substring might be the more common approach.
    Console.WriteLine("abc123".Substring(0, 3))
    ' Or you could look at it a different way...
    Console.WriteLine("abc123".Remove(3, 3))
  End Sub

End Class

Answer 2

Hey Thank you!  This worked like a charm!!!



Answer 3

Now this brings me to a new question but related.  Without using 'Imports VB = Microsoft.VisualBasic', is there other native functions that work like the old VB Left$(), Right$(), Mid$() (respectively, LEFT(), Right(), Mid() )?  In general, string manipulation functions.


Answer 4



<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure