Resolved How do you output an entire array?

j_wolfe

Member
Joined
May 25, 2009
Messages
5
Programming Experience
Beginner
Hello, I am trying to make a simple program that generates random musical notes, and for the most part, I'm done, but at the end, I'm left with an array full of notes that I'm not sure what to do with. I want to output the notes to a label, and the array length is variable. I really have no idea what to do. I am trying to display the array OutputNotes:
VB.NET:
Public Class Form1
    Private Sub DoneButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DoneButton.Click
        Randomize()
        Dim NoteString As String() = {"a", "a#", "b", "c", "c#", "d", "d#", "e", "f", "f#", "g", "g#"}
        Dim NumberOfNotes As Integer
        Dim StartNote As Integer

        'determine starting note
        If RandomNoteCheckBox.Checked = True Then
            StartNote = Int((11 - 0 + 1) * Rnd())
        Else
            StartNote = Array.BinarySearch(NoteString, StartingNoteTextBox.Text)
        End If

        'determine # of notes in sequence
        If RandomNumberCheckBox.Checked = True Then
            NumberOfNotes = Int((30 - 5 + 1) * Rnd())
        Else
            NumberOfNotes = Integer.Parse(SequenceTextBox.Text)
        End If

        Dim OutputNotes(NumberOfNotes - 1) As String
        Dim i As Integer = 0

        'generate notes
        Do Until i = NumberOfNotes
            OutputNotes(i) = NoteString(StartNote)
            i += 1
            StartNote = Int((11 - 0 + 1) * Rnd())
        Loop

        'output notes
        ???????

    End Sub
End Class
 
Last edited:
Well 2 ways I can think of

VB.NET:
  lblOutputNotes.Text = ""
        '1) for each loop doesn't care how variable the array length is
        For Each strNoteOut As String In OutputNotes
            lblOutputNotes.Text &= strNoteOut
        Next

        '2) getupperbound(0) gives us the last indexed element in the array 
        For iIndex As Integer = 0 To OutputNotes.GetUpperBound(0)
            lblOutputNotes.Text &= OutputNotes(iIndex)
        Next
 
You can also use String.Join method.
 
Back
Top