|
-
May 25th, 2011, 01:13 PM
#1
[RESOLVED] Dynamic Object Names
I am needing a little help on how to create dynamic object names for a listview of unknown size.
Code:
ColumnHeader dynamicName = new ColumnHeader();
I have the following and I know I am close I just can not figure out how to increment the object names.
Code:
int i = 0;
while (i != j)
{
string myObject = "myObject" + i.ToString();
MessageBox.Show(myHeadings[i].ToString()); //just for testing to see the data
MessageBox.Show(myObject); //just for testing to see the data
i++;
ColumnHeader myObject//<-- what I have issues with // = new ColumnHeader();
listView1.Columns.Add(myHeadings[i].ToString(),200);
}
-
May 25th, 2011, 02:01 PM
#2
Re: Dynamic Object Names
Short tip:
Code:
while (i < j)
listView1.Columns.Add(String.Format("myObject{0}", i), 200);
Things to consider about what you've done...
* You are using the same variable name twice, "myObject", once as a string, and again as a ColumnHeader. Which is it? You assign a string value to it, but then you try to create another new object of the same name... why is that?
* You are refering to an array "myHeadings[]", where is this created? Where are you adding items to that array? What type of objects does this array have in it?
-
May 26th, 2011, 09:29 AM
#3
Re: Dynamic Object Names
Thank you for your answer, however that is not exactly what I am having issues with but do like your code.
I am having issues with actually creating each specific column.
For example:
ColumnHeader columnHeading1 = new ColumnHeader();
columnHeading1 is what I need to increment. I am trying create the columnHeaders for a ListView with out knowing the exact number of columns in a csv file. I have been able to pull out the headers for the csv file and have stored them in the myHeadings[] array. From what I know of C# the code that you advise will not actually create the column headers just set the text and width of the columnheaders. Their is still the issue of actually creating each column heading. In the past I have created them like below but I knew the number of columns before creation.
Code:
ColumnHeader column1 = new ColumnHeader();
ColumnHeader column2 = new ColumnHeader();
ColumnHeader column3 = new ColumnHeader();
..... and so on
To answer your questions...
Things to consider about what you've done...
* You are using the same variable name twice, "myObject", once as a string, and again as a ColumnHeader. Which is it? You assign a string value to it, but then you try to create another new object of the same name... why is that? --> I am not using it as a columnHeader I am trying to use it as a name of an instance of the ColumnHeader object.
* You are refering to an array "myHeadings[]", where is this created? Where are you adding items to that array? What type of objects does this array have in it? --> it is just an array of string values that I pull from the csv file and then use the .Split(',') to load the array. I use the .Length function to get the number in the array and then increment through the array to pull the string values.
-
May 26th, 2011, 11:28 AM
#4
Re: Dynamic Object Names
Okay...
Code:
string[] columnNames = new string[] { "one", "two", "three" };
foreach (string colName in columnNames)
{
this.listView1.Columns.Add(colName, 200);
}
That code works perfectly fine... the reason is that listview1.Columns.Add() has an overload that will accept (string, width) and create a new column using that information for you.
You can still do it this way if it is more to your liking...
Code:
string[] columnNames = new string[] { "one", "two", "three" };
for (int colIndex = 0; colIndex < columnNames.Length; colIndex++)
{
ColumnHeader newHeader = new ColumnHeader();
newHeader.Text = columnNames[colIndex];
newHeader.Width = 200;
this.listView1.Columns.Add(newHeader);
}
The problem with using the same variable name twice, is the compiler can't know what you want, do you want a string, or a ColumnHeader called myObject? Once it is initialized as a string, and the compiler sees you try to initialize it again as a ColumnHeader, it attempts to convert it, which fails.
I hope this helps out
-
May 27th, 2011, 08:39 AM
#5
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|