这是一个非常简单的代码。我刚刚开始使用vb。我正在制作一款游戏,您尝试以最少的时间和最少的点击量来解决难题。每次我调试程序时,它都会突出显示Clicks和FinalTime,并表示Null Reference Exception未处理。我全局宣布了Clicks和FinalTime
Public Class Form1
Dim Clicks As Integer = 0 'The variable that counts the number of times you clicked
Dim Time As Integer 'The vairable that holds the time
Dim TimeMin As Integer 'The variable that holds the minutes
Dim TimeSec As Integer 'The variable that holds the seconds
Dim FinalTime As String 'The variable that holds the final time (minutes and seconds)
Dim NumArray() As Integer
Dim NumArray1() As String
Private Sub Times
Time = Time + 1
TimeSec = TimeSec + 1
TimeMin = Convert.ToInt32(TimeSec \ 60)
If Time >= 60 Then
Time = 0
End If
FinalTime = TimeMin & " min " & Time & " seconds"
lblTime.Text = FinalTime
End Sub
Private Sub Record(ByVal NumArray() As Integer, ByVal NumArray1() As String)
For i As Integer = 0 To 1000
NumArray(i) = Clicks 'Problem is here
i = +1
Array.Sort(NumArray)
Next i
lblRecordClicks.Text = NumArray(0) & " Clicks"
For k As Integer = 0 To 1000
NumArray1(k) = FinalTime 'Problem is here
k = +1
Array.Sort(NumArray1)
Next k
lblRecordTime.Text = NumArray1(0)
End Sub
答案 0 :(得分:2)
规则#1。 VB.NET中的字符串与VB6中的字符串不同。
在VB6中你可以说
Dim text as String
您将text = ""
初始化
在VB.NET中你需要
Dim text as String = vbNullString
或
Dim text as New String
这同样适用于字符串数组(或任何其他数组)
Dim arr as String() ' This makes arr = Nothing
arr = New String(10) { } ' This allocates an array with 11 items (0..10)
或
Dim arr as String() = New String(10) {}
或
Dim arr() As String = New String(10) {}
或
Dim arr() = New String(10) {}
或
Dim arr = New String(10) {}