Click or drag to resize
ZenophSMSparsePhoneNumber Method
Parses a phone number into a specific international format.

Namespace: Zenoph.SMSLib
Assembly: Zenoph.SMSLib (in Zenoph.SMSLib.dll) Version: 2015.9.30.0 (2015.09.30.0)
Syntax
public string parsePhoneNumber(
	string phonenum
)

Parameters

phonenum
Type: SystemString
The phone number to be parsed into specific international format.

Return Value

Type: String
Returns the phone number in international format for submission. If the phone number is invalid or not allowed on user routes, the return value will be null.
Exceptions
ExceptionCondition
Exception Thrown if authentication has not been performed for user routes to be loaded or when phonenum is invalid.
Remarks

The method parses the phone number and returns it in a specific international format. For example, if the phone number is given as +231xxxxxxxxx or 0023320xxxxxxx, the method will parse the phone number and return it in the form 231xxxxxxxxx or 23320xxxxxx. Hence, phone numbers can be specified with the leading +, 00, or without them.

Phone numbers can also be specified in local format with or without the leading zero (0). For example, 0246xxxxxx, 246xxxxxx In this case, the method will automatically format the phone numbers in international format by choosing the default international dial code set to be used. If no international dial code has been set for the current session, the default international dial code set for user account will be prepended to phone numbers given in local number format.

The method also ensures that phone numbers passed to it are valid and that they are allowed on user routes. If a phone number is invalid or not allowed on user routes, the method returns null.

Examples

The following code demonstrates the capabilities of this method.

using System;
using System.Collections.Generic;
using Zenoph.SMSLib;
using Zenoph.SMSLib.Enums;

public class Program
{
    public static void Main()
    {
        try
        {
            // Initialise SMS object and perform authentication.
            ZenophSMS sms = new ZenophSMS();
            sms.setUser("account_login");
            sms.setPassword("account_password");
            sms.authenticate();

            /*
             * add phone numbers in local number format, not forgetting
             * that null can be returned if the number is invalid or not allowed.
             * Assuming the default international dial code set is +233.
             * In each case, the return must be checked whether is null or not before being added.
             *
             * If there is the need to check the return value before adding the phone number,
             * the return value can be saved and the check being done
             */
            string num = sms.parsePhoneNumber("0249999999");  // will return 233249999999

            if (!string.IsNullOrEmpty(num))
                sms.addRecipient(num);

            /*
             * Or we can add the destination on the fly. If the return value is null, the throwing of
             * Exception will be ignored since the second argument to addRecipient is true.
             */
            sms.addRecipient(sms.parsePhoneNumber("249888888"), true);  // will add 233249888888 if route activated.

            // the international dial code to be used can even be changed for adding
            // destinations specified in local number format.
            sms.setDefaultDialCode("+234");     // to Nigeria, must already be activated for account

            // add phone numbers.
            sms.addRecipient(sms.parsePhoneNumber("0807777777"), true);    // will add 234807777777 if route activated.
            sms.addRecipient(sms.parsePhoneNumber("806333333"), true);     // will add 234806333333 if route activated.

            /*
             * phone numbers can also be specified in international format. In this case
             * the international dial code that has been set is not used. Also ignore throwing of Exception.
             */
            sms.addRecipient(sms.parsePhoneNumber("233203111111"), true);    // will add 233203111111 if route activated.
            sms.addRecipient(sms.parsePhoneNumber("+233509888888"), true);   // will add 233509888888 if route activated.
            sms.addRecipient(sms.parsePhoneNumber("00233531666666"), true);  // will add 233531666666 if route activated.

            // and even more, set international dial code and local number.
            string dialcode = "00233";    // international dial code.
            num = "0573010101";           // in local number format.

            // do concatenation.
            num = dialcode + num;     // num will now be 002330573010101

            // the following method call will also take away the zero that precedes the local number.
            sms.addRecipient(sms.parsePhoneNumber(num), true);   // will add 233573010101 if route activated.
        }

        catch (SMSException sex)
        {
            Console.WriteLine("Error: " + sex.Message);
        }

        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }
}
See Also