20 มิถุนายน 2557

Array และ InArray ใน VB.NET

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Return Index Of Array
        Dim numbers As String() = {"aaa", "bbb", "ccc"}
        MsgBox(numbers.ToList().FindIndex(Function(x) x = "bbb")) 'Case Found
        'MsgBox(numbers.ToList().FindIndex(Function(x) x = "zzz")) 'Case Not Found
        Dim index As Integer = Array.IndexOf(numbers, "ccc")

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim source As String = "111,222,333,444"
        Dim myarray() As String = Split(source, ",")
        MsgBox(myarray(1))
    End Sub

    Public Function InArray(ByVal myArray As String(), ByVal ValueTocheck As String)
        If (myArray.ToList().FindIndex(Function(x) x = ValueTocheck)) = -1 Then
            Return False
        Else
            Return True
        End If
    End Function

    Private Sub btnCheckInArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckInArray.Click
        Dim arr1 As String() = {"rrr", "ssss", "ttttt"}
        MsgBox(InArray(arr1, "tt"))
    End Sub

End Class

ไม่มีความคิดเห็น: