Advertisement
NelloRizzo

Untitled

Apr 24th, 2020
1,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.65 KB | None | 0 0
  1. ''' <summary>
  2. ''' Definizione di un contatto.
  3. ''' </summary>
  4. ''' <remarks>Un contatto ha un nome e un recapito.</remarks>
  5. Public Class Contact
  6.     ''' <summary>
  7.     ''' Nome del contatto.
  8.     ''' </summary>
  9.     Public Property Name As String
  10.     ''' <summary>
  11.     ''' Recapito del contatto.
  12.     ''' </summary>
  13.     Public Property Delivery As Address
  14.  
  15.     ''' <summary>
  16.     ''' Rappresentazione del contatto come stringa.
  17.     ''' </summary>
  18.     Public Overrides Function ToString() As String
  19.         Dim description As New System.Text.StringBuilder ' devo restituire una stringa costruita per passaggi successivi
  20.         description.AppendFormat("Nome: {0}", Name) ' aggiungo il nome del contatto
  21.         If Delivery IsNot Nothing Then ' controllo se è stato specificato un valore per il recapito
  22.             description.AppendFormat(" [{0}]", Delivery) ' in questo caso lo aggiungo al risultato
  23.         End If
  24.         Return description.ToString ' restituisco la stringa costruita passo passo
  25.     End Function
  26. End Class
  27. ''' <summary>
  28. ''' Un qualsiasi indirizzo.
  29. ''' </summary>
  30. ''' <remarks>Serve solo a dare un nome alla FAMIGLIA di classi che rappresentano gli indirizzi.</remarks>
  31. Public MustInherit Class Address
  32.  
  33. End Class
  34. ''' <summary>
  35. ''' Un numero telefonico.
  36. ''' </summary>
  37. Public Class PhoneNumber
  38.     Inherits Address ' è un indirizzo
  39.  
  40.     ''' <summary>
  41.     ''' Numero di telefono.
  42.     ''' </summary>
  43.     Public Property Phone As String
  44.     ''' <summary>
  45.     ''' Descrizione di un numero di telefono.
  46.     ''' </summary>
  47.     Public Overrides Function ToString() As String
  48.         Return String.Format("Telefono: {0}", Phone)
  49.     End Function
  50. End Class
  51. ''' <summary>
  52. ''' Un indirizzo email.
  53. ''' </summary>
  54. Public Class EMailAddress
  55.     Inherits Address ' anche questo è un indirizzo
  56.     ''' <summary>
  57.     ''' Indirizzo di posta elettronica.
  58.     ''' </summary>
  59.     Public Property Email As String
  60.     ''' <summary>
  61.     ''' Descrizione di un indirizzo di posta elettronica.
  62.     ''' </summary>
  63.     Public Overrides Function ToString() As String
  64.         Return String.Format("Email: {0}", Email)
  65.     End Function
  66. End Class
  67. ''' <summary>
  68. ''' Un indirizzo postale.
  69. ''' </summary>
  70. Public Class SnailmailAddress
  71.     Inherits Address ' anch'esso è un indirizzo
  72.     ''' <summary>
  73.     ''' Via.
  74.     ''' </summary>
  75.     Public Property Street As String
  76.     ''' <summary>
  77.     ''' Città.
  78.     ''' </summary>
  79.     Public Property City As String
  80.     ''' <summary>
  81.     ''' Descrizione di un indirizzo postale.
  82.     ''' </summary>
  83.     Public Overrides Function ToString() As String
  84.         Return String.Format("Indirizzo postale: {0} - {1}", Street, City)
  85.     End Function
  86. End Class
  87. Module Module1
  88.  
  89.     Sub Main()
  90.         ' i miei contatti...
  91.         Dim paperino As New Contact With {
  92.             .Name = "Paperino",
  93.             .Delivery = New EMailAddress() With {.Email = "paperino@paperopoli.net"}
  94.             }
  95.         Dim topolino As New Contact With {
  96.             .Name = "Topolino",
  97.             .Delivery = New PhoneNumber With {.Phone = "1234567"}
  98.             }
  99.         Dim paperone As New Contact With {
  100.             .Name = "Paperon De' Paperoni",
  101.             .Delivery = New SnailmailAddress With {.City = "Paperopoli", .Street = "via del Deposito, n. 1"}
  102.             }
  103.         ' ...aggiunti alla lista          ---+
  104.         '                                    |
  105.         '                                    V
  106.         Dim rubrica As New List(Of Contact) From {paperino, topolino, paperone}
  107.         '          il From serve per inizializzare una lista con un "elenco di inizializzazione"
  108.  
  109.         ' consumo la lista
  110.         For Each item As Contact In rubrica
  111.             Console.WriteLine(item)
  112.         Next
  113.     End Sub
  114.  
  115. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement