Array/Collection Question

M

markliam

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.Columns.AddRange(columns);
 
P

Peter Duniho

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 = 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.Length];

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

column.Text = rgstrText;
column.Width = rgdxWidth;
columns = 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.Columns.Add each time at the
end of the loop.

Hope that helps.

Pete
 
J

Jon Skeet [C# MVP]

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.Columns.AddRange(columns);

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[] CreateColumnHeaders(int count)
{
ColumnHeader[] ret = new ColumnHeader[count];
for (int i=0; i < count; i++)
{
ret = new ColumnHeader();
}
}

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top