Here is an example of the type of article I'm talking about:
http://support.microsoft.com/kb/319401
I assume these articles are written by people who work for Microsoft and that the code in the articles will always be rock solid and never contain any malicious code. I just want to make sure I can explain to my boss that this is an ok place to copy code from (I've been told never to copy code from the internet, but this seems like a safe source).
What are trusted documents? Trusted documents are files that have been marked as trusted by enabling active content in them. Active content (macros, ActiveX controls, data connections, and so on) opens without the Message Bar warning after you mark the file as trusted.
If you need immediate technical support for your business, Professional Support is available as a single pay-per-incident (PPI) or a 5-pack of incidents. Microsoft experts help you troubleshoot a specific problem, error message or functionality that isn't working as expected.
Click Options. Click Trust Center, and then click Trust Center Settings. Click Privacy Options. Under Privacy Options, select or clear Check Microsoft Office documents that are from or link to suspicious Web sites check box.
On the File tab, click Options. In the Access Options dialog box, on the left, click Trust Center. On the right, under Microsoft Office Access Trust Center, click Trust Center Settings. In the left pane of the Trust Center dialog box, click Trusted Locations.
I would trust them not to be malicious, but they're not always good code. (MSDN samples are sometimes pretty awful.)
For example, here's some code in the sample you gave:
compareResult = ObjectCompare.Compare
(listviewX.SubItems[ColumnToSort].Text,
listviewY.SubItems[ColumnToSort].Text);
// Calculate correct return value based on object comparison
if (OrderOfSort == SortOrder.Ascending)
{
// Ascending sort is selected, return normal result of compare operation
return compareResult;
}
else if (OrderOfSort == SortOrder.Descending)
{
// Descending sort is selected, return negative result of compare operation
return (-compareResult);
}
else
{
// Return '0' to indicate they are equal
return 0;
}
Now, there are two issues here:
int.MinValue
- because -int.MinValue == int.MinValue
. It's better to reverse the arguments used to perform the original comparison.There are other things I'd take issue with in this code, but these two should be enough to make my point.
I heartily agree with the other answers too, in terms of: - Check the copyright / licence etc of any code you want to use - Make sure you understand anything you want to use
Your boss probably wouldn't mind if you only copied the code into a test project that you use to test and understand the code. You can then use what you've learned to write the production code.
And while I don't think anyone outside of Microsoft knows the names of the people who write those support articles, they come from the same vendor that your toolchain does, so if you don't trust the support articles, then you can't trust the tools you've bought either.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With