第一個可能發生的例外狀況類型 'System.InvalidCastException' 發生於 Microsoft.VisualBasic.dll
下列程式碼是我想寫個猜數字遊戲,當按下button1產生四個0~9不相等的數字
但是當執行時,即時執行視窗一直出現下列錯訊。但是還是可以執行。
心情就不是很好了
第一個可能發生的例外狀況類型 'System.InvalidCastException' 發生於 Microsoft.VisualBasic.dll
於是就到了google查到2個MSDN
當您參照的 DataColumn 是 NULL 值 InvalidCastException 例外狀況
變數宣告變更 Visual Basic 2005 中, 並在 Visual Basic . NET 的行為
看完後我還是找不出來問題所在,於是就亂試吧↓
原來的錯誤訊息就變為
錯誤 1 Option Strict On 不允許從 'Integer' 到 'Byte' 的隱含轉換。 C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\guessNumber\guessNumber\Form1.vb 15 27 guessNumber
原來問題就在這兩行
Dim generator As New Random '這代表是Integer
Dim randomValue As Byte
我想說要節省空間就把randomValue的Integer宣告為Byte,但是New Random卻是Integer
所以就會有需要轉型態的問題產生了。原來我真的犯了MSDN里面的錯了。
之後改為 Dim randomValue As Integer 問題就解決啦!
睡覺!
1 Public Class Form1
2 '用來產生隨機數字的陣列
3 Dim oneDimArray() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
4 '用來儲存隨機四個數字的陣列
5 Dim ansArray() As Byte = {11, 11, 11, 11}
6 ''用來儲存猜答案四個數字的陣列
7 Dim guessArray() As Byte = {11, 11, 11, 11}
8 '用來儲存四個數字猜中狀況
9 Dim rightArray() As Byte = {11, 11, 11, 11}
10 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
11 Dim generator As New Random '這代表是Integer
12 Dim randomValue As Integer
13 Dim i, temp As Byte
14 For i = 0 To 9
15 randomValue = generator.Next(0, 9)
16 temp = oneDimArray(i)
17 oneDimArray(i) = oneDimArray(randomValue)
18 oneDimArray(randomValue) = temp
19 Next
20 Me.TextBox1.Text = ""
21 For i = 0 To 3
22 ansArray(i) = oneDimArray(i)
23 Me.TextBox1.Text &= oneDimArray(i)
24 Next
25 End Sub
26 End Class
2 '用來產生隨機數字的陣列
3 Dim oneDimArray() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
4 '用來儲存隨機四個數字的陣列
5 Dim ansArray() As Byte = {11, 11, 11, 11}
6 ''用來儲存猜答案四個數字的陣列
7 Dim guessArray() As Byte = {11, 11, 11, 11}
8 '用來儲存四個數字猜中狀況
9 Dim rightArray() As Byte = {11, 11, 11, 11}
10 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
11 Dim generator As New Random '這代表是Integer
12 Dim randomValue As Integer
13 Dim i, temp As Byte
14 For i = 0 To 9
15 randomValue = generator.Next(0, 9)
16 temp = oneDimArray(i)
17 oneDimArray(i) = oneDimArray(randomValue)
18 oneDimArray(randomValue) = temp
19 Next
20 Me.TextBox1.Text = ""
21 For i = 0 To 3
22 ansArray(i) = oneDimArray(i)
23 Me.TextBox1.Text &= oneDimArray(i)
24 Next
25 End Sub
26 End Class
但是當執行時,即時執行視窗一直出現下列錯訊。但是還是可以執行。
心情就不是很好了
第一個可能發生的例外狀況類型 'System.InvalidCastException' 發生於 Microsoft.VisualBasic.dll
於是就到了google查到2個MSDN
當您參照的 DataColumn 是 NULL 值 InvalidCastException 例外狀況
變數宣告變更 Visual Basic 2005 中, 並在 Visual Basic . NET 的行為
看完後我還是找不出來問題所在,於是就亂試吧↓
資料型別變數宣告, 轉換, 和 Option Strict
就到 [專案] > [屬性]將 Option Strict 從Off改為On原來的錯誤訊息就變為
錯誤 1 Option Strict On 不允許從 'Integer' 到 'Byte' 的隱含轉換。 C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\guessNumber\guessNumber\Form1.vb 15 27 guessNumber
原來問題就在這兩行
Dim generator As New Random '這代表是Integer
Dim randomValue As Byte
我想說要節省空間就把randomValue的Integer宣告為Byte,但是New Random卻是Integer
所以就會有需要轉型態的問題產生了。原來我真的犯了MSDN里面的錯了。
之後改為 Dim randomValue As Integer 問題就解決啦!
睡覺!
浙公网安备 33010602011771号