zipfeli

Error 1 The name 'NullToString' does not exist in the current context

I receive the above error compiling this email validating class:

using System;

using System.Collections.Generic;

using System.Text;

using System.Text.RegularExpressions;

using System.Net.Mail;

namespace WindowsApplication1

{

class Check

{

public static bool isEmail(string inputEmail)

{

inputEmail = NullToString(inputEmail);

string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +

@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +

@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\] )$";

Regex re = new Regex(strRegex);

if (re.IsMatch(inputEmail))

return (true);

else

return (false);

}

}

}



Re: Visual C# General Which Namespace contains NullToString ?

cgraus

There is no such thing as nulltostring. Best guess, it looks something like this:

private string NullToString (string s)

{

return (s== null) "" : s;


}






Re: Visual C# General Which Namespace contains NullToString ?

David Hayden

My best guess is that you are attempting to use the new string.IsNullOrEmpty method for parameter checking in your case.

You would do something like

if (string.IsNullOrEmpty(inputEmail))
throw new ArgumentNullException("inputEmail"):

// process as normal...

Regards,

Dave






Re: Visual C# General Which Namespace contains NullToString ?

PJ. van de Sande

The method simply doesn't exist in the current contex, but your method should look like the example below. In .NET 2.0 you can use the String.IsNullOrEmpty method:


public static bool isEmail(string inputEmail)
{
if( inputEmail == null || inputEmail.Lengt == 0 )
{
throw new ArgumentNullException( "inputEmail" );
}

const string expression = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\] )$";

Regex regex = new Regex(strRegex);
return regex.IsMatch(inputEmail);
}







Re: Visual C# General Which Namespace contains NullToString ?

zipfeli

Thank you very much for your answers. Trying the code from PJ I get this error now:

Error 1 The name 'strRegex' does not exist in the current context





Re: Visual C# General Which Namespace contains NullToString ?

cgraus

He's passing in a string which has not been declared anywhere. I assume that he's meant to pass in the string that was an input parameter.






Re: Visual C# General Which Namespace contains NullToString ?

PJ. van de Sande

Sorry, my bad. I wrote it out of the head. But here is a new example


public static bool isEmail(string inputEmail)
{
if( inputEmail == null || inputEmail.Lengt == 0 )
{
throw new ArgumentNullException( "inputEmail" );
}

const string expression = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\] )$";

Regex regex = new Regex(expression);
return regex.IsMatch(inputEmail);
}







Re: Visual C# General Which Namespace contains NullToString ?

Tatworth

Null to String is at http://greatestcodesnippet.blogspot.com/2006/12/nulltoboolean-nulltocurrency.html





Re: Visual C# General Which Namespace contains NullToString ?

Chas75287

No reason to throw an exception: the function is called isEmail. Therefore, instead of throwing an exception if the string is null or empty, just return false, because the purpose of the function is to determine if a string is an email.