none
VB learning question RRS feed

  • 問題

  • http://msdn.microsoft.com/zh-tw/library/25xw097s(VS.80).aspx

    in this page, i try this code under Dim team and Dim player

        For Each player In team
           ComboBox1.Items.Add(player.Position)
        Next

    but it say these code can't write here, where should i write these code will correct?

    2012年12月6日 上午 03:11

解答

  •     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With player1
                .FirstName = "Andrew"
                .LastName = "Cencini"
                .Number = 43
                .Position = "Shortstop"
            End With
            With player2
                .FirstName = "Robert"
                .LastName = "Lyon"
                .Number = 11
                .Position = "Catcher"
            End With
            With player3
                .FirstName = "Eduardo"
                .LastName = "Saavedra"
                .Number = 52
                .Position = "First Base"
            End With
    
            With player4
                .FirstName = "Karl"
                .LastName = "Jablonski"
                .Number = 22
                .Position = "Pitcher"
            End With
    
            team.Add(player1, player1.Position)
            team.Add(player2, player2.Position)
            team.Add(player3, player3.Position)
            team.Add(player4, player4.Position)
    
            For Each player In team
               ComboBox1.Items.Add(player.Position)
            Next
        End Sub
    
    把For Each放到Form_Load裡,試試看。

    理直氣和,切記。

    http://blog.kkbruce.net

    • 已標示為解答 貝特 2012年12月6日 上午 03:47
    2012年12月6日 上午 03:43

所有回覆

  • 你是否有依照 http://msdn.microsoft.com/zh-tw/library/x84ydca5(VS.80).aspx 的順序一步一步建立起專案內容,你的那一篇只有部分程式碼,單獨複製貼上,當然會有問題。

    理直氣和,切記。

    http://blog.kkbruce.net

    2012年12月6日 上午 03:23
  • 是的,我有按照之前的步驟將專案建立起來,因此有前面完整的程式碼。

    只有這步for each按照文件上所說:於宣告區段 (在 team 的宣告下面) 中加入以下程式碼。

    會產生"陳述式不可以出現在方法主體之外"等編譯器錯誤訊息。

    我是否放錯了位置? 還是程式碼有所遺漏? 我先留下整個程式碼。 感謝回覆。

    Public Class Form1
        Dim player1 As New Persons.Players
        Dim player2 As New Persons.Players
        Dim player3 As New Persons.Players
        Dim player4 As New Persons.Players
        Dim team As New Collection
        Dim player As Persons.Players

        For Each player In team '這三行會出錯"
           ComboBox1.Items.Add(player.Position)
        Next

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With player1
                .FirstName = "Andrew"
                .LastName = "Cencini"
                .Number = 43
                .Position = "Shortstop"
            End With
            With player2
                .FirstName = "Robert"
                .LastName = "Lyon"
                .Number = 11
                .Position = "Catcher"
            End With
            With player3
                .FirstName = "Eduardo"
                .LastName = "Saavedra"
                .Number = 52
                .Position = "First Base"
            End With

            With player4
                .FirstName = "Karl"
                .LastName = "Jablonski"
                .Number = 22
                .Position = "Pitcher"
            End With

            team.Add(player1, player1.Position)
            team.Add(player2, player2.Position)
            team.Add(player3, player3.Position)
            team.Add(player4, player4.Position)
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MsgBox(player1.Position & " " & player1.FullName & " is now at bat.")
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            MsgBox(player2.Position & " " & player2.FullName & " is on deck.")
        End Sub

        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            Dim SelectedPlayer As Persons.Players
            SelectedPlayer = team(ComboBox1.SelectedItem)
            MsgBox("playing " & ComboBox1.SelectedItem & " is " & SelectedPlayer.FullName)
        End Sub
    End Class


    • 已編輯 貝特 2012年12月6日 上午 03:33
    2012年12月6日 上午 03:31
  •     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With player1
                .FirstName = "Andrew"
                .LastName = "Cencini"
                .Number = 43
                .Position = "Shortstop"
            End With
            With player2
                .FirstName = "Robert"
                .LastName = "Lyon"
                .Number = 11
                .Position = "Catcher"
            End With
            With player3
                .FirstName = "Eduardo"
                .LastName = "Saavedra"
                .Number = 52
                .Position = "First Base"
            End With
    
            With player4
                .FirstName = "Karl"
                .LastName = "Jablonski"
                .Number = 22
                .Position = "Pitcher"
            End With
    
            team.Add(player1, player1.Position)
            team.Add(player2, player2.Position)
            team.Add(player3, player3.Position)
            team.Add(player4, player4.Position)
    
            For Each player In team
               ComboBox1.Items.Add(player.Position)
            Next
        End Sub
    
    把For Each放到Form_Load裡,試試看。

    理直氣和,切記。

    http://blog.kkbruce.net

    • 已標示為解答 貝特 2012年12月6日 上午 03:47
    2012年12月6日 上午 03:43
  • 恩,確實能正確動作了,

    之前我也有試著將for each放到form_load中,但沒注意應該要接在team.add之後,造成加入combobox1還是沒東西的問題。

    非常感謝解答這個初學者的問題。 :)

    2012年12月6日 上午 03:50
  • 你要對集合取值,要先有集合才能取,對吧。

    加油。


    理直氣和,切記。

    http://blog.kkbruce.net

    2012年12月6日 上午 03:55