English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have 20 integers in an array and I am picking out all the even numbers. But then I am supposed to display all of them in a message box. I can display them in a Label

For Each x As Integer In MyArray
If x Mod 2 = 0 Then
Label2.Text = Label2.Text & x.ToString & ", "
End If
Next x
But when I change it to
message = message & x.ToString & ", "
MessageBox.Show(message, "Name", MessageBoxButtons.OK, MessageBoxIcon.Information)

The second 'message' has the green scribbled line and the error that the variable message has been assigned before it has been assigned a value. The numbers will show, but in one message box at a time. What am I forgetting? Any ideas?

2007-10-12 12:12:32 · 3 answers · asked by VBnewbieANDherHubby 3 in Computers & Internet Programming & Design

I did declare the message, that is why it is weired, it only underlines the second 'message', not the first one.

2007-10-12 12:23:46 · update #1

message (this one is not underlined) = message(this one is) & x.ToString & ", "

2007-10-12 12:25:01 · update #2

MARK:

Thanks you are the bomb!!!! I got it to work that everything shows in one message box. Will try to get rid of the underline now! THANKS!!!!

2007-10-12 12:51:54 · update #3

3 answers

William V is correct about needing to declare the message variable.

DIM message as string

EDIT:

The green line means that VB is unsure of what the newly variable contains. In otherwords you should initialize it and not trust that it is created as an empty string

DIM message as string = ""

OR
on a seperate line just before the FOR/NEXT loop place
message = "" to initialize

The reason you are getting multiple message boxes is because you are showing the message box with in the loop and it is getting called each time the loop runs.

Place you messagbox calls outside of the For Next loop
FOR
......
NEXT
MessageBox.Show(message, "Name", MessageBoxButtons.OK, MessageBoxIcon.Information)

Now you will only see one message box with the entire answer after the loop completes

2007-10-12 12:25:20 · answer #1 · answered by MarkG 7 · 0 0

You need to declare the variable "message" as in:

Dim message as String

When you dragged the TextBox control to the form, Visual Basic instantiated (created a copy of) the control in code that's created behind the scenes. That's not the case for the variable "message" that you created.

hth

2007-10-12 12:18:49 · answer #2 · answered by Anonymous · 1 0

not particular what you're asking the following yet enable me see if i have were given it. this technique creates a random wide form and places it in a label. the youngster will then favor to furnish an answer in a textual content container which has some correlation to the wide form contained in the label and also you would have a 2d label advising no matter if the answer presented is ideal or not. Is that it?

2016-10-09 02:56:00 · answer #3 · answered by fomby 4 · 0 0

fedest.com, questions and answers