|
I wrote this at 4AM the other morning :S I'm a little embarrassed. But it's still doing it's bit in my program.
for(int y = 0; y < bmp.biHeight; y=y+3){
for(int x = 0; x < bmp.biWidth; x=x+3){
for(int i = 0; i < 9; i++){
int yp=0, xp=0;
get_pixel(x+xp,y+yp,blank[i]);
if(xp<3)
xp++;
else{
xp = 0;
yp++;
}}
avg(blank, Merge, 9);
set_pixel(x,y,Merge);
for(int i = 0; i < 9; i++){
blank[i].rgbtBlue=0;
blank[i].rgbtGreen=0;
blank[i].rgbtRed=0;
}
clear_pixel(Merge);
yp=0, xp=0;
}}
"Sir, I protest. I am NOT a merry man!"
|
|
|
|
|
|
Did I miss something? isn't:
yp=0, xp=0;
out of scope?
|
|
|
|
|
Yeah, that was an error I have since fixed. long xp=0, yp=0 was defined before this segment. Only after did I realise I had 'int'ed them.
"Sir, I protest. I am NOT a merry man!"
|
|
|
|
|
Did you also realize that you haven't 'int'ed them, you actually have 2 new variables defined in the scope of the inner loop? :-P the long ones are only being assigned 0 every time that loop executes and thats it.
Scope is a wonderful thing, but it can also come back to bite you if you aren't careful.
|
|
|
|
|
Yes... that was the error I fixed.
"Sir, I protest. I am NOT a merry man!"
|
|
|
|
|
How fast does that run? in my experience using GetPixel and SetPixel are extremely slow
|
|
|
|
|
Fast enough that changing it would have no observable change.
But then, that may just be the fruits of overclocking.
"Sir, I protest. I am NOT a merry man!"
|
|
|
|
|
I can't even be assed looking at code formatted like that – incomprehensible! The first thing I do at work when I encounter rubbish like that is re-format it.
Steve
|
|
|
|
|
Nothing too bad there. One of the most "elegant" pieces of code I wrote in college was done after pounding down a fifth of tequila It was an amazing piece of work that worked very well.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
|
It seems the author doesn't know foreach . Nor how to cast.
|
|
|
|
|
That's just the start, it gets better. Check out the using statements and where the objects are going.
I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
|
|
|
|
|
People like to make things hard for themselves. What was this guy smoking when he was coding? My thoughts are that he's an ex-C++ programming who landed a C# project.
Looks like this is part of the BO layer that works with multiple databases (db) that contains exchange rates. The database pointer is passed down to the data access layer, data is retrieved and parsed into a simple list and returned to the caller.
Not exactly an efficient way of doing it...
Am I close?
|
|
|
|
|
You're close. It's very similar to a common error new C++ developers make, and is something that garbage collection is supposed to fix, but they've managed to do it in C#. Think about the lifetime of the objects created in the loop and the using statement.
I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
|
|
|
|
|
Yeah... Trying to return data that's already expired! Nice one... lol
|
|
|
|
|
Yeah, since the dispose method closes database connections, then just wrap the object in a using and it will be automatically closed.
They cut and pasted a standard dispose method including the check that it only runs once, so you can only close the database connection once, which is only an issue when you save one of the objects.
I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
|
|
|
|
|
I don't understand .Net memory management myself... I seriously think a delete operator wouldnt hurt. Why is there a new and no delete ? I don't blame this guy for making mistakes, at least his code is well formatted. I work on VB code that looks like it was written by monkies in a scientific experiment.
|
|
|
|
|
so,
for(int i=0; i<source.length;> {
result[i] = source[i];
source[i].~object();
result[i].~object();
}
return result;
Makes sense to you?
I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
|
|
|
|
|
Complete class provided for entertainment purposes
<font color="Blue">namespace</font> BusinessEntities
<font color="DarkBlue">{</font>
<font color="Blue">public</font> <font color="Blue">class</font> <font color="Teal">DropDownLists</font>
<font color="DarkBlue">{</font>
<font color="Blue">private</font> <font color="Teal">Collection</font><font color="DarkBlue"><</font><font color="Teal">ListItem</font><font color="DarkBlue">></font> myList<font color="DarkBlue">;</font>
<font color="Blue">public</font> <font color="Blue">static</font> <font color="Teal">DropDownLists</font> Construct_DropDownLists<font color="DarkBlue">(</font><font color="DarkBlue">)</font>
<font color="DarkBlue">{</font>
<font color="Teal">DropDownLists</font> tmpDropDownLists <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">DropDownLists</font><font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
<font color="Blue">return</font> tmpDropDownLists<font color="DarkBlue">;</font>
<font color="DarkBlue">}</font>
<font color="Blue">public</font> <font color="Teal">Collection</font><font color="DarkBlue"><</font><font color="Teal">ListItem</font><font color="DarkBlue">></font> MyList
<font color="DarkBlue">{</font>
<font color="Blue">get</font>
<font color="DarkBlue">{</font>
<font color="Blue">if</font> <font color="DarkBlue">(</font>myList <font color="DarkBlue">==</font> <font color="Blue">null</font><font color="DarkBlue">)</font>
<font color="DarkBlue">{</font>
myList <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">Collection</font><font color="DarkBlue"><</font><font color="Teal">ListItem</font><font color="DarkBlue">></font><font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
<font color="DarkBlue">}</font>
<font color="Blue">return</font> myList<font color="DarkBlue">;</font>
<font color="DarkBlue">}</font>
<font color="Blue">set</font> <font color="DarkBlue">{</font> myList <font color="DarkBlue">=</font> <font color="Blue">value</font><font color="DarkBlue">;</font> <font color="DarkBlue">}</font>
<font color="DarkBlue">}</font>
<font color="DimGray"> <font color="DimGray"> <font color="DimGray"> <font color="DimGray"> <font color="DimGray"> <font color="Blue">public</font> <font color="Teal">Collection</font><font color="DarkBlue"><</font><font color="Teal">ListItem</font><font color="DarkBlue">></font> GetTitles<font color="DarkBlue">(</font><font color="Teal">DropDownLists</font> tmpDropDownLists<font color="DarkBlue">)</font>
<font color="DarkBlue">{</font>
<font color="DarkGreen">
tmpDropDownLists<font color="DarkBlue">.</font>myList <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">Collection</font><font color="DarkBlue"><</font><font color="Teal">ListItem</font><font color="DarkBlue">></font><font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
<font color="Teal">ListItem</font> li <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">ListItem</font><font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
li<font color="DarkBlue">.</font>Value <font color="DarkBlue">=</font> <font color="Maroon">"1"</font><font color="DarkBlue">;</font>
li<font color="DarkBlue">.</font>Text <font color="DarkBlue">=</font> <font color="Maroon">"Mr."</font><font color="DarkBlue">;</font>
tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">.</font>Add<font color="DarkBlue">(</font>li<font color="DarkBlue">)</font><font color="DarkBlue">;</font>
li <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">ListItem</font><font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
li<font color="DarkBlue">.</font>Value <font color="DarkBlue">=</font> <font color="Maroon">"2"</font><font color="DarkBlue">;</font>
li<font color="DarkBlue">.</font>Text <font color="DarkBlue">=</font> <font color="Maroon">"Mrs."</font><font color="DarkBlue">;</font>
tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">.</font>Add<font color="DarkBlue">(</font>li<font color="DarkBlue">)</font><font color="DarkBlue">;</font>
li <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">ListItem</font><font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
li<font color="DarkBlue">.</font>Value <font color="DarkBlue">=</font> <font color="Maroon">"3"</font><font color="DarkBlue">;</font>
li<font color="DarkBlue">.</font>Text <font color="DarkBlue">=</font> <font color="Maroon">"Me."</font><font color="DarkBlue">;</font>
tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">.</font>Add<font color="DarkBlue">(</font>li<font color="DarkBlue">)</font><font color="DarkBlue">;</font>
li <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">ListItem</font><font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
li<font color="DarkBlue">.</font>Value <font color="DarkBlue">=</font> <font color="Maroon">"4"</font><font color="DarkBlue">;</font>
li<font color="DarkBlue">.</font>Text <font color="DarkBlue">=</font> <font color="Maroon">"Dr."</font><font color="DarkBlue">;</font>
tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">.</font>Add<font color="DarkBlue">(</font>li<font color="DarkBlue">)</font><font color="DarkBlue">;</font>
li <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">ListItem</font><font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
li<font color="DarkBlue">.</font>Value <font color="DarkBlue">=</font> <font color="Maroon">"5"</font><font color="DarkBlue">;</font>
li<font color="DarkBlue">.</font>Text <font color="DarkBlue">=</font> <font color="Maroon">"Prof."</font><font color="DarkBlue">;</font>
tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">.</font>Add<font color="DarkBlue">(</font>li<font color="DarkBlue">)</font><font color="DarkBlue">;</font>
<font color="Blue">string</font><font color="DarkBlue">[]</font> myKeys <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Blue">string</font><font color="DarkBlue">[</font>tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">.</font>Count<font color="DarkBlue">]</font><font color="DarkBlue">;</font>
<font color="Blue">string</font><font color="DarkBlue">[]</font> myValues <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Blue">string</font><font color="DarkBlue">[</font>tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">.</font>Count<font color="DarkBlue">]</font><font color="DarkBlue">;</font>
<font color="Blue">int</font> counter <font color="DarkBlue">=</font> <font color="Red">0</font><font color="DarkBlue">;</font>
<font color="Blue">foreach</font> <font color="DarkBlue">(</font><font color="Teal">ListItem</font> lit <font color="Blue">in</font> tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">)</font>
<font color="DarkBlue">{</font>
myKeys<font color="DarkBlue">[</font>counter<font color="DarkBlue">]</font> <font color="DarkBlue">=</font> lit<font color="DarkBlue">.</font>Value<font color="DarkBlue">;</font>
myValues<font color="DarkBlue">[</font>counter<font color="DarkBlue">]</font> <font color="DarkBlue">=</font> lit<font color="DarkBlue">.</font>Text<font color="DarkBlue">;</font>
counter<font color="DarkBlue">++</font><font color="DarkBlue">;</font>
<font color="DarkBlue">}</font>
<font color="Teal">Collection</font><font color="DarkBlue"><</font><font color="Teal">ListItem</font><font color="DarkBlue">></font> tmpList <font color="DarkBlue">=</font> <font color="Blue">this</font><font color="DarkBlue">.</font>SortItemsAlphabetically<font color="DarkBlue">(</font>myKeys<font color="DarkBlue">,</font> myValues<font color="DarkBlue">)</font><font color="DarkBlue">;</font>
tmpDropDownLists<font color="DarkBlue">.</font>myList <font color="DarkBlue">=</font> tmpList<font color="DarkBlue">;</font>
<font color="Blue">return</font> tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">;</font>
<font color="DarkBlue">}</font>
<font color="DimGray"> <font color="DimGray"> <font color="DimGray"> <font color="DimGray"> <font color="DimGray"> <font color="Blue">public</font> <font color="Teal">Collection</font><font color="DarkBlue"><</font><font color="Teal">ListItem</font><font color="DarkBlue">></font> GetCountries<font color="DarkBlue">(</font><font color="Teal">DropDownLists</font> tmpDropDownLists<font color="DarkBlue">)</font>
<font color="DarkBlue">{</font>
<font color="DarkGreen">
tmpDropDownLists<font color="DarkBlue">.</font>myList <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">Collection</font><font color="DarkBlue"><</font><font color="Teal">ListItem</font><font color="DarkBlue">></font><font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
<font color="Teal">ListItem</font> li <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">ListItem</font><font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
li <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">ListItem</font><font color="DarkBlue">(</font><font color="Maroon">"Argentina"</font><font color="DarkBlue">,</font> <font color="Maroon">"AR"</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">.</font>Add<font color="DarkBlue">(</font>li<font color="DarkBlue">)</font><font color="DarkBlue">;</font>
li <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">ListItem</font><font color="DarkBlue">(</font><font color="Maroon">"Bangladesh"</font><font color="DarkBlue">,</font> <font color="Maroon">"BD"</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">.</font>Add<font color="DarkBlue">(</font>li<font color="DarkBlue">)</font><font color="DarkBlue">;</font>
<font color="DarkGreen">
li <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">ListItem</font><font color="DarkBlue">(</font><font color="Maroon">"Venezuela"</font><font color="DarkBlue">,</font> <font color="Maroon">"VE"</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">.</font>Add<font color="DarkBlue">(</font>li<font color="DarkBlue">)</font><font color="DarkBlue">;</font>
li <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">ListItem</font><font color="DarkBlue">(</font><font color="Maroon">"Vietnam"</font><font color="DarkBlue">,</font> <font color="Maroon">"VN"</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">.</font>Add<font color="DarkBlue">(</font>li<font color="DarkBlue">)</font><font color="DarkBlue">;</font>
<font color="Blue">string</font><font color="DarkBlue">[]</font> myKeys <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Blue">string</font><font color="DarkBlue">[</font>tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">.</font>Count<font color="DarkBlue">]</font><font color="DarkBlue">;</font>
<font color="Blue">string</font><font color="DarkBlue">[]</font> myValues <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Blue">string</font><font color="DarkBlue">[</font>tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">.</font>Count<font color="DarkBlue">]</font><font color="DarkBlue">;</font>
<font color="Blue">int</font> counter <font color="DarkBlue">=</font> <font color="Red">0</font><font color="DarkBlue">;</font>
<font color="Blue">foreach</font> <font color="DarkBlue">(</font><font color="Teal">ListItem</font> lit <font color="Blue">in</font> tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">)</font>
<font color="DarkBlue">{</font>
myKeys<font color="DarkBlue">[</font>counter<font color="DarkBlue">]</font> <font color="DarkBlue">=</font> lit<font color="DarkBlue">.</font>Value<font color="DarkBlue">;</font>
myValues<font color="DarkBlue">[</font>counter<font color="DarkBlue">]</font> <font color="DarkBlue">=</font> lit<font color="DarkBlue">.</font>Text<font color="DarkBlue">;</font>
counter<font color="DarkBlue">++</font><font color="DarkBlue">;</font>
<font color="DarkBlue">}</font>
<font color="Teal">Collection</font><font color="DarkBlue"><</font><font color="Teal">ListItem</font><font color="DarkBlue">></font> tmpList <font color="DarkBlue">=</font> <font color="Blue">this</font><font color="DarkBlue">.</font>SortItemsAlphabetically<font color="DarkBlue">(</font>myKeys<font color="DarkBlue">,</font> myValues<font color="DarkBlue">)</font><font color="DarkBlue">;</font>
tmpDropDownLists<font color="DarkBlue">.</font>myList <font color="DarkBlue">=</font> tmpList<font color="DarkBlue">;</font>
<font color="Blue">return</font> tmpDropDownLists<font color="DarkBlue">.</font>myList<font color="DarkBlue">;</font>
<font color="DarkBlue">}</font>
<font color="DimGray"> <font color="DimGray"> <font color="DimGray"> <font color="DimGray"> <font color="DimGray"> <font color="DimGray"> <font color="Blue">private</font> <font color="Teal">Collection</font><font color="DarkBlue"><</font><font color="Teal">ListItem</font><font color="DarkBlue">></font> SortItemsAlphabetically<font color="DarkBlue">(</font><font color="Blue">string</font><font color="DarkBlue">[]</font> myKeys<font color="DarkBlue">,</font> <font color="Blue">string</font><font color="DarkBlue">[]</font> myValues<font color="DarkBlue">)</font>
<font color="DarkBlue">{</font>
<font color="Teal">Collection</font><font color="DarkBlue"><</font><font color="Teal">ListItem</font><font color="DarkBlue">></font> list <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">Collection</font><font color="DarkBlue"><</font><font color="Teal">ListItem</font><font color="DarkBlue">></font><font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
<font color="DarkGreen">
Array<font color="DarkBlue">.</font>Sort<font color="DarkBlue">(</font>myKeys<font color="DarkBlue">,</font> myValues<font color="DarkBlue">,</font> <font color="Red">1</font><font color="DarkBlue">,</font> <font color="Red">3</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
<font color="Blue">for</font> <font color="DarkBlue">(</font><font color="Blue">int</font> i <font color="DarkBlue">=</font> <font color="Red">0</font><font color="DarkBlue">;</font> i <font color="DarkBlue"><</font> myKeys<font color="DarkBlue">.</font>Length<font color="DarkBlue">;</font> i<font color="DarkBlue">++</font><font color="DarkBlue">)</font>
<font color="DarkBlue">{</font>
<font color="Teal">ListItem</font> lit <font color="DarkBlue">=</font> <font color="Blue">new</font> <font color="Teal">ListItem</font><font color="DarkBlue">(</font>myValues<font color="DarkBlue">[</font>i<font color="DarkBlue">]</font><font color="DarkBlue">.</font>ToString<font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">,</font> myKeys<font color="DarkBlue">[</font>i<font color="DarkBlue">]</font><font color="DarkBlue">.</font>ToString<font color="DarkBlue">(</font><font color="DarkBlue">)</font><font color="DarkBlue">)</font><font color="DarkBlue">;</font>
list<font color="DarkBlue">.</font>Add<font color="DarkBlue">(</font>lit<font color="DarkBlue">)</font><font color="DarkBlue">;</font>
<font color="DarkBlue">}</font>
<font color="Blue">return</font> list<font color="DarkBlue">;</font>
<font color="DarkBlue">}</font>
<font color="DarkBlue">}</font>
<font color="DarkBlue">}</font>
|
|
|
|
|
Congratulations to these "self-studies".
-+--+--+--+--+--+--+-+-+-+
Carlos Zani - SP - Brazil
Powered by Microsof.Net
|
|
|
|
|
But which part do you particularly wish to point out?
I find the use of string.ToString() the most humorous.
I'm also not sure what you mean by the title;
Array.Sort(myKeys, myValues, 1, 3);
won't "Sort by the first 3 characters", it sorts the second through fourth items.
modified on Sunday, June 8, 2008 11:14 AM
|
|
|
|
|
PIEBALDconsult wrote: I'm also not sure what you mean by the title;
Array.Sort(myKeys, myValues, 1, 3);
won't "Sort by the first 3 characters", it sorts the second through fourth items.
Exactly
Thats the real horror, how he gets there is a horror too!
All this has been reduced to 2 lines of code getting data straight from the DB.
|
|
|
|
|
|
This enhances readability.
Greetings from Germany
|
|
|
|