vb.net 变量“名称”在被赋值之前使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16262377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Variable 'Name' is used before it has been assigned a value
提问by ramon robben
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
你好,我想聊天,我在互联网上搜索,我终于找到了一些可能有用的东西。但是现在我在 name 上收到一个错误,它说在分配值之前使用了变量名
Code:
代码:
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
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.
我该如何解决这个错误??我已经尝试移动结束 if 并更改其他一些以结束 if。但仍然没有找到正确的代码请帮助。
回答by Jon Skeet
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"
You're trying to concatenate the currentvalue of namewith another string ("Foo") - but namedoesn't havea value yet.
你试图以连接电流的值name与另一个字符串(“富”) -但name不具有价值呢。
You could change it to:
您可以将其更改为:
Dim name as String = ""
name = name & "Foo"
... but you'd be better off using a StringBuilder. So for example, your first loop could be:
...但你最好使用StringBuilder. 例如,您的第一个循环可能是:
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
回答by James
The problem is there are a couple of scenarios that could lead to your variable never being initialised. To fix, simply give a default value e.g.
问题是有几种情况可能会导致您的变量永远不会被初始化。要修复,只需给出一个默认值,例如
Dim Name As String = ""
Also, you are re-declaring the same variable twice, you only need to do this once, just clear it again if you need it to be "blank" for the next loop or use 2 separate variables (I.e. more specifically named) instead.
此外,您要重新声明同一个变量两次,您只需要执行一次,如果您需要它为下一个循环“空白”或使用 2 个单独的变量(即更具体命名),只需再次清除它。

