2015-10-04 49 views
0

的兩個成員之後啓動類參數,起初我很抱歉,如果它是一個愚蠢的問題。我是.net新手。在啓動類別

我有一個移動類是這樣的:

public class Mobile 
{ 
    private string region; 
    private string number; 
    private bool regionIsValid; 
    private bool numberContentIsValid; 
    private string internationalNumber; 

    public string Region 
    { 
     set { region = value.ToUpper(); ConvertNumber(); } 
     get { return region; } 
    } 
    public string Number 
    { 
     set { number = value; ConvertNumber(); } 
     get { return number; } 
    } 
    public Interface Reason 
    { 
     set { reason = value; } 
     get { return reason; } 
    } 
    public bool NumberContentIsValid 
    { 
     set { numberContentIsValid = value; } 
     get { return numberContentIsValid; } 
    } 
    public string InternationalNumber 
    { 
     set { internationalNumber = value; } 
     get { return internationalNumber; } 
    } 

    public void ConvertNumber() 
    { 
     var sw = Stopwatch.StartNew(); 

     try 
     { 
      if (Number == null || Region == null || DoesNumberContentValid() == false || NumberContentIsValid == false) 
       return; 

      var phoneUtil = PhoneNumberUtil.GetInstance(); 
      var phone = phoneUtil.Parse(Number, Region); 
      InternationalNumber = phoneUtil.Format(phone, PhoneNumberFormat.INTERNATIONAL); 

      Log.GetLogger().Info(Format.CreateLogMessage("converting mobile number completed successfully.", sw.Elapsed.TotalMilliseconds)); 
      return; 
     } 
     catch (Exception ex) 
     { 
      Log.GetLogger().Error(Format.CreateLogMessage(ex.ToString(), sw.Elapsed.TotalMilliseconds)); 
      return; 
     } 

    } 
    public bool DoesNumberContentValid() 
    { 
     var sw = Stopwatch.StartNew(); 
     var len = new Regex(@"^.{2,20}$"); //length at least 6 characters and maximum of 20 
     var digit = new Regex(@"^[0-9]*$"); //a string consisting only of numbers 

     if (number.StartsWith(@"+") == true) number = number.Substring(1); 
     number = number.Replace(" ", string.Empty); 

     try 
     { 
      if (!len.IsMatch(Number)) 
      { 
       NumberContentIsValid = false; 
       Reason = new Interface("G00005"); 
      } 
      else if (!digit.IsMatch(Number)) 
      { 
       NumberContentIsValid = false; 
       Reason = new Interface("G00005"); 
      } 
      else if (Region != "IR") 
      { 
       NumberContentIsValid = false; 
       Reason = new Interface("G00005"); 
      } 
      else 
      { 
       var phoneUtil = PhoneNumberUtil.GetInstance(); 
       var mobile = phoneUtil.Parse(Number, Region); 
       if (phoneUtil.IsValidNumber(mobile) != true) 
       { 
        NumberContentIsValid = false; 
        Reason = new Interface("G00005"); 
       } 
       else if (phoneUtil.GetNumberType(mobile).ToString() != "MOBILE") 
       { 
        NumberContentIsValid = false; 
        Reason = new Interface("G00005"); 
       } 
       else 
        NumberContentIsValid = true; 
      } 

      Log.GetLogger().Info(Format.CreateLogMessage("mobile content validation completed successfully.", sw.Elapsed.TotalMilliseconds)); 
      return true; 
     } 
     catch (Exception ex) 
     { 
      Reason = new Interface("G00001", ex.Message); 
      Log.GetLogger().Error(Format.CreateLogMessage(ex.ToString(), sw.Elapsed.TotalMilliseconds)); 
      return false; 
     } 
    } 
} 

我想發起internationalNumber如果NumberRegion都是可用的。請大家看看我的convertnumber()方法:

 public void ConvertNumber() 
    { 
     var sw = Stopwatch.StartNew(); 

     try 
     { 
      if (Number == null || Region == null || DoesNumberContentValid() == false || NumberContentIsValid == false) 
       return; 

      var phoneUtil = PhoneNumberUtil.GetInstance(); 
      var phone = phoneUtil.Parse(Number, Region); 
      InternationalNumber = phoneUtil.Format(phone, PhoneNumberFormat.INTERNATIONAL); 

      Log.GetLogger().Info(Format.CreateLogMessage("converting mobile number completed successfully.", sw.Elapsed.TotalMilliseconds)); 
      return; 
     } 
     catch (Exception ex) 
     { 
      Log.GetLogger().Error(Format.CreateLogMessage(ex.ToString(), sw.Elapsed.TotalMilliseconds)); 
      return; 
     } 

    } 

爲了做到這一點我使用NumberRegion的屬性設置是這樣的:

public string Region 
{ 
    set { region = value.ToUpper(); ConvertNumber(); } 
    get { return region; } 
} 
public string Number 
{ 
    set { number = value; ConvertNumber(); } 
    get { return number; } 
} 

它的工作原理。但它可能會導致意外的錯誤(你意識到如果看看我的DoesNumberContentValid()方法。tips:我必須用number而不是Number) 現在我想知道有沒有更好的方法來做到這一點? (忘記我不能使用班級承包商,因爲我不能同時啓動兩個參數。)

謝謝。

+0

國際號碼屬性總是與其他兩個屬性相關嗎?您確定要允許用戶分別設置國際號碼屬性嗎? –

+0

謝謝親愛的@ Maor Veitsman。是的,國際號碼總是與其他兩個屬性有關,我想單獨分析國際號碼屬性。 – David

+0

可以肯定的是,InternationalNumber屬性應該有一個私人設置者,對吧? –

回答

1

也許這會爲你工作:

public string InternationalNumber 
{ 
    set { internationalNumber = value; } 
    get { return internationalNumber ?? CalcInternationalNumber(); } 
} 

而且功能CalcInternationalNumber:

public string CalcInternationalNumber() 
{ 
    var sw = Stopwatch.StartNew(); 

    try 
    { 
     if (Number == null || Region == null || DoesNumberContentValid() == false || NumberContentIsValid == false) 
      return null; 

     var phoneUtil = PhoneNumberUtil.GetInstance(); 
     var phone = phoneUtil.Parse(Number, Region); 
     var intNum = phoneUtil.Format(phone, PhoneNumberFormat.INTERNATIONAL); 

     Log.GetLogger().Info(Format.CreateLogMessage("converting mobile number completed successfully.", sw.Elapsed.TotalMilliseconds)); 
     return intNum; 
    } 
    catch (Exception ex) 
    { 
     Log.GetLogger().Error(Format.CreateLogMessage(ex.ToString(), sw.Elapsed.TotalMilliseconds)); 
     return null; 
    } 

} 

希望這是你所期待的。

+0

謝謝親愛的@Maor Veitsman。我想是的。 – David

+1

很高興幫助! –