变量“名称"在被赋值之前使用 [英] Variable 'Name' is used before it has been assigned a value
问题描述
你好,我想聊天,我在互联网上搜索,我终于找到了一些可能有用的东西.但现在我在 name 上收到一个错误,它说在分配值之前使用了变量名
hello i wanted to make a chat and i searched on the internet and i finaly found something that may work. but now i get an error at name it says Variable Name is used before it has assigned a value
代码:
Public Sub findForm1()
If Trim(Mid(My.Forms.Private1.Text, My.Forms.Private1.Text.Length - 2)) = formNo Then
My.Forms.Private1.RichTextBox1.Text = My.Forms.Private1.RichTextBox1.Text & poruka + vbCrLf
ElseIf Trim(Mid(My.Forms.Private2.Text, My.Forms.Private2.Text.Length - 2)) = formNo Then
My.Forms.Private2.RichTextBox1.Text = My.Forms.Private2.RichTextBox1.Text & poruka + vbCrLf
Else
If My.Forms.Private1.Visible = False Then
Dim name As String
For i As Integer = 1 To poruka.Length
If Mid(poruka, i, 2) = ": " Then
Exit For
name = name & Mid(poruka, i, 1)
End If
Next
My.Forms.Private1.Show()
My.Forms.Private1.Text = Trim(name) & " " & br
My.Forms.Private1.RichTextBox1.Text = My.Forms.Private1.RichTextBox1.Text & poruka + vbCrLf
Else
Dim name As String
For i As Integer = 1 To poruka.Length
If Mid(poruka, i, 2) = ": " Then
Exit For
End If
name = name & Mid(poruka, i, 1)
Next
My.Forms.Private2.Show()
My.Forms.Private2.Text = Trim(name) & " " & br
My.Forms.Private2.RichTextBox1.Text = My.Forms.Private2.RichTextBox1.Text & poruka + vbCrLf
End If
End If
formNo = Nothing
poruka = Nothing
End Sub
我该如何修复这个错误??我已经尝试移动结束 if 并更改其他一些以结束 if.但仍然没有找到正确的代码请帮助.
how can i fix this error?? i already tried to move the end if and change some else to end if. but still not find the right code PLEAS HELP.
推荐答案
是的,编译器完全正确.您的代码中的问题可以归结为以下内容:
Yes, the compiler's absolutely right. The problem in your code can be boiled down to something like this:
Dim name as String
name = name & "Foo"
您正在尝试将 name
的 current 值与另一个字符串 ("Foo") 连接起来 - 但 name
没有 还有一个值.
You're trying to concatenate the current value of name
with another string ("Foo") - but name
doesn't have a value yet.
您可以将其更改为:
Dim name as String = ""
name = name & "Foo"
...但是您最好使用 StringBuilder
.例如,您的第一个循环可能是:
... but you'd be better off using a StringBuilder
. So for example, your first loop could be:
Dim nameBuilder As StringBuilder = New StringBuilder()
For i As Integer = 1 To poruka.Length
If Mid(poruka, i, 2) = ": " Then
Exit For
nameBuilder.Append(Mid(poruka, i, 1))
End If
Next
My.Forms.Private1.Show()
My.Forms.Private1.Text = Trim(nameBuilder.ToString()) & " " & br
这篇关于变量“名称"在被赋值之前使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!