【问题标题】:vb.net overload resolution failed for find/replace search查找/替换搜索的 vb.net 重载解析失败
【发布时间】:2012-06-15 23:15:07
【问题描述】:
Public Sub MyFunction()
    Dim lowstring As String
    lowstring = "hi"
    Me.RichTextView.Find(lowstring, 0, 2)
End Sub

以上产生的错误

重载解析失败,因为没有缩小转换就无法调用可访问的'Find'


Public Function Find(characterSet() As Char, 
    start As Integer, end As Integer) As Integer:

参数匹配参数'characterSet'变窄 从'String''1-dimensional array of Char'


Public Function Find(str As String, start As Integer, 
    options As System.Windows.Forms.RichTextBoxFinds) As Integer: 

自变量匹配参数'options''Integer'缩小到 'System.Windows.Forms.RichTextBoxFinds'.

如果您更改替换字符串值,则不会发生错误,仅当您将第二个或第三个值更改为 0 以外的其他值时才会发生错误。

为什么在这里不使用标准整数?这个错误的真正含义是什么?谁能指出我在 vb.net (2010) 中处理重载函数的一些文档?

我希望这个问题足够集中……我只是对这个问题感到很困惑。

感谢您的帮助 - EB

【问题讨论】:

    标签: vb.net function replace find


    【解决方案1】:

    如您所见,RichTextBox.Find 有 7 个重载。 您使用 3 个参数和两个整数调用的那个将 Char[] 作为第一个参数,而不是 String

    当您想从字符列表中查找字符的第一个实例时,使用此重载。

    我假设您想在给定范围内找到string 的位置。那么你需要使用这个重载:RichTextBox.Find(String, Int32, Int32, RichTextBoxFinds)

    例如:

    ' Obtain the location of the search string in RichTextView'
    Dim index = RichTextView.Find(lowstring, 0, 2, RichTextBoxFinds.WholeWord)
    

    请注意,您可以按位组合不同的RichTextBoxFinds

    例如:

    Dim index = Me.RichTextView.Find(
            lowstring,
            0,
            2,
            RichTextBoxFinds.WholeWord Or RichTextBoxFinds.MatchCase
        )
    

    【讨论】:

      【解决方案2】:

      您的函数调用与任何重载都不完全匹配。但是,如果您提供的参数被强制转换为不同的类型,VB 很友好地找到了两个可能的匹配项。

      您可能希望使用string 参数进行重载。所以你应该写,

      RichTextBox1.Find(lowstring, 0, RichTextBoxFinds.WholeWord)
      

      RichTextBoxFinds.WholeWord 碰巧有 2 的数值这一事实并不是使用该值而不是枚举成员名称的理由。

      这也可以:

      RichTextBox1.Find(lowstring, 0, CType(2, RichTextBoxFinds))
      

      但这很愚蠢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-13
        • 1970-01-01
        • 2013-01-11
        • 1970-01-01
        • 1970-01-01
        • 2019-01-12
        • 2015-01-04
        • 1970-01-01
        相关资源
        最近更新 更多