Avatar of deleyd
deleyd
Flag for United States of America asked on

Trying to use libphonenumber-csharp NuGet package

I would like to format a phone number. The program could be run anywhere in the world, so should format phone number for local standards.

I found the C# port of Google's https://code.google.com/p/libphonenumber/. The C# version is at https://bitbucket.org/pmezard/libphonenumber-csharp

I got NuGet installed, and did:

PM> Install-Package libphonenumber-csharp

It added a "packages.config" file to my project:
<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="libphonenumber-csharp" version="4.10.0.62" targetFramework="net40-Client" />
</packages>

Open in new window

and that's as far as I've got. From here I'm not sure how to access the functionality of the package. The tests in the source code say I should be able to do something like:
            PhoneNumberInfo phoneInfo = null;

            phoneInfo = LibPhoneNumber.DoFormatAndFindRegion(@"+41 (0)31 388 10 10");

Open in new window


One thing is my project is Visual Basic VB.net and this is a C# package.
.NET ProgrammingC#Visual Basic.NET

Avatar of undefined
Last Comment
deleyd

8/22/2022 - Mon
kaufmed

One thing is my project is Visual Basic VB.net and this is a C# package.
That doesn't matter. The project could be written in IronPython, and you would still be able to use it (provided the version of .NET in use is compatible). When .NET languages are compiled to assemblies, such assemblies contain intermediate language (known as MSIL--Microsoft Intermediate Language). Any .NET language is compiled to this. This allows the .NET runtime to run code that was written in any .NET language.

As to your issue, are you importing the namespace at the top of your code file?
deleyd

ASKER
I'm not sure what the namespace is I need to import. I tried various Import statements but Visual Studio complained about all of them. I tried:
Imports libphonenumber-csharp
Imports libphonenumber.csharp
Imports libphonenumber
Imports GlauxSoft.Phone.NumberUtil

Open in new window

kaufmed

Which test are you looking at (i.e. the stuff you mentioned in your original post)?
Your help has saved me hundreds of hours of internet surfing.
fblack61
deleyd

ASKER
I found it. (Yet still not sure how to use it.) Tests are at:

https://bitbucket.org/pmezard/libphonenumber-csharp/src/f5236354a379/csharp/PhoneNumbers.Test?at=csharp

I can do:
Imports PhoneNumbers

Open in new window

with no complaints.

But still not sure what I can do. I can almost do:
Dim formatter As AsYouTypeFormatter = phoneUtil.GetAsYouTypeFormatter("ZZ")

Open in new window

but Visual Studio complains it doesn't know what phoneUtil is. I'm trying to find an example of where that gets instantiated.
ASKER CERTIFIED SOLUTION
Nasir Razzaq

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
deleyd

ASKER
I've had some success with the following code:
        Dim phoneUtil As PhoneNumberUtil = PhoneNumberUtil.GetInstance()
        Dim formatter As AsYouTypeFormatter = phoneUtil.GetAsYouTypeFormatter("US")
        Dim x As String
        'x = formatter.InputDigit("1"c)
        x = formatter.InputDigit("8"c)
        x = formatter.InputDigit("0"c)
        x = formatter.InputDigit("5"c)
        x = formatter.InputDigit("5"c)
        x = formatter.InputDigit("5"c)
        x = formatter.InputDigit("5"c)
        x = formatter.InputDigit("1"c)
        x = formatter.InputDigit("2"c)
        x = formatter.InputDigit("3"c)
        x = formatter.InputDigit("4"c)

Open in new window

I guess as with much of great code there is little documentation making the code much less useable.
deleyd

ASKER
(I'm having trouble closing this question. Website technical issues.)

I found some of the solution myself, however comments by others were helpful in jogging my imagination as I sought a creative solution.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.