String类型的值不能转换为String的1维数组

时间:2015-11-19 20:10:22

标签: arrays vb.net

这是我宣布我的代码的方式:

Dim startline As String
Dim curserline As String
Dim curserpos As Integer
Dim x As Integer
Dim back As Boolean = False
Dim word() As String
Dim word1() As String

Dim AutoComplete As New List(Of String)


"This is part of my code but am not going to share all of it. I assume your getting."

 word1 = sr.ReadLine.Split("|"c)

 word = word1

 AutoComplete.Add(word(0) & word(1))


"For starters, I am getting this error here:"

Value of type String cannot be converted into 1 dimensional array of String

"From here:"


 word = Mid(RichTextBox1.Text, curserpos + 1, RichTextBox1.SelectionStart - curserpos)


"And here:"


 If auto.StartsWith(word) = True And word <> "" And word <> " " Then


"From this code here:"



Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged



    Try

        ListView1.Visible = False
        ListView1.Items.Clear()



        startline = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
        curserline = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
        curserpos = RichTextBox1.SelectionStart



        While Mid(RichTextBox1.Text, curserpos, 1) <> vbTab And Mid(RichTextBox1.Text, curserpos, 1) <> " " And Mid(RichTextBox1.Text, curserpos, 1) <> "." And Mid(RichTextBox1.Text, curserpos, 1) <> "=" And Mid(RichTextBox1.Text, curserpos, 1) <> "(" And Mid(RichTextBox1.Text, curserpos, 1) <> ")" And Mid(RichTextBox1.Text, curserpos, 1) <> "," And Mid(RichTextBox1.Text, curserpos, 1) <> "&" And Mid(RichTextBox1.Text, curserpos, 1) <> "{" And Mid(RichTextBox1.Text, curserpos, 1) <> "}" And Mid(RichTextBox1.Text, curserpos, 1) <> """" And Mid(RichTextBox1.Text, curserpos, 1) <> "<" And Mid(RichTextBox1.Text, curserpos, 1) <> ">" And Mid(RichTextBox1.Text, curserpos, 1) <> "!" And curserline = startline



            curserpos -= 1
            curserline = RichTextBox1.GetLineFromCharIndex(curserpos)
        End While


        If curserline = startline Then
            word = Mid(RichTextBox1.Text, curserpos + 1, RichTextBox1.SelectionStart - curserpos)
        Else
            word = Mid(RichTextBox1.Text, curserpos + 2, RichTextBox1.SelectionStart - curserpos - 1)
        End If

请帮帮我们!这是一个VB.NET问题,我试图建立一个自动完成。但是主题中显示的错误让我的生活变得生机勃勃。请帮助伙计!

我刚刚添加的代码:

   For Each auto As String In AutoComplete

            If auto.StartsWith(word) = True And word <> "" And word <> " " Then

这是sr。部分请:

    Try
        Dim output As String = "C:\Users\Acer\Documents\Visual Studio 2010\Projects\WindowsApplication3\WindowsApplication3\My Project\Grammarchecker1.txt"


        If File.Exists(output) Then
            Using sr As New StreamReader(output)
                While Not sr.EndOfStream
                    word1 = sr.ReadLine.Split("|"c)

                    word = word1

                    AutoComplete.Add(word(0).ToString & word(1).ToString)


                End While
            End Using
        End If

3 个答案:

答案 0 :(得分:0)

问题在于这一行:

word = Mid(RichTextBox1.Text, curserpos + 1, RichTextBox1.SelectionStart - curserpos)

您已在此行中将word声明为数组字符串:

Dim word() As String '<-- the word() with the parens means it's an array of strings, not a single String.

Mid函数返回单个字符串,而不是字符串数组。您应该将其分配给另一个声明为String类型的变量:

Dim singleWord As String = Mid(RichTextBox1.Text, curserpos + 1, RichTextBox1.SelectionStart - curserpos)

答案 1 :(得分:0)

字符串列表是一个数组。我想如果你要添加像

这样的字符串列表
  AutoComplete.Add(word(0).toString & word(1).toString )

然后您可以通过

解析数组
    Dim Starts_with As String = "0"
    For Each word In AutoComplete
        If word.StartsWith("D") Then
            Starts_with = word
        End If
    Next

答案 2 :(得分:0)

您对变量的使用似乎过于节俭。您不需要尽可能少地使用它 - 它只会导致问题。

例如,您读取数据的代码可能更像

Dim inputFile As String = "C:\Users\Acer\Documents\Visual Studio 2010\Projects\WindowsApplication3\WindowsApplication3\My Project\Grammarchecker1.txt"

If File.Exists(inputFile) Then
    Using sr As New StreamReader(inputFile)
        While Not sr.EndOfStream
            Dim wordParts = sr.ReadLine.Split("|"c)
            If wordParts.Count >= 2 Then
                AutoComplete.Add(wordParts(0) & wordParts(1))
            End If
        End While
    End Using
Else
    Throw New FileNotFoundException(String.Format("Input file {0} not found.", inputFile))
End If

请注意我如何将输入文件命名为inputFile而不是output,并将该部分命名为wordParts。如果您关注的话,将为您处理不再使用的变量。另外,我进行检查以确保至少有两个以“|”分隔的条目否则wordParts(1)将不存在,因此会抛出异常。

如果文件不存在可能无关紧要,在这种情况下删除Throw New FileNotFoundException(String.Format("Input file {0} not found.", inputFile))行。