|
|
Do you two realize how much money you just cost CP with this waste of disk space? You better click on some ads to pay for your mistakes.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
What was that message??, now its not there
Previous -> Read "CLR via C#" by Jeffrey Ritcher.
Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder.
Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.
|
|
|
|
|
Weird, I wonder why they were removed. They just had a ton of smiley face icons.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
May be because of "Smiley Overflows"
Previous -> Read "CLR via C#" by Jeffrey Ritcher.
Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder.
Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.
|
|
|
|
|
You owe me for the bandwidth cost of 2396 unnecessary s! Only the icon has 831 bytes, which amounts to a total of 1,991,076 wasted bytes. Gotta count now the extra html chars and send you the invoice!
|
|
|
|
|
Well, the null has really nothing to do there. Check if(employees.Count()==0)
I would do like this
class TestClass()
{
List<Employee> employees { get; set; }
TestClass()
{
employees = GetEmployees();
}
private List<Employee> GetEmployees()
{
return (from oRec in DataModel.Employees select oRec).ToList();
}
}
The signature is in building process.. Please wait...
|
|
|
|
|
Thanks.
Previous -> Read "CLR via C#" by Jeffrey Ritcher.
Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder.
Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.
|
|
|
|
|
vonb wrote: return (from oRec in DataModel.Employees select oRec).ToList();
Why do so many people insist on adding a pointless "from x in source select x " query to their code, when it has the same result as the much more concise "source "?
private List<Employee> GetEmployees()
{
return DataModel.Employees.ToList();
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
This was just a simple implementation of more complex queries which I actually use
publc List<Employee> GetEmployees(int departmentId)
{
return (from oRec in DataModel.Employees where oRec.DepartmentID == departmentId).ToList();
}
The signature is in building process.. Please wait...
|
|
|
|
|
That's one of the reasons why I tend to prefer the method-call syntax for most LINQ queries - if you remove the .Where(filter) clause, your code still looks sensible.
return DataModel.Employees.Where(oRec => oRec.DepartmentID == departmentId).ToList();
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Probably code that has been edited and then not refactored. No real wtf here, ey?
|
|
|
|
|
But that's not the case I have verified the previous versions from source control...
Previous -> Read "CLR via C#" by Jeffrey Ritcher.
Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder.
Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.
|
|
|
|
|
lol. Then its probably just a brain fart, or late night coding session
|
|
|
|
|
Guess I don't know c# well enough, but I didn't this you could use any comparison operator against NULL. it's probably bad practice to even if the language allows it.
|
|
|
|
|
Mark Starr wrote: I didn't this you could use any comparison operator against NULL You're mixing up SQL with C# as well as "this" vs "think". SQL isn't case sensitive and comparison operators won't work with null, NULL, or NuLl where all versions of the null keyword are valid. In C#, NULL is nonsense unless you've declared an object name NULL and the language is designed to make comparisons (==, !=) with null
|
|
|
|
|
Yup: I don't know C# well enough. Agreed. And, I didn't capitalize the 'i' at the beginning of the next sentence.
|
|
|
|
|
This thread started with:
List<Employee> employees = new List<Employee>();
if (employees != null){
employees = GetEmployees();
There are layers of things wrong with just this bit of code. "employees = new ..." created a new object, it is impossible for "if (employees != null)" to return false. (Others have said memory critical operations might have problems, but that can't happen because if it did happen, the behavior of C# is to throw an error. In which case, "if" would never execute. THEN it executes "employees = GetEmployees()" which threw the "=..." part of "employees = new ..." into the trash. NOW employees may or may not be null and no if checking if it is null or not is done, it just assumes that since it wasn't null it still isn't null. It is incredible how much bad code can be compressed into so little space, so it is worth posting in the weird and wonderful. The following code has exactly the same amount of effective error checking as the first bit of code:
List<Employee> employees = GetEmployees();
Sorry, I wasn't trying to bash you about your knowledge. (I was teasing you about your mistyping, thinking you wrote it while tired and made an easy mistake to make when you are tired.) The SQL part of what I said was just trying to teach you a little bit of the difference between the two languages. Everyone is ignorant in some things in their lives. Nothing wrong with that. There is something wrong with wanting to be ignorant and I didn't think that was happening with you.
|
|
|
|
|
Not a prob, I was in fact, just starting the day, and clearly had too little coffee in my system to type, much less proof.
We had just been playing around with javascript to see what happens when you don't declare a variable versus when you declare it and don't assign a value - how it affects checking for undefined, not-undefined, null, etc. And, Yes, I typically write a lot more in T-SQL, but I also use VB, VB.NET, et al in my daily work - hence my disclaimer about not writing a lot of C#. I'm always careful when checking for nulls, so when I see comparison operator my hair stands up a bit.
Initially, to me, the code looked as though someone had somehow had a bad experience populating the list from using the "new List<employee>();" call, and so put in a safety-catch (if you will) to try again if necessary. Or, perhaps, the second part was already there, and someone later came along and added the first line... I'm sure they though it was right at the time. Who knows!
Thanks for the note KP. Cheers and happy coding,
Mark
|
|
|
|
|
Check your version control. It's possible declaration and/or the assignment at declaration was added later. If an earlier version of the method had the List<employee> passed as a parameter, then the test for non-null would make sense.
|
|
|
|
|
Quote: It's possible declaration and/or the assignment at declaration was added later.
Ok but in the case if it is added later also, the guy should have removed the null check as it is not applicable now. Anyhow that wasn't the case, I checked the source control
Previous -> Read "CLR via C#" by Jeffrey Ritcher.
Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder.
Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.
|
|
|
|
|
I bet the person who wrote this comes from a C/C++ background, there, you have to check for null every time or you may blow out something...
|
|
|
|
|
Correct. *** You're superb,,, What a guess!!! ***
Previous -> Read "CLR via C#" by Jeffrey Ritcher.
Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder.
Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.
|
|
|
|
|
The only thing that I can think of is technically new doesn't guarantee that the variable initialization will take place. In programming languages like c++ it could return null. This could happen if the memory was so fragmented that the allocation failed. However, instead of returning null in C# I believe it throws a out of memory exception. I have only seen this done in system critical embedded systems.
|
|
|
|
|
Thanks Michael. That was a excellent explanation.
Previous -> Read "CLR via C#" by Jeffrey Ritcher.
Current -> Exploring WCF thru Apress' "Pro WCF" by Chris Peiris and Dennis Mulder.
Next -> Need to read "The Art of Computer Programming" by Donald E. Knuth.
|
|
|
|