Array/Collection Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • markliam@gmail.com

    #1

    Array/Collection Question

    How would I declare a fixed number of ColumHeader objects without
    having to manually type "new ColumnHeader()" for each one? Is it even
    possible? I'm thinking there has to be an easier way than this:

    // CREATE LISTVIEW COLUMNS

    ColumnHeader[] columns = { new ColumnHeader(), new
    ColumnHeader() }; // this is what I'd like to shorten

    columns[0].Text = "Test Column";
    columns[0].Width = 100;

    columns[1].Text = "Test Column #2";
    columns[1].Width = 200;

    listView1.Colum ns.AddRange(col umns);

  • Peter Duniho

    #2
    Re: Array/Collection Question

    markliam@gmail. com wrote:
    How would I declare a fixed number of ColumHeader objects without
    having to manually type "new ColumnHeader()" for each one? Is it even
    possible?
    Not really. Not with a reference type. At some point, each
    ColumnHeader instance needs to be instantiated. That only happens by
    using the "new" operator.

    If you were dealing with a lot of instances (20, for example), it might
    be more efficient to do something like this:

    ColumnHeader[] columns = new ColumnHeader[20];

    for (int i = 0; i < columns.Length; i++)
    {
    columns[i] = new ColumnHeader();
    }

    But if you're only dealing with two, that's not going to save you any
    typing.

    You could combine the above with the code you posted, resulting in
    something like this:

    ColumnHeader[] columns = new ColumnHeader[2];

    columns[0] = new ColumnHeader();
    columns[0].Text = "Test Column";
    columns[0].Width = 100;

    columns[1] = new ColumnHeader();
    columns[1].Text = "Test Column #2";
    columns[1].Width = 200;

    Then at least if you're copying and pasting the skeleton of your
    initialization code, you just have to type the initialization once. But
    again, if you only have two elements, that's not likely to save you any
    typing.

    Finally, since I don't think I've made this complicated enough :), in
    the example you gave you seem to be initializing each column
    identically. In that case, I prefer to have some kind of data and a
    loop. Again, not necessarily less typing for a very small number of
    instances, but perhaps more maintainable, and more easily updated:

    string[] rgstrText = { "Test Column", "Test Column #2" };
    int[] rgdxWidth = { 100, 200 };

    ColumnHeader[] columns = new ColumnHeader[rgstrText.Lengt h];

    for (int i = 0; i < columns.Length; columns++)
    {
    ColumnHeader column = new ColumnHeader();

    column.Text = rgstrText[i];
    column.Width = rgdxWidth[i];
    columns[i] = column;
    }

    If you were performing this initialization on a regular basis, you would
    make the data arrays some kind of constant or pre-initialized data,
    rather than doing it each time through, of course.

    Note that with this method, you could actually get rid of the "columns"
    array altogether, and just call listView1.Colum ns.Add each time at the
    end of the loop.

    Hope that helps.

    Pete

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Array/Collection Question

      <markliam@gmail .comwrote:
      How would I declare a fixed number of ColumHeader objects without
      having to manually type "new ColumnHeader()" for each one? Is it even
      possible? I'm thinking there has to be an easier way than this:
      >
      // CREATE LISTVIEW COLUMNS
      >
      ColumnHeader[] columns = { new ColumnHeader(), new
      ColumnHeader() }; // this is what I'd like to shorten
      >
      columns[0].Text = "Test Column";
      columns[0].Width = 100;
      >
      columns[1].Text = "Test Column #2";
      columns[1].Width = 200;
      >
      listView1.Colum ns.AddRange(col umns);
      Well, in C# 3 you could do:

      ColumnHeader[] columns = new []
      {
      new ColumnHeader { Text="Test Column", Width=100 },
      new ColumnHeader { Text="Test Column #2", Width=200 }
      };

      Before C# 3 there isn't much you can do. You could write a little
      method like this:

      static ColumnHeader[] CreateColumnHea ders(int count)
      {
      ColumnHeader[] ret = new ColumnHeader[count];
      for (int i=0; i < count; i++)
      {
      ret[i] = new ColumnHeader();
      }
      }

      That could save a bit of code if you've got a lot of columns...

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      Working...