I was trying to make a way to send text messages through email. Turns out most providers offer a SMStoEmail.

I have created a class to help me do it.
I want to be sure that i've used the correct way (I've used dictionary).

I needed one dictionary to keep,
[number], [provider] and this is accessed from the UI (or whatever creates the object).

I also have another [provider], [provideremail] dictionary which stores the providers and there respective email.

This way seemed useful since I can check if a provider exists easily, and keep track of emails. I then match the provider with they number, and send the email.

Again, I want to know if using the dictionary is the best way to accomplish this..
Here is my class

Imports System.IO
Imports System.Text.RegularExpressions

vb Code:
  1. Public Class SMS
  2.     Private _numberLength As Integer
  3.     Private _ListOfNumbers As New Dictionary(Of String, String)
  4.     Private _ListOfProviders As New Dictionary(Of String, String)
  5.     Private _phonenumber As String
  6.     Private _FilePath As String
  7.     Private __defaultprovider As String
  8.  
  9. #Region "Properties"
  10.  
  11.     Private Property PhoneNumber() As String
  12.         Get
  13.             Return _phonenumber
  14.         End Get
  15.         Set(ByVal value As String)
  16.             _phonenumber = value
  17.         End Set
  18.     End Property
  19.  
  20.  
  21.     Private Property Length() As Integer
  22.         Get
  23.             Return _numberLength
  24.         End Get
  25.         Set(ByVal value As Integer)
  26.             _numberLength = value
  27.         End Set
  28.     End Property
  29.    
  30.     Private Property ListOfProviders() As Dictionary(Of String, String)
  31.         Get
  32.             Return _ListOfProviders
  33.         End Get
  34.         Set(ByVal value As Dictionary(Of String, String))
  35.             _ListOfProviders = value
  36.         End Set
  37.     End Property
  38.  
  39.  
  40.     Private Property ListOfNumbers() As Dictionary(Of String, String)
  41.         Get
  42.             Return _ListOfNumbers
  43.         End Get
  44.         Set(ByVal value As Dictionary(Of String, String))
  45.             _ListOfNumbers = value
  46.         End Set
  47.     End Property
  48.  
  49.  
  50.     Private Property DefaultProvidor() As String
  51.         Get
  52.             Return __defaultprovider
  53.         End Get
  54.         Set(ByVal value As String)
  55.             __defaultprovider = value
  56.         End Set
  57.     End Property
  58.  
  59.     Public Property FilePath() As String
  60.         Get
  61.             Return _FilePath
  62.         End Get
  63.         Set(ByVal value As String)
  64.             _FilePath = value
  65.         End Set
  66.     End Property
  67. #End Region
  68. #Region "Constructors"
  69.  
  70.     Public Sub New(ByVal df As String)
  71.         Try
  72.             If Not String.IsNullOrEmpty(df) Then
  73.                 DefaultProvidor = df
  74.                 Length = 10
  75.                 LoadProviders()
  76.             Else
  77.                 Throw New Exception
  78.             End If
  79.         Catch ex As Exception
  80.             'log your error
  81.             Console.WriteLine(ex.Message)
  82.         End Try
  83.  
  84.     End Sub
  85.     Public Sub New(ByVal df As String, ByVal l As Integer)
  86.         Try
  87.             If Not String.IsNullOrEmpty(df) Or Integer.Parse(l) Then
  88.                 DefaultProvidor = df
  89.                 Length = l
  90.                 LoadProviders()
  91.             Else
  92.                 Throw New Exception("Default Provider must be a string")
  93.             End If
  94.         Catch ex As Exception
  95.             'handle your exception here.
  96.             Console.WriteLine(ex.Message)
  97.         End Try
  98.        
  99.     End Sub
  100.  
  101.     Public Sub New(ByVal df As String, ByVal fp As String)
  102.         If Not String.IsNullOrEmpty(fp) Or Not String.IsNullOrEmpty(fp) Then
  103.             DefaultProvidor = df
  104.             Length = 10
  105.             FilePath = fp
  106.         Else
  107.             Throw New Exception("Your filepath/Defaultprovider is invalid")
  108.         End If
  109.     End Sub
  110. #End Region
  111. #Region "Public Functions"
  112.  
  113.  
  114.     'public functions.
  115.     Public Sub AddSingle(ByVal number As String, ByVal provider As String)
  116.         PhoneNumber = number
  117.         If CheckNumber() Then
  118.             If DoesProviderExist(provider) Then
  119.                 ListOfNumbers.Add(PhoneNumber, provider)
  120.             End If
  121.         End If
  122.     End Sub
  123.  
  124.     Public Sub AddSingle(ByVal number As String) 'uses the default provider
  125.         PhoneNumber = number
  126.         If CheckNumber() Then
  127.             ListOfNumbers.Add(PhoneNumber, DefaultProvidor)
  128.         End If
  129.     End Sub
  130.  
  131.     Public Sub Delete(ByVal number As String)
  132.         ListOfNumbers.Remove(number)
  133.     End Sub
  134.  
  135.     Public Sub SendAll()
  136.  
  137.         Dim fullEmail As String = String.Empty
  138.  
  139.         For Each num In ListOfNumbers
  140.             Dim pro As String = String.Empty
  141.             If ListOfProviders.TryGetValue(num.Value, pro) Then
  142.                 Dim temp As String = pro.Replace("number", num.Key)
  143.                 SendEmail(temp)
  144.             Else
  145.                 Console.WriteLine("Sendall failed")
  146.             End If
  147.         Next
  148.     End Sub
  149. #End Region
  150. #Region "Private functions"
  151.  
  152.  
  153.     Private Sub SendEmail(ByVal email As String)
  154.         'send your email here.
  155.         Console.WriteLine(email)
  156.     End Sub
  157.  
  158.     Private Sub LoadProviders()
  159.         Dim reader As New IO.StreamReader(FilePath) ' create new streamreader
  160.         Do Until reader.EndOfStream
  161.             Dim line As String = reader.ReadLine 'read single line
  162.             Dim proArray() As String = line.Split(",") 'split the line
  163.             ListOfProviders.Add(proArray(0).Trim, proArray(1).Trim) 'store both parts into dictionary
  164.         Loop
  165.         reader.Close()
  166.     End Sub
  167.     'this method parses the number, and removes any dashses.
  168.     Private Function CheckNumber() As Boolean
  169.         Dim ph As String = PhoneNumber
  170.         'remove one or zero.
  171.         If ph.StartsWith(0) Or ph.StartsWith(1) Then
  172.             ph = ph.Remove(0, 1)
  173.         End If
  174.  
  175.  
  176.         'remove all dashes and check for length
  177.         ph = ph.Replace("-", String.Empty)
  178.         If ph.Length <> Length Or ph.Length + 1 <> Length + 1 Then
  179.             Console.WriteLine("Length didnt match")
  180.             Return False
  181.         End If
  182.  
  183.         ' Check a (USA style) telephone number
  184.         '1-800-201-1929 (true)
  185.         '800-201-1929 (true)
  186.         '201-1929 (false)
  187.         Dim myRegex As New Regex( _
  188.            "^1?\s*-?\s*(\d{3}|\(\s*\d{3}\s*\))\s*-?\s*\d{3}\s*-?\s*\d{4}$")
  189.  
  190.         If Not myRegex.IsMatch(ph) Then
  191.             Console.WriteLine("not in right format")
  192.             Return False
  193.         End If
  194.  
  195.         PhoneNumber = ph
  196.         Console.WriteLine(PhoneNumber)
  197.         Return True
  198.     End Function
  199.  
  200.  
  201.     Private Function DoesProviderExist(ByVal name As String) As Boolean
  202.         If ListOfProviders.ContainsKey(name.Trim) Then
  203.             Console.WriteLine("provider exists: " & name)
  204.  
  205.             Return True
  206.         Else
  207.             Console.WriteLine("provider don't exist")
  208.             Return False
  209.         End If
  210.     End Function
  211. #End Region
  212. End Class