1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Help me understand some C# code?

Discussion in 'Programming & Webmastering' started by Seany1212, Dec 17, 2010.

  1. Seany1212

    Seany1212

    Joined:
    Jul 6, 2006
    Messages:
    662 (0.22/day)
    Thanks Received:
    23
    Location:
    England, UK
    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 :respect:

    // 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;
    }
  2. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (5.08/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    Code:
            [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 :toast:

    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.
    Last edited: Dec 19, 2010
    Seany1212 and Ross211 say thanks.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page