Question 2 simple functions are failing... why? Right() & Left()

motoburn

Member
Joined
Jun 15, 2010
Messages
17
Location
Collinwood, TN
Programming Experience
Beginner
I am getting an error on compilation for the Right() and Left() sections of this. I borrowed this function from another site earlier today (been all over so I dont remember where) I needed this function to strip the directory and extension off a file name so i could use the filename as a field in a database table.

The error states:
VB.NET:
Error	1	'Public ReadOnly Property Right() As Integer' has no parameters and its return type cannot be indexed.	
~\WindowsApplication1\mtI1.vb	29	17	MotoTally

Error	2	'Public Property Left() As Integer' has no parameters and its return type cannot be indexed.
~\WindowsApplication1\mtI1.vb	34	21	MotoTally

and the function is :
VB.NET:
    Public Function GetFileName(ByVal flname As String) As String

        'Get the filename without the path or extension.
        'Input Values:
        '   flname - path and filename of file.
        'Return Value:
        '   GetFileName - name of file without the extension.

        Dim posn As Integer, i As Integer
        Dim fName As String

        posn = 0
        'find the position of the last "\" character in filename
        For i = 1 To Len(flname)
            If (Mid(flname, i, 1) = "\") Then posn = i
        Next i

        'get filename without path
        fName = [U][COLOR="Navy"]Right[/COLOR][/U](flname, Len(flname) - posn)

        'get filename without extension
        posn = InStr(fName, ".")
        If posn <> 0 Then
            fName = [U][COLOR="Navy"]Left[/COLOR][/U](fName, posn - 1)
        End If
        GetFileName = fName
    End Function

I am using VS 2008 and VB.Net and building a Windows Forms application to search a specific directory and harvest data from excel files generated by a new machine on the production line... and help would be greatly appreciated.

:)
thanks
 
I'm guessing that your code is in a form, in which case your use of Left and Right are interpreted as referring to the Left and Right properties of the form, not the Left and Right methods of the Strings module. To avoid the name clash you would have to qualify those method names, i.e. use Strings.Left and Strings.Right.

That said, that code is completely unnecessary anyway. The Path class in the System.IO namespace is what you should use to manipulate file an folder paths. It has GetFileName and GetFileNameWithoutExtension methods.

Apart from that, that code looks like it was written in VB6. If you really were going to write that method in VB.NET then it should look like this:
VB.NET:
''' <summary>
''' Gets the file name without the path or the extension.
''' </summary>
''' <param name="filePath">
''' The full path of the file.
''' </param>
''' <returns>
''' The name of the file without the extension.
''' </returns>
Public Function GetFileName(ByVal filePath As String) As String
    'Get the index of the last delimiter.
    Dim lastDelimiterIndex As Integer = filePath.LastIndexOf("\"c)

    'Get the index of the dot.
    Dim dotIndex As Integer = filePath.LastIndexOf("."c)

    Return filePath.Substring(lastDelimiterIndex + 1, dotIndex - lastDelimiterIndex - 1)
End Function
 
Back
Top