Ruby vs VB.net way


SUBMITTED BY: Guest

DATE: July 31, 2014, 10:19 a.m.

FORMAT: C#

SIZE: 1.6 kB

HITS: 24913

  1. 'The Ruby way
  2. 'Socket.ip_address_list.select(&:ipv4?).collect(&:ip_address)
  3. 'Amateur's way: With indexed loop
  4. Function Get_Local_IPv4_Addresses1() As String()
  5. Dim All_Addresses() As IPAddress
  6. All_Addresses = Dns.GetHostAddresses(Dns.GetHostName)
  7. Dim Result() As String = {}
  8. For i = 0 To All_Addresses.Length - 1
  9. If All_Addresses(i).AddressFamily = InterNetwork Then
  10. ReDim Preserve Result(Result.Length)
  11. Result(Result.Length - 1) = All_Addresses(i).ToString
  12. End If
  13. Next
  14. Return Result
  15. End Function
  16. 'Professional's way. With "for each" loop. No need to declare array to hold results
  17. Function Get_Local_IPv4_Addresses2() As String()
  18. Dim Result() As String = {}
  19. For Each ip In Dns.GetHostAddresses(Dns.GetHostName())
  20. If ip.AddressFamily = Sockets.AddressFamily.InterNetwork Then
  21. ReDim Preserve Result(Result.Length)
  22. Result(Result.Length - 1) = ip.ToString()
  23. End If
  24. Next
  25. Return Result
  26. End Function
  27. 'Expert's way. With generic collections and lambda expressions
  28. Function Get_Local_IPv4_Addresses3() As String()
  29. Dim All_Addresses() As IPAddress
  30. All_Addresses = Array.FindAll(Dns.GetHostAddresses(Dns.GetHostName()), Function(ip) ip.AddressFamily = InterNetwork)
  31. Return Array.ConvertAll(All_Addresses, Function(ip) ip.ToString())
  32. End Function

comments powered by Disqus