Format() in Public Shared Function

M

Matt

I'm trying to write a shared function that will convert all my phone numbers
from format 1234567890 to (123) 456-7890.

Here is my function that I wrote:


Public Shared Function CheckPhone(byVal val as String) as String
dim strCheckPhone as String
If val = "" Then
strCheckPhone = ""
Elseif val = nothing then
strCheckPhone = ""
else
strCheckPhone = Format(Convert.ToDouble(val), "(###) ###-####")
End If

return strCheckPhone
End Function

Here is my compile command:

c:\windows\microsoft.net\framework\v1.1.4322\vbc.exe /t:library
/r:System.Data.dll /r:System.dll functions.vb

Here is what I am importing:

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Imports System.Text.UTF8Encoding
Imports System.configuration
Imports System.Security.Cryptography
Imports System.Web
Imports System.Text.Encoding

(There are some other functions in this file, so I know that most of those
don't relate to this function.)

And here are the errors I receive:

C:\Documents and Settings\m\Desktop\functions.vb(180) : error BC30451:

******************************************************************
Name 'Format' is not declared.


strCheckPhone = Format(Convert.ToDouble(val), "(###) ###-####")
~~~~~~
******************************************************************

C:\Documents and Settings\m\Desktop\functions.vb(180) : error BC30561:


******************************************************************
'Convert' is ambiguous, imported from the namespaces or types
'System.Text.Encod
ing, System'.

strCheckPhone = Format(Convert.ToDouble(val), "(###) ###-####")
~~~~~~~
******************************************************************

Thanks for your help.

Matt
 
M

Matt

Thanks.

What about this one?

'Convert' is ambiguous, imported from the namespaces or types
'System.Text.Encoding, System'.

strCheckPhone = Format(Convert.ToDouble(val), "(###) ###-####")
~~~~~~~

Thanks again!

Matt
 
M

Matt

I got this to work, but what I had to do was System.Convert.ToDouble.

I don't understand why I need to do this since I was already importing
System.

Matt
 
D

Derek Harmon

Matt said:
What about this one?

'Convert' is ambiguous, imported from the namespaces or types
'System.Text.Encoding, System'.

You should remove the,

Imports System.Text.Encoding

statement because Encoding is a Class, not a Namespace, and you've already
imported System.Text (which is the Namespace containing the Encoding class)
so there's no benefit to importing the Class individually.

The error is telling you that the VB.NET compiler can't tell whether you are
referring to the System.Convert Class or the Encoding.Convert Method (it's
System.Convert; as soon as you remove the extraneous Import it'll cease
being ambiguous.)


Derek Harmon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top