.Net 3.5 Implementation of String.IsNullOrWhitespace with Code Contracts - c#

I'm attempting to use Contracts in my .Net 3.5 (C#) project. I found where I had written something like if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(s.Trim())) throw new ArgumentException("Required", "s"); at the beginning of methods.
I could keep them there and use legacy contracts. I could change it to Contract.Requires(s!= null && string.IsNullOrEmpty(s.Trim()));. But besides not being very sure about the correctness or performance of the Whitespace part of the condition, those are lame (opinion).
Instead, I tried to encapsulate the condition in a method for use in Contracts. Problem is that I still can't figure out how to describe the Whitespace part declaratively. The static checker finds several issues depending on how I write it.
I'd like to hear comments about the appropriateness of data content checks such as this one for whitespace AS WELL AS a correct definition.
public static class str
{
[Pure]
public static bool IsNullOrWhitespace(string s)
{
Contract.Ensures(s != null || Contract.Result<bool>());
Contract.Ensures((s != null && !Contract.ForAll<char>(s, c => char.IsWhiteSpace(c))) || Contract.Result<bool>());
if (s == null) return true;
for (var i = 0; i < s.Length; i++)
{
if (!char.IsWhiteSpace(s, i))
{
Contract.Assume(!Contract.ForAll<char>(s, c => char.IsWhiteSpace(c)));
return false;
}
}
return true;
}
}
This might be sloppy, but my initial experimentation method is here. The Asserts on a, b, and c are failing or are unproven.
static void Test()
{
string a = null;
string b = "";
string c = " ";
string d = "xyz";
string e = " xyz ";
if (!str.IsNullOrWhitespace(a))
{
Contract.Assert(a != null);
a = a.Trim();
}
if (!str.IsNullOrWhitespace(b))
{
Contract.Assert(b != null && b != "");
b = b.Trim();
}
if (!str.IsNullOrWhitespace(c))
{
Contract.Assert(c != null && c != " ");
c = c.Trim();
}
if (!str.IsNullOrWhitespace(d))
{
d = d.Trim();
}
if (!str.IsNullOrWhitespace(e))
{
e = e.Trim();
}
}

Here's the built-in contract from .NET 4:
Contract.Ensures(
(Contract.Result<bool>() && ((str == null) || (str.Length == 0)))
? ((bool) true)
: ((Contract.Result<bool>() || (str == null))
? ((bool) false)
: ((bool) (str.Length > 0)))
, null, "Contract.Result<bool>() && (str == null || str.Length == 0) ||\r\n!Contract.Result<bool>() && str != null && str.Length > 0");
Despite its complexity, you can see that there is no mention of whitespace. I've tried several times, but I can't get something working that will satisfy the static checker.
There appear to be a couple of issues stopping me from writing it, so I've filed some questions on the Code Contracts forum.
My contract was:
Contract.Ensures(Contract.Result<bool>() ==
(s == null || Contract.Exists(s, char.IsWhiteSpace)))

Related

How can I pull a repeated where clause expression from linq into a function?

I have a large project where I have dozens of linq statements where I am looking for a matching record by checking several fields to see if they match or both field and compared field are null.
var testRecord = new { firstField = "bob", secondField = (string)null, thirdField = "ross" };
var matchRecord = dataContext.RecordsTable.FirstOrDefault(vi =>
(vi.first == testRecord.firstField || ((vi.first == null || vi.first == string.Empty) && testRecord.firstField == null))
&& (vi.second == testRecord.secondField || ((vi.second == null || vi.second == string.Empty) && testRecord.secondField == null))
&& (vi.third == testRecord.thirdField || ((vi.third == null || vi.third == string.Empty) && testRecord.thirdField == null)));
//do stuff with matchRecord
Ideally I would replace all that duplicated code (used around 50 times across the system I'm working on) with something like the following
Expression<Func<string, string, bool>> MatchesOrBothNull = (infoItem, matchItem) => (
infoItem == matchItem || ((infoItem == null || infoItem == string.Empty) && matchItem == null));
var matchRecord = dataContext.RecordsTable.FirstOrDefault(vi =>
MatchesOrBothNull(vi.first, testRecord.firstField)
&& MatchesOrBothNull(vi.second, testRecord.secondField)
&& MatchesOrBothNull(vi.third, testRecord.thirdField));
//do stuff with matchRecord
My question is two-fold: First, is there a matched or both null function already available? (I've looked without luck).
Second, the code block above compiles, but throws a "no supported translation to sql" error, is there a way to have a function in the where clause? I know that there is a translation because it works if I don't pull it into the function. How can I get that translated?
First of all you can check whether string is null or empty with single code which is called : String.IsNullOrEmpty(vi.first). You need a method like this one :
public bool MatchesOrBothNull(string first,string second){
if(first==second||String.IsNullOrEmpty(first)||String.IsNullOrEmpty(second))
return true;
else return false;
}
You can use it in where clause
var matchRecord = dataContext.RecordsTable.Where(vi =>
MatchesOrBothNull(vi.first, testRecord.firstField)
&& MatchesOrBothNull(vi.second, testRecord.secondField)
&& MatchesOrBothNull(vi.third, testRecord.thirdField)
).FirstOrDefault();

Due to LINQ Retrieving of Record data's i have this error: NullReferenceException was unhandled by user code

private void getUserLoginDepartment(string AccessID, string UserPROFid)
{
try
{
DBWPAccountRecordsDataContext DBACCOUNT = new DBWPAccountRecordsDataContext();
var query = (from i in DBACCOUNT.WP_UserAccessPorts
join
z in DBACCOUNT.WP_Departments on i.AccessPortID equals z.Dept_ID
where i.AccessPortID == AccessID && i.ProfileUser_ID == UserPROFid
select new
{
PORT1 = i.AccessPoint1,
PORT2 = i.AccessPoint2,
PORT3 = i.AccessPoint3,
PORT4 = i.AccessPoint4,
DEPT = z.Dept_DESC,
DEPTPORT = z.Dept_PortNo
}).FirstOrDefault();
if (query.PORT1.ToString() != null || query.PORT1.ToString() != string.Empty)
{ Session["Port1"] = query.PORT1; }
else { Session["Port1"] = ""; }
if (query.PORT2.ToString() != null || query.PORT2.ToString() != string.Empty)
{ Session["Port2"] = query.PORT2; }
else { Session["Port2"] = ""; }
if (query.PORT3.ToString() != null || query.PORT3.ToString() != string.Empty)
{ Session["Port3"] = query.PORT3; }
else { Session["Port3"] = ""; }
if (query.PORT4.ToString() != null || query.PORT4.ToString() != string.Empty)
{ Session["Port4"] = query.PORT4; }
else { Session["Port4"] = ""; }
}
finally
{
}
}
The Error occures when i reach break point 1st IF Statement the record on my database shows that its not empty which its value is "WebAdmin" but then suppost to be it should pick it up and store it to the Session["PORT1"] that i have made is there something i missed or i'm doing it wrong on my linq Query. NOTE:*This is an ASP.NET C# Application
EDIT 10/2/2013 0420PM:
It's still an Error After using that method sir.
1) you should check query for null when you use FirstOrDefault
2) you need to check each PORTX for null
3) use string.IsNullOrEmpty( ) to check if the string of PORTX is null
var query = ( ... ).FirstOrDefault( );
if( query != null )
{
if( query.PORT1 != null && !string.IsNullOrEmpty( query.PORT1.ToString( ) ) )
{
}
else { ... }
}
You have to check query.PORT1 for null before calling ToString on it, you can use String.IsNullOrEmpty to check both conditions. Before checking query.PORT1 you need to check if query is null or not. You also need to use && instead of or operator as || will cause the right side of or operator to be evaluated if left is false and on right side calling ToString on null will again through exception.
if (query != null && query.PORT1 != null && query.PORT1.ToString() != string.Empty)
{ Session["Port1"] = query.PORT1; }
Using IsNullOrEmpty
if(query != null && !String.IsNullOrEmpty(query.PORT1))
{
Session["Port1"] = query.PORT1;
}

Variables within lambda/LINQ

is there a way for bellow lambda expression to move the method call ConvertFilterType(filter.FilterTypeId) into a variable so it is not called repeatedly for every condition ?
This if statement is making sure collection consist of all required filters.
if (run.Filters.All(
filter => (ConvertFilterType(filter.FilterTypeId) != FilterType.A)
&& (ConvertFilterType(filter.FilterTypeId) != FilterType.B)
&& (ConvertFilterType(filter.FilterTypeId) != FilterType.C)
&& (ConvertFilterType(filter.FilterTypeId) != FilterType.D)
&& (ConvertFilterType(filter.FilterTypeId) != FilterType.E)))
{
throw new ArgumentException();
}
if (run.Filters.All(
filter => {
FilterType t = ConvertFilterType(filter.FilterTypeId);
return
t != FilterType.A && t != FilterType.B && t != FilterType.C && t != FilterType.D && t != FilterType.E;
}))
{
throw new ArgumentException();
}
King beat me to it. Although it is not what you asked, I would also recommend that for improved readability, that you create a separate method with a more meaningful name. This chain of logic can obscure the intent of what you are doing. Extracting it into a method would help clarify the method's intent. I would have put this in the comment's to King's post, but don't have the reputation yet.
Here is some sample code, though I don't know what the type of the items in your Filters collection to know what the parameter of the method would need to be.
if (run.Filters.All(filter => { return IsFilterAllowed(filter); } )
{
throw new ArgumentException();
}
private bool IsFilterAllowed(FiltersItemType filter)
{
FilterType t = ConvertFilterType(filter.FilterTypeId);
return
t != FilterType.A &&
t != FilterType.B &&
t != FilterType.C &&
t != FilterType.D &&
t != FilterType.E;
}
King's answer is the way to go but could you could also do this it seems.
FilterType[] notTheseFilters = new FilterType[] { FilterType.A, FilterType.B...};
bool result = !Filters.Any(f =>
notTheseFilters.Contains(ConvertFilterType(f.FilterTypeId)));
If my logic serves me right, you could also do this
var filterTypesToAvoid = new[]{
FilterType.A,
FilterType.B,
FilterType.C,
FilterType.D,
FilterType.E
};
if (run.Filters.All(
filter => !filterTypesToAvoid.Contains(ConvertFilterType(filter.FilterTypeId))
))
{
throw new ArgumentException();
}
This way there is no need for brackets in the LINQ query.
To extend what brader24 proposed, you could try :
if (run.Filters.All(IsFilterAllowed))
{
throw new ArgumentException();
}
private bool IsFilterAllowed(FiltersItemType filter)
{
FilterType t = ConvertFilterType(filter.FilterTypeId);
return
t != FilterType.A &&
t != FilterType.B &&
t != FilterType.C &&
t != FilterType.D &&
t != FilterType.E;
}

C# short-circuiting weirdness

Given a = null
Shouldn't this not throw an exception?
if(a != null && ((string)a).ToLower()=="boo"){
... operation
}
Due to lazy evaluation the second statement should never be called so this ((string)a).ToLower() shouldn't be throwing an exception right ?
UPDATE
IOrderedEnumerable<SPListItem> Items = sourceList.GetItems("ProjectID", "Title", "ProjectName", "Featured", "Size", "Description")
.Cast<SPListItem>().AsEnumerable().OrderBy((i => rnd.Next()));
SPListItemCollection randProjImages = SPContext.Current.Web.Lists.TryGetList("Random Projects Images").GetItems();
var randomImgNrs = Enumerable.Range(0, randProjImages.Count).OrderBy(i => rnd.Next()).ToArray();
Dictionary<string, string> projectImages = new Dictionary<string,string>();
IEnumerable<SPListItem> projImages = SPContext.Current.Web.Lists.TryGetList("Assigned Projects Images").
GetItems().Cast<SPListItem>().AsEnumerable();
foreach (SPListItem it in projImages) projectImages.Add((string)it["ProjectID"], (string)it["ServerUrl"]);
int qCount = 0;
foreach (SPListItem item in Items) {
if (item["Size"] != null && item["Featured"]!=null &&
((string)item["Size"]).ToLower() == "big" || ((string)item["Featured"]).ToLower() == "yes") {
dataItems.Add(new Project(item["ProjectID"].ToString(), (string)item["Title"],
"/sites/Galileo/SitePages/" + Utils.getCurrentLang().ToLower() + "/Projects.aspx#id=pwp" + item["ProjectID"],
projectImages[(string)item["ProjectID"]],
Utils.truncateString(item.Fields["Description"].GetFieldValueAsText(item["Description"]), 175), "project"));
}else{
Replacing the foreach with this:
foreach (SPListItem item in Items) {
var k = item["Size"];
if (item["Size"] != null && item["Featured"]!=null &&
((string)item["Size"]).ToLower() == "big" || ((string)item["Featured"]).ToLower() == "yes") {
dataItems.Add(new Project(item["ProjectID"].ToString(), (string)item["Title"],
"/sites/Galileo/SitePages/" + Utils.getCurrentLang().ToLower() + "/Projects.aspx#id=pwp" + item["ProjectID"],
projectImages[(string)item["ProjectID"]],
Utils.truncateString(item.Fields["Description"].GetFieldValueAsText(item["Description"]), 175), "project"));
}else{
And breaking just before the if statement in the debugger, k == null
if (item["Size"] != null && item["Featured"]!=null &&
((string)item["Size"]).ToLower() == "big" || ((string)item["Featured"]).ToLower() == "yes")
it goes to the ((string)item["Featured"]).ToLower() == "yes")
No exception:
string a = null;
if (a != null && ((string) a).ToLower() == "boo")
string a = "";
if (a != null && ((string) a).ToLower() == "boo")
string a = "boo";
if (a != null && ((string) a).ToLower() == "boo")
this is as it should be (though the casting is unnecessary).
Does not compile:
T a = new T();
if (a != null && ((string) a).ToLower() == "boo")
T? a = null;
if (a != null && ((string) a).ToLower() == "boo")
T? a = new T();
if (a != null && ((string) a).ToLower() == "boo")
where T is any type other than string due to casting from T to string.
The real question is: Why do think it throws an exception?
Use
String.IsNullOrWhiteSpace(a)
You are right that the second argument of && is not evaluated if the first argument is false (a is null). So the code cannot throw a NullReferenceException. However, if a is not null but an object that cannot be cast to string, the code can throw an InvalidCastException.
Note that the best practice for comparing strings in a case-insensitive way is to use the String.Equals Method:
if (String.Equals(a, "foo", StringComparison.CurrentCultureIgnoreCase))
{
// ... operation
}
Since the method is static, no null-check is needed.
Exactly, this will not throw an exception. Since your expression is AND, evaluating only the first part is enough to know that it will never evaluate to true, so the second part is skipped.
Correct! (except that it's not called lazy evaluation)
If the first operand evaluates to true, the second operand isn't evaluated.
http://msdn.microsoft.com/en-us/library/vstudio/6373h346.aspx

How can I validate something without using regular expressions in C#?

HI how can I validate a string, but without using regular expressions. For example how can validate this: xxx/xxxx where x is a digit? thanks
Something like that:
bool ValidateExpression(string expression)
{
string[] parts = expression.Split("/");
if (
parts.Length != 2
|| parts[0].Length != 3
|| parts[1].Length != 4
) return false;
int parsed;
return Int32.TryParse(parts[0], out parsed) && Int32.TryParse(parts[1], out parsed);
}
To be used later as
bool isValid = ValidateExpression("123/4567");
You can use Char.IsDigit to check if characters are a digit. For your specific case, you could do something like this:
public bool IsMyStringValid(string myString)
{
foreach(var c in myString)
if(!Char.IsDigit() && !c == '/') return false;
return true;
}
This is actually more specific to your case (3 digits, one '/' at index 3, followed by 4 digits):
public bool IsMyStringValid(string myString)
{
if(myString.Length != 8) return false;
for(var i = 0; i <8, i++)
if(!Char.IsDigit(myString[i]) || (i == 3 && myString[i] == '/') return false;
return true;
}
For that specific format you can use:
bool valid =
value.Length == 8 &&
value.Take(3).All(Char.IsDigit) &&
value[3] == '/' &&
value.Skip(4).All(Char.IsDigit);
Try this :
public bool ValidateString(string str)
{
var strArr = str.Split('/');
return strArr[0].All(char.IsDigit) && strArr[1].All(char.IsDigit);
}
hope this help;
I would use the .All to check something like this so no matter what the string is
if there is a non digit in it, it will not pass
public static bool IsMyStringValid(string strValidateString)
{
bool boolIsValid = false;
if (strValidateString.All(Char.IsDigit))
{
boolIsValid = true;
}
return boolIsValid;
}

Categories