1

I've created a USB Device Scanner which allows the user to see all Devices in a Tree-Hierarchy. First i was happy that it worked, but after the joy I saw that my code quality was... not good (see code example).

For Better understanding: This is how the tree looks like and what the code is giving back.

BUS-Type              Device Description
----------            -------------------
----------
PCI      |            |USB-Controller
USB      |            |----|USB-Root Hub
USB      |            |----|-----|Generic USB Hub
USB      |            |----|-----|-----|USB Device
USB      |            |----|-----|Generic USB Hub
.                     .
.                     .
And so on.

Now a code example:

if (hierarchyDeviceZero.Children.Count != 0) {
    for (int usbHierarchyLevelOne = 0; usbHierarchyLevelOne < hierarchyDeviceZero.Children.Count; usbHierarchyLevelOne++) {
        foreach (var hierarchyDeviceOne in usbDeviceTree.USBDeviceNodes.Where(
            d1 => d1.HardwareID.Equals(hierarchyDeviceZero.Children[usbHierarchyLevelOne].HardwareID))
        ) {
            Console.WriteLine("{0}\t|\t\t|---|{1}", hierarchyDeviceOne.EnumeratorName, hierarchyDeviceOne.Description);
            if (hierarchyDeviceOne.Children.Count != 0) {
                for (int usbHierarchyLevelTwo = 0; usbHierarchyLevelTwo < hierarchyDeviceZero.Children.Count; usbHierarchyLevelTwo++) {
                    foreach (var hierarchyDeviceTwo in usbDeviceTree.USBDeviceNodes.Where(
                        d1 => d1.HardwareID.Equals(hierarchyDeviceOne.Children[usbHierarchyLevelTwo].HardwareID))
                    ) {
                        Console.WriteLine("{0}\t|\t\t    |---|{1}", hierarchyDeviceTwo.EnumeratorName, hierarchyDeviceTwo.Description);
                        if (hierarchyDeviceTwo.Children.Count != 0) {
                            for (int usbHierarchyLevelThree = 0; usbHierarchyLevelThree < hierarchyDeviceZero.Children.Count; usbHierarchyLevelThree++) {
                                foreach (var hierarchyDeviceThree in usbDeviceTree.USBDeviceNodes.Where(
                                    d1 => d1.HardwareID.Equals(hierarchyDeviceTwo.Children[usbHierarchyLevelThree].HardwareID))
                                ) {
.
.
.
.
And so on (until 10th Hierarchylevel).

My Question is, how can i put this into a Recursive Method? I tried some tutorials but i somewhat did not understand how a method can call itself 10 times without overwriting its values... I hope you guys understand what i was trying to so.

PS.: Sorry for my bad english.

3
  • 4
    Might be better here: codereview.stackexchange.com Commented Feb 2, 2015 at 16:00
  • 2
    refactor it to a method that can be called with a paremeter level Commented Feb 2, 2015 at 16:00
  • 4
    Variables declared in the scope of the method (and parameters) are local to the particular invocation of that method. So a recursive call uses a separate set of local variables. Commented Feb 2, 2015 at 16:05

1 Answer 1

1

In your data structure I see something wierd, namely you seem to have a UsbDeviceTree with all nodes, but you also have a .Children property on an individual node.

That aside, I think you can do something like this if you can just use the .Children node.

public class Device {
    public int HardwareId { get; set; }
    public Device[] Children {get; set; }
    /* ...the rest... */
}                               

void DisplayNodes(IEnumerable<Device> currentCollection, int indentation) {
    foreach (var currentNode in currentCollection) {
        // Display current node
        Console.WriteLine(
            ...
            display the node and
            use the indentation parameter to control the --- or ---|---
            ...
        );

        if (currentNode.Children != null) {
            DisplayNode(currentNode.Children, indentation + 1);
        }
    }
}

// Data
IEnumerable<Device> allDevices = usbDeviceTree.USBDeviceNodes;
IEnumerable<Device> rootDevices = allDevices
    .Where(x => x.IsRootNode /* TODO */)
    .ToArray();

// Display
DisplayNodes(rootDevices, 1);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. That helped me alot! About that structure, I am working on that too =).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.