By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Populating An Array With String Values

P: 1
How can I fill an array with string values from an InputBox and then display that in a ListBox?

Expand|Select|Wrap|Line Numbers
  1. Public Class frmQuiz
  2.     Dim strQuestions(10) As String
  3. Private Sub btnQuestion2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuestion2.Click
  4.         Dim strQuestion2 As String
  5.         strQuestion2 = InputBox("What is the last name of the player who many consider to be the best player of all time?")
  6.         strQuestions(1) = strQuestion2
  7.     End Sub
  8.  Private Sub btnGradeQuiz_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGradeQuiz.Click
  9.         MessageBox.Show("Your answers will be displayed. You may make changes to your response before you submit.")
  10.         For Each Value As String In strQuestions
  11.             lstAnswers.Items.Add(strQuestions)
  14.         Next
  21.     End Sub

This does not work right and just displays "String[] Array"
Jan 12 '17 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,731
The ListBox.Items.Add method adds a single item to the items and you are providing a whole array. It is displaying "String[] Array" because it is calling the ToString method on the array since it is not a single string.

Provide the array to the Items property instead of using the Add method...or if you can't do that then loop through the array and add the items to the ListBox individually using the Add method.
Jan 12 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.