NelloRizzo

[VBNET] Introduzione ai delegati

Feb 14th, 2017
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.23 KB | None | 0 0
  1. Module Module1
  2.  
  3.     Sub SortAsc(a() As Integer)
  4.         For index As Integer = 0 To a.Length - 2
  5.             For scan As Integer = index + 1 To a.Length - 1
  6.                 'If a(index) > a(scan) Then
  7.                 If ConfrontaAsc(a(index), a(scan)) Then
  8.                     Dim temp As Integer = a(index)
  9.                     a(index) = a(scan)
  10.                     a(scan) = temp
  11.                 End If
  12.             Next
  13.         Next
  14.     End Sub
  15.  
  16.     Sub SortDesc(a() As Integer)
  17.         For index As Integer = 0 To a.Length - 2
  18.             For scan As Integer = index + 1 To a.Length - 1
  19.                 ' If a(index)< a(scan) Then
  20.                 If ConfrontaDesc(a(index), a(scan)) Then
  21.                     Dim temp As Integer = a(index)
  22.                     a(index) = a(scan)
  23.                     a(scan) = temp
  24.                 End If
  25.             Next
  26.         Next
  27.     End Sub
  28.  
  29.     ' รจ un NUOVO tipo di dato che rappresenta TUTTE le funzioni che accettano in
  30.     ' ingresso due interi e restituiscono un Boolean
  31.     Delegate Function CompareDelegateFunction(x As Integer, y As Integer) As Boolean
  32.  
  33.     Sub Sort(a() As Integer, func As CompareDelegateFunction)
  34.         For index As Integer = 0 To a.Length - 2
  35.             For scan As Integer = index + 1 To a.Length - 1
  36.                 ' Invocazione del DELEGATO "func"
  37.                 If func(a(index), a(scan)) Then
  38.                     Dim temp As Integer = a(index)
  39.                     a(index) = a(scan)
  40.                     a(scan) = temp
  41.                 End If
  42.             Next
  43.         Next
  44.     End Sub
  45.  
  46.     Function ConfrontaAsc(a As Integer, b As Integer) As Boolean
  47.         Return a > b
  48.     End Function
  49.     Function ConfrontaDesc(a As Integer, b As Integer) As Boolean
  50.         Return a < b
  51.     End Function
  52.     Function ConfrontaRandom(a As Integer, b As Integer) As Boolean
  53.         If a Mod 2 = 0 Then
  54.             Return a > b
  55.         Else
  56.             Return b > a
  57.         End If
  58.     End Function
  59.  
  60.     Sub Print(a() As Integer)
  61.         For Each n As Integer In a
  62.             Console.WriteLine(n)
  63.         Next
  64.     End Sub
  65.     Sub Main()
  66.         Dim numbers() = {2341, 6, 1234, 47, 123, 1234, 78, 1234, 78, 1234, 48, 968234}
  67.         Console.WriteLine("Array iniziale:")
  68.         Print(numbers)
  69.         Console.WriteLine("Array ordinato in maniera crescente:")
  70.         SortAsc(numbers)
  71.         Print(numbers)
  72.         Console.WriteLine("Array ordinato in maniera decrescente:")
  73.         SortDesc(numbers)
  74.         Print(numbers)
  75.         Console.WriteLine("Array ordinato in maniera crescente con delegato:")
  76.         ' invocazione di SORT con PASSAGGIO della FUNZIONE DELEGATA ConfrontaAsc
  77.         Sort(numbers, AddressOf ConfrontaAsc)
  78.         Print(numbers)
  79.         Console.WriteLine("Array ordinato in maniera decrescente con delegato:")
  80.         ' invocazione di SORT con PASSAGGIO della FUNZIONE DELEGATA ConfrontaDesc
  81.         Sort(numbers, AddressOf ConfrontaDesc)
  82.         Print(numbers)
  83.         Console.WriteLine("Array ordinato in maniera casuale:")
  84.         ' invocazione di SORT con PASSAGGIO della FUNZIONE DELEGATA ConfrontaRandom
  85.         Sort(numbers, AddressOf ConfrontaRandom)
  86.         Print(numbers)
  87.         Console.ReadLine()
  88.     End Sub
  89.  
  90. End Module
Add Comment
Please, Sign In to add comment