 |
 | C# Compiler bug or Subtle bug, I don't know, but this is certainly a doozy! |  | Super Lloyd | 4hrs 34mins ago |
|
|
I have a problem with Lambda expression, looks like a C# compiler bug to me....
This piece of code
static void Main(string[] args) { var names = new List<string> { "Lloyd", "Stuart", "Gabe", "Mark" }; var actions = new List<Action>(); foreach (var item in names) actions.Add(() => Console.WriteLine(item)); foreach (var a in actions) a(); }
Will print "Mark, Mark, Mark, Mark" Instead of "Lloyd, Stuart, Gabe, Mark" as it should....
I can fix it with the slightly modified version below..
static void Main(string[] args) { var names = new List<string> { "Lloyd", "Stuart", "Gabe", "Mark32" }; var actions = new List<Action>(); foreach (var item in names) { string s = item; actions.Add(() => Console.WriteLine(s)); } foreach (var a in actions) a(); }
But I think it's a C# compiler bug...
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
|
|
|
|
 |
|
|
Yeah, it should print, "Chuck Norris,Chuck Norris,Chuck Norris,Chuck Norris". 
|
|
|
|
 |
|
|
ReSharper gives a warning about that. Let's just keep it at a feature.
|
|
|
|
 |
|
|
It's always fun learning new technologies all at once such as ASP.NET MVC + Autofac (IoC container) + LinqToSQL and running into some bizzare exception such as the following:
The requested service 'controller.v' has not been registered
Lets see who can guess what the error is 
EDIT: here is the answer[^]
Todd Smith
modified on Tuesday, December 2, 2008 1:01 PM
|
|
|
|
 |
|
|
Probably a bug in MVC, they have been actively introducing them.
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
 |
|
|
I don't know Autofac at all, but it sounds like a strange error message from deep inside of it.
I assume that because one can say that an IoC-container is about requesting previously registered services. I don't think there is one (or should be) having this weird name. This sounds like part of the 'magic', or maybe it's something that LinqToSQL thinks it should have by the sheer law of nature...
Regards Thomas
www.thomas-weller.deProgramming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
 |
|
|
 |
|
|
Thomas Weller wrote: I assume that because one can say that an IoC-container is about requesting previously registered services. I don't think there is one (or should be) having this weird name. This sounds like part of the 'magic', or maybe it's something that LinqToSQL thinks it should have by the sheer law of nature...
I had many similar thoughts on what it could be. I wasn't able to find it until I downloaded the source code to Autofac and ASP.NET MVC and was able to step through them.
It ended up being a copy'n'paste bug. src="v/images/search.gif" should have been something like src="../../Content/images/search.gif". The url /v/images/search.gif matches the default /controller/action/id pattern that MVC looks for. So Autofac was trying to construct a controller object named v which resulted in an obscure exception message.
Another good tip is to add routes.IgnoreRoute ("favicon.ico"); or you'll get an error on that one as well.
Todd Smith
|
|
|
|
 |
 | C++: Reading an ifstream |  | PJ Arends | 10:09 1 Dec '08 |
|
|
This bit of code has been running flawlessly for quite a while.
The data file is a text file that contains a bunch of white space separated integers that get read into a vector. I allow comments in the data file by preceding the comment with a semicolon.
I always had revision history comments at the end of the file but yesterday I added a new one at the end and the whole app stopped working.
Can you see the problem?std::vector<int> targets; std::ifstream targetfile(FileName.c_str()); if (targetfile.is_open()) { int number = 0; targets.push_back(number); // place holder for position zero while (!targetfile.eof()) { char next = targetfile.peek(); if (next == ';') { // bypass the rest of the line as it is a comment while (next != '\n') { targetfile.get(next); } } else if (isspace(next)) { // advance pass the white-space targetfile.get(next); } else { // grab the number and add it to the vector targetfile >> number; targets.push_back(number); } } targetfile.close(); }
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good - Use it!
|
|
|
|
 |
|
|
Your final line didn't end with a newline character?
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall
|
|
|
|
 |
|
|
Yeah, that was it. The solution was to check for the end of file while attempting to bypass the comment line.// bypass the rest of the line as it is a comment while (next != '\n' && !targetfile.eof()) {
Lesson: always check for EOF when reading a file in a loop.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good - Use it!
|
|
|
|
 |
|
|
A newline at the end of a comment is worth two in the bush.
-- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com
S. L.
|
|
|
|
 |
|
|
We all know that null, when compared with anything, returns false. Even null=null evaluates to false. But I was amazed to see that null!=Value also returns false.
Run the following T-SQL statements and you will get false for both the cases.
print (Case When null ='Value' Then 'true' else 'false' end) print (Case When null!='Value' Then 'true' else 'false' end)
The query was something like:
Select ... Case When Column1='SomeValue' .. Then .. Case When Column1!='SomeValue' .. Then .. ..
I was wondering why none of case statements were evaluating true. Finally, I came to know Column1 resulted in a null due to a left join and then null!='SomeValue' returned false.
Syed Mehroz Alam
My BlogMy ArticlesComputers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein
modified on Friday, November 14, 2008 7:12 AM
|
|
|
|
 |
|
|
Syed Mehroz Alam wrote: We all know that null, when compared with anything, returns false. Even null=null evaluates to false. But I was amazed to see that null!=Value also returns false.
!= is still a comparison operator.
Example: I have two things on my desk. One is a baseball. Is the other thing a baseball? Is the other thing not a baseball? You don't know, right?
Now, I think it would have been more useful to have comparisons containing nulls return null, instead of false. But that is beside the point.
|
|
|
|
 |
|
|
You're certainly right about how it actually does behave. But I don't really see why comparions with null ought to behave differently in SQL than in, say, C#. This whole design decision of using the is operator for comparison to null seems illogical and counterintuitive, but I presume there is some reason for it. If you see a good reason, would you care to elaborate?
|
|
|
|
 |
|
|
Syed Mehroz Alam wrote: I was amazed
I wasn't.
|
|
|
|
 |
|
|
Which is why you should use the isnull() function when you want to compare a nullable field to a another field or literal, and field is null (or field is not null) when you want to check for null, though I typically use isnull(field,'') <> '' as our users are wont to make fields empty strings.
I don't claim to be a know it all, for I know that I am not...
I usually have an answer though.
|
|
|
|
 |
|
|
You are right, I also tackled it using IsNull but the original query wasn't written by me and it was hard to find the cause of error since the query was a complex one.
My BlogMy ArticlesComputers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein
|
|
|
|
 |
|
|
Null = Value does not evaluate to false, nor true. It's Unknown. So there are not only two values: True and False, but also Unknown
From MSDN:
A value of NULL indicates that the value is unknown. A value of NULL is different from an empty or zero value. No two null values are equal. Comparisons between two null values, or between a NULL and any other value, return unknown because the value of each NULL is unknown.
|
|
|
|
 |
|
|
Scott Barbour wrote: I typically use isnull(field,'') <> '' as our users are wont to make fields empty strings.
If you do not want to represent two different things by null or empty string, why is the field nullable in the first place? In my view, using invariant representations is a virtue.
(Of course, there may be some cases where null and empty string is different for some purposed but not others, and then I guess this technique might be useful.)
|
|
|
|
 |
|
|
I didn't design the tables, I'm just tasked with working with them. If it has never had a value, it is null. If the value has been cleared, it is an empty string.
Of course, using the isnull(field,'') is even more useful when you are looking for specific values when nulls are present. It has been my experience with MS SQL Server 2000 that queries behave oddly with nulls (such as excluding rows from the resultset.) especially when using the LIKE operator.
Of course MSSQL 2000 has a number of quirks anyway. I've reworked a number of queries that just wouldn't return the correct data.
I don't claim to be a know it all, for I know that I am not...
I usually have an answer though.
|
|
|
|
 |
 | ref keyword bad communication between .net languages! |  | mrcooll | 11:11 4 Nov '08 |
|
|
i have discover that if u have two different project in same solution first is Presentation tear in C# and the other is BLL 'Business logic layer' tear in Vb.net have by ref Form parameter, you cant call it form c# by sending this as a parameter!
public sub SetFormPrivilage(by ref frm as Form) '' 'Some code goes here! '' End sub private SomeForm_load(object sender,EventArgs e) { SomeClassInOtherProjectObject Obj=new SomeClassInOtherProjectObject(); Obj.SetFormPrivilage(ref this);//Error you cant assign ref to this //or Obj.SetFormPrivilage(this);//Error SetFormPrivilage expect ref keyword
}
|
|
|
|
 |
|
|
Form blah = this; Obj.SetFormrivilage(ref blah);
Of course, if blah changes, "this" will not. But that would be strange anyways
This also happens in pure C#, iirc, as it should
|
|
|
|
 |
|
|
That's not a language problem, you're simply not allowed to pass this that way:
" CS1605: Cannot pass '<this>' as a ref or out argument because it is read-only "
And it's certainly not subtle.
In your case you probably don't need to pass by reference anyway.
Or you could do something like:
object o = this ; Obj.SetFormPrivilage ( ref o ) ;
|
|
|
|
 |
|
|
PIEBALDconsult wrote: And it's certainly not subtle.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]
|
|
|
|
 |