?
Solved

How to change the length of the array at run time.

Posted on 2009-02-19
8
Medium Priority
?
603 Views
Last Modified: 2012-08-13
I have an array of datarows. I have couple of questions here:

1. Is is necessary to assign the length of the array at the design time? Can we not set it at run time.

2. Lets suppose we have set the length of the array at the design time. This needs to be changed at run time depends upon the number of elements filled in the array. Can we do that.

Please see the code attached. I have an array of datarows. I have given assigned it the length of 10 at design time. This I assume I had to because if I was leaving it blank it was giving me error saying 'Index is out of boud'. At run time I fill this array with few rows. lets suppose 2 rows. Now the initial length declared was 10 and it contains only 2 elements. How can I change the length of the array.
DataRow[] rowsToDel = new DataRow[10];
 
            int totCols = ds.Tables[0].Columns.Count;
            int totRows = ds.Tables[0].Rows.Count;
 
            int rowDelCnt = 0;
 
            foreach (DataRow dR in ds.Tables[0].Rows )
            {
                Boolean isDel = (Boolean) dR[totCols-1];
                if (isDel)
                {
                    rowsToDel[rowDelCnt] = dR;
                    rowDelCnt++;
                }
            }

Open in new window

0
Comment
Question by:shieldguy
8 Comments
 
LVL 19

Expert Comment

by:daveamour
ID: 23680348
You can say
DataRow[] rowsToDel = new DataRow[ds.Tables[0].Rows.Count];
I think count is there off the top of my head

0
 
LVL 19

Expert Comment

by:daveamour
ID: 23680366
oops sorry just realised you only want certain rows!
However you can see that yews it can be set at design time  - you just need different code to mine that's all.
Note also that you can use ArrayList
0
 
LVL 19

Expert Comment

by:daveamour
ID: 23680373
Sorry excuse my typing meant to say yes it can be set at run time (not design time)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:masheik
ID: 23680462
0
 
LVL 1

Author Comment

by:shieldguy
ID: 23680463
do I need to assign the length before I fill in the elements or after.
0
 
LVL 19

Expert Comment

by:daveamour
ID: 23680469
for an array before
for an ArrayList you don't you just add them ad hoc
0
 
LVL 1

Accepted Solution

by:
shieldguy earned 0 total points
ID: 23681835
I have found the right solution of it. Please see the function below

Array.Resize(ref "arrayName", "Newsize")
Array.Resize(ref rowsToDel, rowDelCnt)

Thanks for all of your help
0
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 23691535
I would not say thats the simplest or best way.

in 1.1 use an ArrayList

otherwise I'd use generics and List<DataRow>

List<DataRow> rowsToDel = new List<DataRow>();
 
int totCols = ds.Tables[0].Columns.Count;
int totRows = ds.Tables[0].Rows.Count;
 
int rowDelCnt = 0;
 
foreach (DataRow dR in ds.Tables[0].Rows )
{
	Boolean isDel = (Boolean) dR[totCols-1];
	if (isDel)
	{
		rowsToDel.Add(dR);
	}
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video teaches viewers about errors in exception handling.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question