Como manipular colecciones en VB.NET con delegados

27 01 2008

Hola a todos. Hoy me encontré con una consulta que no pude responder enseguida: El uso de delegados en VB.NET. Hasta ahora siempre los había usado en C#; pero en VB.NET no todavía asi que empecé haciendo un ejemplo simple, e investigando un poco.

Llegué al post: http://vbnotebookfor.net/2007/08/23/how-to-use-the-action-and-predicate-delegates/ que me sirvió de mucha ayuda. En el se explica como usar los delegados de Predicate y Action para operar en colecciones.

Bueno aquí les dejo el ejemplo:

Imports System.Text

 

Module Module1

 

    Sub Main()

        Dim personas As New List(Of Persona)

 

        Dim p1 As New Persona(1, “Walter”, “Poch”, “Italia 65″)

        personas.Add(p1)

        Dim p2 As New Persona(2, “Wilfredo”, “Mata”, “San Luis 1928″)

        personas.Add(p2)

        Dim p3 As New Persona(3, “Carlos”, “Memem”, “Anillaco 1000″)

        personas.Add(p3)

 

        Console.WriteLine(“Personas que empiezan con W”)

        Console.WriteLine(“—————————”)

        Dim personasEmpiezanConW As List(Of Persona)

        personasEmpiezanConW = personas.FindAll(AddressOf empiezaConW)

        personasEmpiezanConW.ForEach(AddressOf mostrarPersona)

        Console.WriteLine()

 

        Console.WriteLine(“Persona llamada Carlos”)

        Console.WriteLine(“———————-”)

        Dim elCarlos As Persona

        elCarlos = personas.Find(AddressOf buscarElCarlos)

        Console.Write(elCarlos)

        Console.WriteLine()

 

        Console.ReadKey()

    End Sub

 

    Public Function empiezaConW(ByVal persona As Persona) As Boolean

        Return persona.Nombre.StartsWith(“”, StringComparison.InvariantCultureIgnoreCase)

    End Function

 

    Public Function buscarElCarlos(ByVal persona As Persona) As Boolean

        Return persona.Nombre.Equals(“carlos”, StringComparison.InvariantCultureIgnoreCase)

    End Function

 

    Public Sub mostrarPersona(ByVal persona As Persona)

        Console.Write(persona)

    End Sub

 

    Class Persona

        Private _id As Integer

        Private _nombre As String

        Private _apellido As String

        Private _direccion As String

 

        Public Property Id() As Integer

            Get

                Return Me._id

            End Get

            Set(ByVal value As Integer)

                Me._id = value

            End Set

        End Property

 

        Public Property Nombre() As String

            Get

                Return Me._nombre

            End Get

            Set(ByVal value As String)

                Me._nombre = value

            End Set

        End Property

 

        Public Property Apellido() As String

            Get

                Return Me._apellido

            End Get

            Set(ByVal value As String)

                Me._apellido = value

            End Set

        End Property

 

        Public Property Direccion() As String

            Get

                Return Me._direccion

            End Get

            Set(ByVal value As String)

                Me._direccion = value

            End Set

        End Property

 

        Public Sub New(ByVal id As Integer, _

        ByVal nombre As String, _

        ByVal apellido As String, _

        ByVal direccion As String)

            Me._id = id

            Me._nombre = nombre

            Me._apellido = apellido

            Me._direccion = direccion

        End Sub

 

        Public Overrides Function ToString() As String

            Dim sb As New StringBuilder()

            sb.AppendFormat(“ID: {0}{1}”, Me._id, ControlChars.NewLine)

            sb.AppendFormat(“Nombre: {0}{1}”, Me._nombre, ControlChars.NewLine)

            sb.AppendFormat(“Apellido: {0}{1}”, Me._apellido, ControlChars.NewLine)

            sb.AppendFormat(“Direccion: {0}{1}”, Me._direccion, ControlChars.NewLine)

            sb.AppendLine()

            Return sb.ToString()

        End Function

    End Class

End Module

Espero que les sirva!

Happy Coding!


Acciones

Información

Deja un comentario