none
請教 程式片斷 RRS feed

  • 問題

  • 諸位先進

    小弟觀摩他人的程式   以下片段一直不得搞不懂 不知是否可以指點一二

     尤其Function GetDataSplit 是啥作用呢

     If tmp.GetLength(0) > index Then Return tmp(index)又有何目的

    感謝

            Private myID As String

            Const Sep As String = "|"
            Const DataCommandListClients As String = "01"
            Const DataCommandGlobalMessage As String = "02"
            Const DataCommandSpecialMessage As String = "03"
            Const DataCommandClientIdentity As String = "04"
            Const DataCommandKickClient As String = "05"
            Const DataCommandClientID As String = "06"

     Private Function GetDataSplit(ByVal data As String, ByVal index As String) As String

                Dim tmp As String() = data.Split(Sep)

                If tmp.GetLength(0) > index Then Return tmp(index)

            End Function

    #Region "HandleReceivedData"

            Private Sub HandleReceivedData(ByVal Data As String)

                Select Case GetDataSplit(Data, 0)
                    '' Const DataCommandListClients As String = "01"

                Case DataCommandClientIdentity  ''  Const DataCommandClientIdentity As String = "04"
                        'server only

                    Case DataCommandListClients ''    Const DataCommandKickClient As String = "05
                        Dim Num As Integer = CInt(GetDataSplit(Data, 1))
                        Dim ClientList As New System.Collections.ArrayList
                        Dim i As Integer
                        For i = 1 To Num
                            Dim ClientInfo As New ClientListEntry
                            ClientInfo.ID = GetDataSplit(Data, (5 * (i - 1)) + 2)
                            ClientInfo.Hostname = GetDataSplit(Data, (5 * (i - 1)) + 3)
                            ClientInfo.IPAddress = GetDataSplit(Data, (5 * (i - 1)) + 4)
                            ClientInfo.Nickname = GetDataSplit(Data, (5 * (i - 1)) + 5)
                            ClientInfo.Username = GetDataSplit(Data, (5 * (i - 1)) + 6)
                            ClientList.Add(ClientInfo)
                        Next
                        Try
                            RaiseEvent ReceivedClientList(ClientList)
                        Catch
                        End Try

                    Case DataCommandGlobalMessage '' Const DataCommandGlobalMessage As String = "02"
                        Try
                            RaiseEvent ReceivedGlobalMessage(GetDataSplit(Data, 1))
                        Catch
                        End Try

                    Case DataCommandSpecialMessage '' Const DataCommandSpecialMessage As String = "03"
                        Dim Show As Boolean = True
                        Dim Num As Integer = CInt(GetDataSplit(Data, 2))
                        Dim DidNotSentTo As New ArrayList
                        Dim i As Integer
                        Dim ID As String
                        For i = 1 To Num
                            ID = GetDataSplit(Data, 2 + i)
                            DidNotSentTo.Add(ID)
                            If myID = ID Then Show = False
                        Next
                        If Show = True Then
                            Try
                                RaiseEvent ReceivedSpecialMessage(GetDataSplit(Data, 1), DidNotSentTo)
                            Catch
                            End Try
                        End If

                    Case DataCommandClientID     ' Const DataCommandClientID As String = "06"
                        myID = GetDataSplit(Data, 1)
                        Try
                            RaiseEvent ReceivedClientID(myID)
                        Catch
                        End Try

                    Case DataCommandKickClient
                        'server only

                End Select

            End Sub

    2006年8月13日 下午 07:56

解答

  • 1.Dim tmp As String() = data.Split(Sep),這段是在將字串用step拆解

    2.If tmp.GetLength(0) > index Then Return tmp(index),判段你要取回的index是否合理

    例如你下 GetDataSplit("a|b|c", "0") 會得到因為在1步時tmp(0) 為a  1為b   2為c

    第2步判別陣列大小3比你要取的index值來得大(表示合理值,取用不會出錯), 回傳a

    2006年8月14日 上午 12:21