NelloRizzo

[VBNET] Interruttore e lampadina

Feb 15th, 2017
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.69 KB | None | 0 0
  1. Module Module1
  2.  
  3.     Delegate Sub InterruttorePremutoDelegate()
  4.     ' FIRMA STANDARD DI UN EVENTO DOTNET
  5.     Delegate Sub EventHandler(sender As Object, e As EventArgs)
  6.  
  7.     Class Interruttore
  8.         Public Property Nome As String
  9.         Public Event Click As InterruttorePremutoDelegate
  10.  
  11.         Public Sub Premi()
  12.             Console.WriteLine("Qualcuno preme sull'interruttore {0}", Nome)
  13.             RaiseEvent Click()
  14.         End Sub
  15.     End Class
  16.  
  17.     Class Lampadina
  18.         Public Property Nome As String
  19.         Public Property Accesa As Boolean
  20.         Public Sub GestoreClickSuInterruttore()
  21.             Accesa = Not Accesa
  22.             ' If Accesa Then Write("Accesa") Else Write("SPENTA")
  23.             Console.WriteLine("La lampadina {1} รจ {0}",
  24.                               IIf(Accesa, "Accesa", "Spenta"),
  25.                               Nome)
  26.         End Sub
  27.     End Class
  28.  
  29.     Sub GestoreClick()
  30.         Console.WriteLine("La lampadina si accende e si spegne")
  31.     End Sub
  32.  
  33.     Sub Main()
  34.         Dim s As New Interruttore With {.Nome = "Cucina"}
  35.  
  36.         's.Click = AddressOf GestoreClick
  37.  
  38.         s.Premi()
  39.         s.Premi()
  40.         Dim l As New Lampadina With {.Nome = "Cucina"}
  41.         AddHandler s.Click, AddressOf l.GestoreClickSuInterruttore
  42.         s.Premi()
  43.         Dim c As New Lampadina With {.Nome = "Corridoio"}
  44.         AddHandler s.Click, AddressOf c.GestoreClickSuInterruttore
  45.         s.Premi()
  46.         s.Premi()
  47.         Console.WriteLine("Disconnetto la lampadina della cucina")
  48.         RemoveHandler s.Click, AddressOf l.GestoreClickSuInterruttore
  49.         s.Premi()
  50.         s.Premi()
  51.  
  52.  
  53.         Console.ReadLine()
  54.     End Sub
  55.  
  56. End Module
Add Comment
Please, Sign In to add comment