Naveen's Weblog

Bridge to future

Archive for July, 2010

Get all controls on a windows form without recursion

Posted by codingsense on July 22, 2010

Hi,

Its given in microsoft patterns and practices to consider replacing recursion with looping because each recursive call adds data to the stack. Examine your code and see if your recursive calls can be converted to a looping equivalent.

I was wondering if all the solutions that uses recursion can be replaced without using recursion.
So I took the first step in looking into my project and found a piece of code that was using recursion, a function that would give me all the controls on a windows form. So I decided trying to convert that code without the use of recursive call.

Here is what I came up with,



static IList getControls(Control ParentControl)
{
List<Control> ListOfControls = new List<Control>();
List<Control> ContainerControls = new List<Control>();
ContainerControls.Add(ParentControl);

while (ContainerControls.Count > 0)
{
foreach (Control control in ContainerControls[0].Controls)
{
if (control.Controls.Count > 0)
{
ContainerControls.Add(control);
}
ListOfControls.Add(control);
}
ContainerControls.Remove(ContainerControls[0]);
}
return ListOfControls;
}

Great, the method gave me proper result as compared with the recursive function, what next I wanted to see how it has increased in performance and I used stopwatch to determine the time taken by both of the methods on the same form which was heavily loaded with controls and containers, it had 82 controls on it. The time taken by recursive function was double the time taken by the new method.

So I concluded that replacing recursive method with looping will enhance performance and also helps in consuming less memory.

If you get a better way to overcome recursion then please feel free to comment, till then I will continue my search for more recursive calls in my code.

Happy Learning :)
Codingsense

Posted in C# | Tagged: , | Leave a Comment »

The module “D:\..\support files\..\abc.dll” was loaded but the call to DllRegisterServer failed with error code 0×80020009.

Posted by codingsense on July 1, 2010


Hi,

While registering a com dll using RegSvr32 I encountered the following problem in Windows 7

Problem:
The moduleThe module “D:\..\support files\..\abc.dll” was loaded but the call to DllRegisterServer failed with error code 0×80020009.
For more information about this problem, search online using the error code as a search term.

Solution:
1) I opened command prompt in administrator mode. ( Always open the cmd.exe in administrator mode while registring a dll in Vista or Win 7, Steps – Type cmd in search box, when cmd is listed in the above list, right click it and click open as administrator)
2) The folder “Support files” was having a space so it failed to locate it. I navigated to the support files and then used regsvr32 command and all worked fine :)

Happy Learning,
CodingSense

Posted in Errors | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.