1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Other C# Function

Discussion in 'Software' started by Comfyasabadger, 18 Jul 2012.

  1. Comfyasabadger

    Comfyasabadger What's a Dremel?

    Joined:
    23 Jun 2009
    Posts:
    607
    Likes Received:
    28
    Does anyone know if it's possible to loop through the parameters of a function and return the object type and in the instance of say int and string the object value?

    Code:
    xlSheet.Row CreateContentRow(int index, string user, string limit, string card)
        {
            string[] headerColumns = new string[] { "A", "B", "C" };
            //Create the new row.
            xlSheet.Row r = new xlSheet.Row();
            r.RowIndex = (UInt32)index;
            [B]xlSheet.Cell firstCell = CreateTextCell(headerColumns[0], user, index);
            xlSheet.Cell secondCell = CreateTextCell(headerColumns[1], limit, index);
            xlSheet.Cell thirdCell = CreateTextCell(headerColumns[2], card, index);
            r.AppendChild(firstCell);
            r.AppendChild(secondCell);
            r.AppendChild(thirdCell);[/B]
            return r;
        }
    
    FunctionObjects(CreateContentRow)
    {
    foreach(object obj in CreateContentRow.Objects)
    {
    if(obj.type == string)
    {
    string test = obj.value;
    }
    }
    }
    Was just wondering whether it was possible to compress the 6 bold lines into 2?
     
  2. tehBoris

    tehBoris What's a Dremel?

    Joined:
    30 Jan 2011
    Posts:
    616
    Likes Received:
    25
    stick em in an array and iterate over the array would be the way to avoid code duplication.
     
  3. Comfyasabadger

    Comfyasabadger What's a Dremel?

    Joined:
    23 Jun 2009
    Posts:
    607
    Likes Received:
    28
    Thanks.

    Code:
    xlSheet.Row CreateContentRow(int index, string user, string limit, string card)
        {
    string[,] headerColumns = new string[,] { { "A", "B", "C" }, { user, limit, card } };
            //Create the new row.
            xlSheet.Row r = new xlSheet.Row();
            r.RowIndex = (UInt32)index;
            //Loop to add each element to a cell
            for (int i = 0; i <= 2; i++)
            {
                xlSheet.Cell inputCell = CreateTextCell(headerColumns[0, i], headerColumns[1, i], index);
                r.AppendChild(inputCell);
            }
            return r;
    }
     

Share This Page