SilentMajority
asked on
How can I get rid of InvalidCastException
A short program to generate an Array of Random Valuse and display it on TextBox.Text looks OK in static state as below.
Buit when it is activated, InvalidCastException occurs at line 3 and halts the progam execution.
1 Private Sub Button1_Click(ByVal sender As System.Object, _
2 ByVal e As System.EventArgs) Handles Button1.Click
3 Dim K As String = "Random Value: " & GenerateArrayWithRandomVal ues()
4 TextBox1.Text = K
5 End Sub
6
7 Public Function GenerateArrayWithRandomVal ues()
8 Dim n As Integer : n = 1 + Rnd() * 20
9 Dim A(0 To n - 1) As Integer
10 Dim i As Integer
11 For i = LBound(A) To UBound(A)
12 A(i) = Rnd() * 1000
13 Next
14 GenerateArrayWithRandomVal ues = A
15
16 End Function
Will you help me how I should rewrite the line 3?
Thank you,
SilentMajority
Buit when it is activated, InvalidCastException occurs at line 3 and halts the progam execution.
1 Private Sub Button1_Click(ByVal sender As System.Object, _
2 ByVal e As System.EventArgs) Handles Button1.Click
3 Dim K As String = "Random Value: " & GenerateArrayWithRandomVal
4 TextBox1.Text = K
5 End Sub
6
7 Public Function GenerateArrayWithRandomVal
8 Dim n As Integer : n = 1 + Rnd() * 20
9 Dim A(0 To n - 1) As Integer
10 Dim i As Integer
11 For i = LBound(A) To UBound(A)
12 A(i) = Rnd() * 1000
13 Next
14 GenerateArrayWithRandomVal
15
16 End Function
Will you help me how I should rewrite the line 3?
Thank you,
SilentMajority
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim K As String = "Random Value: " & GenerateArrayWithRandomValues()
TextBox1.Text = K
End Sub
Public Function GenerateArrayWithRandomValues()
Dim n As Integer : n = 1 + Rnd() * 20
Dim A(0 To n - 1) As Integer
Dim i As Integer
For i = LBound(A) To UBound(A)
A(i) = Rnd() * 1000
Next
GenerateArrayWithRandomValues = A
End Function
ASKER
Dear Bardobrave,
Thank you for your comment, but your idea of applying "Join Function" to GenerateArrayWithRandomVal ues ended with "AmbiguousMatchException".
There seem to be multiple choices for the Type Conversion from Arrayed Integers to String, according to the hints.
Will this be related with the data structure of the Array, say, Linear or Non-Linear?
Thanks,
Silent Majority
Thank you for your comment, but your idea of applying "Join Function" to GenerateArrayWithRandomVal
There seem to be multiple choices for the Type Conversion from Arrayed Integers to String, according to the hints.
Will this be related with the data structure of the Array, say, Linear or Non-Linear?
Thanks,
Silent Majority
I usually code in C# so I'm not very familiarized with VB methods. But surely must be any option on the language that allows you to convert your array without receiving an error.
ASKER
Dear Bardobrave,
I will try Object Type instead of Integer Type for the array to be converted to String.
I will let you know the end result.
Thanks,
Silent Majority
Tokyo, Japan
I will try Object Type instead of Integer Type for the array to be converted to String.
I will let you know the end result.
Thanks,
Silent Majority
Tokyo, Japan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, I got the same answer from MSDN as well. Namely, breaking the Array Data back to individual char elements is only the way to converting the Array to String at large. Actually I did it and concatinated each char in TextBox, in pararel, while I was generating the random numbers. Thanks a lot for you time and concllusion. SilentMajority, Tokyo, Japan
You must convert returning value of GenerateArrayViwhRandomVal
Dim K As String = "Random Value: " & join(GenerateArrayWithRand