简体   繁体   English

vb.net function Left() 给出错误 BC32016

[英]vb.net function Left() gives error BC32016

I'm having trouble with using Left() function in my vb.net code.我在 vb.net 代码中使用 Left() function 时遇到问题。 while running the code I get an error message "BC32016 Visual Basic AND VB.NET 'Public Overloads Property Left As Integer' has no parameters and its return type cannot be indexed."运行代码时,我收到一条错误消息“BC32016 Visual Basic AND VB.NET 'Public Overloads Property Left As Integer' 没有参数,无法为其返回类型编制索引。”

The syntax seems okay as it defined in语法似乎没问题,因为它定义在

https://learn.microsoft.com/en-us/do.net/api/microsoft.visualbasic.strings.left?view.netframework-4.7.2 https://learn.microsoft.com/en-us/do.net/api/microsoft.visualbasic.strings.left?view.netframework-4.7.2

My code is as bellow我的代码如下

    Dim FileName As String = ""
    Dim FilePath As String = ""
    Dim TargetPath As String = ""
    Dim FilePathLength As Integer = 0

    'Get Text File Location that user need to import (AskForFile is an OpenDialogBox)

    AskForFile.Reset()
    AskForFile.Title = "Select File you want to inport"
    AskForFile.Filter = "Text File|*.txt|All Files|*.*"
    AskForFile.Multiselect = False
    AskForFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    AskForFile.ShowDialog()

    FileName = AskForFile.SafeFileName.ToString
    TargetPath = AskForFile.FileName.ToString
    FilePathLength = Len(TargetPath) - Len(FileName)

    FilePath = Left(TargetPath, FilePathLength)

I'm Using Visual Studio 2017 community edition.我正在使用 Visual Studio 2017 社区版。 Can somebody help me to identify the problem?有人可以帮我找出问题所在吗?

In my case this error was causing by accidentally having this line at the top of the file:在我的例子中,这个错误是由于不小心将这一行放在文件的顶部造成的:

Imports System.Windows.Forms.AxHost

I've no idea how it got there!我不知道它是怎么到那里的!

This seems to have implied a reference to System.Windows.Forms which meant that Left was resolving to System.Windows.Forms.Left instead of Microsoft.VisualBasic.Strings.Left这似乎暗示了对 System.Windows.Forms 的引用,这意味着 Left 解析为 System.Windows.Forms.Left 而不是 Microsoft.VisualBasic.Strings.Left

As the original answer suggests, you might also have another "Left" function somewhere, and if you right click Left and "goto definition" (shift f2 on vb settings or f12 on c# settings) your editor should be taken to the instance of Left which should not be there.正如原始答案所暗示的那样,您可能在某处还有另一个“Left”function,如果您右键单击Left和“goto definition”(在 vb 设置上移动 f2 或在 c# 设置上移动 f12),您的编辑器应该被带到 Left 的实例不应该在那里。

Somewhere in the current class a property by the name Left has been defined. 在当前类的某个地方,已定义了名称为Left的属性。 The compiler always tries to reference the most local objects first, then moves on to the more "global" ones. 编译器总是首先尝试引用最本地的对象,然后再移至更“全局”的对象。

Simply put it thinks you are trying to reference this property called Left rather than the function. 简单地说,它认为您正在尝试引用称为Left该属性,而不是该函数。 It can be fixed by specifying the full namespace which the function is in: 可以通过指定函数所在的完整名称空间来解决此问题:

FilePath = Microsoft.VisualBasic.Strings.Left(TargetPath, FilePathLength)

HOWEVER , both Left() and Len() are methods from Visual Basic 6 which dates back to earlier than 2002 (when VB.NET first was released), and they exist purely for backwards compatibility. 但是Left()Len()都是Visual Basic 6中的方法,它们的历史可以追溯到2002年(最初发布VB.NET时),而它们的存在纯粹是为了向后兼容。 It is not recommended to use these methods in newer projects as you never know if/when they might get removed. 不建议在较新的项目中使用这些方法,因为您永远都不知道是否/何时将它们删除。 I highly recommend you replace these. 强烈建议您更换这些。

In .NET the replacement is String.Substring() and String.Length respectively. 在.NET中,替换分别是String.Substring()String.Length

FilePathLength = TargetPath.Length - FileName.Length

FilePath = TargetPath.Substring(0, FilePathLength)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM