473,324 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

Aug 30 '07 #1
2 1315
ma******@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.Length];

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.Columns.Add each time at the
end of the loop.

Hope that helps.

Pete
Aug 30 '07 #2
<ma******@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.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[i] = new ColumnHeader();
}
}

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: | last post by:
I'm going a little crazy trying to learn how to use arrays as properties in VBScript classes. Hopefully someone can help. First, I can't figure out whether it's possible to iterate through the...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
7
by: Marc Bishop | last post by:
Hi can anyone help? I'm making a shopping cart and am stuck on removing an item from my array? The array is made : cArray(ITEM_NAME,cItem) = ProductName
5
by: Geoff Jones | last post by:
Hi Suppose we have an array of DataRows e.g. Dim drMyRows() As DataRow = myTable.Rows(0).GetChildRows("PricesCompany") generated via a relationship. The question I have is this. Isn't...
14
by: Jim Burns | last post by:
I just started with vb.net and I don't understand why there are no control arrays. what replaced them. I was trying to do drag and drop(What happened with that) and it looked like with no control...
4
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
3
by: karmenkrile | last post by:
In html i have some question, and the answers are radio buttons ... the names of variables in html are array ... like question, question ... etc. and every question has multiple value, depending...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.