Solved

How can I get rid of InvalidCastException

Posted on 2010-11-09
6
499 Views
Last Modified: 2012-05-10
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: " & GenerateArrayWithRandomValues()
4       TextBox1.Text = K
5   End Sub
6
7   Public Function GenerateArrayWithRandomValues()
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     GenerateArrayWithRandomValues = A
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

Open in new window

0
Comment
Question by:SilentMajority
  • 3
  • 3
6 Comments
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34092431
Your GenerateArrayWithRandomValues is returning an array while on Button1_Click you are assigning it to a string.

You must convert returning value of GenerateArrayViwhRandomValues to a string, try with join function:

Dim K As String = "Random Value: " & join(GenerateArrayWithRandomValues(),"|")
0
 

Author Comment

by:SilentMajority
ID: 34160685
Dear Bardobrave,

Thank you for your comment, but your idea of applying "Join Function" to GenerateArrayWithRandomValues 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
0
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34161989
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.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:SilentMajority
ID: 34162374
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
0
 
LVL 19

Accepted Solution

by:
Bardobrave earned 500 total points
ID: 34163869
You also can loop through your array and build a string with it's values and assign it to the button click text instead of assigning the whole array.

More dirty but probably faster and easier.
0
 

Author Closing Comment

by:SilentMajority
ID: 34164156
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
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What algorithm is used to encrypt 33 632
Why is Peterson's algorithm better at mutual exclusion that its predecessors? 1 129
scoresClump  challenge 31 139
Hashing Algorithm 5 67
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question