locked
How to call bool method? RRS feed

  • Question

  • User647458646 posted

    I am trying to test my bool method with string parameter and would like to test my string against the "isValidEmailDomain" method.  

    This is what I have so far:

       public string test()
            {
                string mail = "########";
                
                IsValidEmailDomain(mail);
    
            }

    Is their some example i could follow to create this test method. 

     public static bool IsValidEmailDomain(MailAddress address)
            {
                if (address == null) return false;
    
                var response = DnsClient.Default.Resolve(address.Host, RecordType.Mx);
                if (response == null || response.AnswerRecords == null) return false;
    
                return response.AnswerRecords.OfType<MxRecord>().Any();
            }
    
            public static bool IsValidEmailDomain(string address)
            {
                if (string.IsNullOrWhiteSpace(address)) return false;
    
                MailAddress theAddress;
                try
                {
                    theAddress = new MailAddress(address);
                }
                catch (FormatException)
                {
                    return false;
                }
    
                return IsValidEmailDomain(theAddress);
            }

    Many thanks for your help.

    Wednesday, October 1, 2014 9:03 AM

All replies

  • User-1618234021 posted

    This is how it should be:

    public string test()
            {
                string mail = "structuredcreditinvestor.com";
                
                if (IsValidEmailDomain(mail))
                {
                     //email was valid, put success code here
                }
                else
                {
                     //email was invalid, put error code here
                }
            }

    Wednesday, October 1, 2014 9:17 AM
  • User647458646 posted

    Thank you for your response and help.  

    I am currently getting a invalid error from my test() method, but I would like to know, how can I catch the error from my "IsValidEmailDomain" method into my "test" method:

      public static bool IsValidEmailDomain(MailAddress address)
            {
                // If it throws an exception, you'll need a try..catch block.
                try
                {
                    if (address == null) return false;
                    var response = DnsClient.Default.Resolve(address.Host, RecordType.Mx);
                    if (response == null || response.AnswerRecords == null) return false;
    
                    return response.AnswerRecords.OfType<MxRecord>().Any();
    
                }
                catch (FormatException)
                {
                    return false;
                }
            }
    
            public static bool IsValidEmailDomain(string address)
            {
                if (string.IsNullOrWhiteSpace(address)) return false;
    
                MailAddress theAddress;
                try
                {
                    theAddress = new MailAddress(address);
                }
                catch (FormatException)
                {
                    return false;
                }
    
                return IsValidEmailDomain(theAddress);
            }

    I would like to know, How to return catch result from IsValidEmailDomain() method, into my Test() method:

     public static string test()
            {
                string mail = "###########";
    
                //string result = "";
    
                //string error = "notValid";
    
                if (IsValidEmailDomain(mail))
                {
                    return mail;
                }
                else
                {
                    ///How to return catch from IsValidEmailDomain() method.
                }
    
            }

    Any hint or suggestion would be most appreciated.  Thank you

    Thursday, October 2, 2014 5:05 AM
  • User-1618234021 posted

    Hi

    IsValidEmailDomain is returning a boolean, either true or false. The test method return type is string. Currently if the email is correct, you are returning the the same email from your test method. In case of wrong email, either you can return null or you can send back error message as string. Like:

    public static string test()
            {
                string mail = "###########";
    
                //string result = "";
    
                //string error = "notValid";
    
                if (IsValidEmailDomain(mail))
                {
                    return mail;
                }
                else
                {
                    
                    return null;
    
                }
    
            }

    Thursday, October 2, 2014 6:47 AM
  • User647458646 posted

    Apology for the late reply. 

    I need figure out why my address is invalid(false) by catching the error from the "IsValidEmailDomain" method, into my test method.  Could someone, please provide some asssitance, into how can I achieve this please. 

    many thanks. 

    Thursday, October 2, 2014 10:42 AM