# Help me understand some C# code?



## Seany1212 (Dec 17, 2010)

So ive been asked to create a program with classes and arrays and have been given some lines of code to use in it, the problem is ive been told the code sets up an array gets the next available location to store data but i like to know what my code does line by line without having to copy and paste code thats just been given to me, any help will be greatly appreciated 

        // setup array
        static public Hardware[] NewInitArray(int num)
        {
            Hardware[] arrhardware = new Hardware[num];
            for (int i = 0; i < num; i++)
            {
                arrhardware_ = new Hardware();
            }
            return arrhardware;
        }

        // get the available location to store the next data
        static private int getAvailableLocation(Hardware[] arrhardware)
        {
            int location=0;
            for (int x = 0; x < arrhardware.Length; x++)
            {
                if (arrhardware[x].ID == 0)
                {
                    location = x;
                    break;
                }

            } 
            return location;
        }_


----------



## Kreij (Dec 18, 2010)

```
[COLOR="Green"]// setup array
        // This creates a static method called NewInitArray which returns an
        // array of type Hardware and takes an integer as its parameter[/COLOR]
        static public Hardware[] NewInitArray(int num) 
        {
            [COLOR="Green"]// This creates an array of type Hardware called arrhardware
            // and sets its initial size to "num" (the passed in parameter)[/COLOR]
           Hardware[] arrhardware = new Hardware[num];
            [COLOR="Green"]// This starts a for-loop that loops from zero to "num" in increments of one[/COLOR]
           for (int i = 0; i < num; i++)
            {
               [COLOR="Green"]// This sets the value of arrhardware, at location i to a new
               // instance of type Hardware[/COLOR]
                arrhardware[i] = new Hardware();
            }
            [COLOR="Green"]// This returns the filled arrhardwware array to the callling method[/COLOR]
            return arrhardware;
        }

        [COLOR="Green"]// get the available location to store the next data
        // This creates a static method named getAvailableLocation which returns an int
        // and takes an array of type Hardware as its parameter[/COLOR]
        static private int getAvailableLocation(Hardware[] arrhardware)
        {
            [COLOR="Green"]// This initializes an integer variable named location to zero[/COLOR]
           int location=0;
            [COLOR="Green"]// This starts a for-loop that loops from zero to the length of the arrhardware array
            // in increments of one[/COLOR]
           for (int x = 0; x < arrhardware.Length; x++)
            {
               //[COLOR="Green"] This checks to see if the ID property of the Hardware instance at location x
               // is equal to zero[/COLOR]
               if (arrhardware[x].ID == 0)
                {
                   [COLOR="Green"]// If the ID is 0 set location equal to the value of x
                   // and exit the for-loop (You found an available location)[/COLOR]
                   location = x;
                   break;
                }
               
            } 
            [COLOR="Green"]// This returns the available location to the calling method[/COLOR]
           return location;
        }
```

Added comments to the code. If you have more questions just ask 

Since these are static methods, if you place them in a static class you will not have to instantiate the class before calling them.
In a non-static class you would have to do ...
MyClass _Class = new MyClass();
_Class.Method(Parameters);

With static methods in static classes you can skip the first line and just call the method through the class.
MyClass.Method(Parameters);

Remember though, that if the class is not in the same namespace as the calling method, you will have to tell the program where it is by including it at the top of the program with the "using" directive.
If it is in a seperate library (DLL) you will need to add the DLL file in the project references also.


----------

