473,387 Members | 1,891 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,387 software developers and data experts.

How to initialize array dynamically?

I need an array but don't know how many items will go into it upon
declaration.

int ArrayIndex = 0;
string[] linkArray = new string[]{};

for (int i = 0; i < Links.Count; i++)
{
linkArray[ArrayIndex] = u.AbsoluteUri;
ArrayIndex++;
}

The first time through the loop, I get an out of bounds error. How exactly
does the array need to work in this case?

Thanks,
Brett
Nov 17 '05 #1
2 6631
"Brett" <no@spam.com> wrote in message
news:ul*************@TK2MSFTNGP15.phx.gbl...
I need an array but don't know how many items will go into it upon
declaration.


Hi. Use an ArrayList instead.

System.Collections.ArrayList linkArrayList = new
System.Collections.ArrayList();
for (int i = 0; i < Links.Count; i++) linkArrayList.Add(u.AbsoluteUri);
string[] linkArray = (string[]) linkArrayList.ToArray(typeof(string));

-- Alan
Nov 17 '05 #2
Brett <no@spam.com> wrote:
I need an array but don't know how many items will go into it upon
declaration.

int ArrayIndex = 0;
string[] linkArray = new string[]{};

for (int i = 0; i < Links.Count; i++)
{
linkArray[ArrayIndex] = u.AbsoluteUri;
ArrayIndex++;
}

The first time through the loop, I get an out of bounds error. How exactly
does the array need to work in this case?


In this case, it's easy - you *do* know the number of links:

string[] linkArray = new string[Links.Count];

(You don't need the ArrayIndex variable either; just use i.)

If you genuinely don't know ahead of time how many items you need, use
an ArrayList and if you need to, call ToArray on the ArrayList.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3

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

Similar topics

6
by: SamMan | last post by:
Is there an easy way to re-initialize an array, other than looping through it's length and setting the values to null, or ""? Would unset() be a wise choice? Thanks again -- SamMan
6
by: steveneng | last post by:
C++ Primer Plus Programming Exercises 4th Ed - Prate Help I'm trying to refresh myself and I'm stuck on this problem (not homework/school related but for personal advancement). 6: Do...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
4
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
2
by: deko | last post by:
I trying to create a jagged array of two arrays, with the second array being an array of two-dimensional arrays. A graphical representation might look like this: x y y y x y x y y x ...
38
by: Zytan | last post by:
What is the difference between these two lines? Dim args As Object() = New Object() {strText} Dim args As Object() = {strText} args seems usuable from either, say, like so: ...
18
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: ...
11
by: Bob Altman | last post by:
Hi all, I have a class that contains a member variable that is an array of class instances: class MyClass { private: SomeClass m_someClass; SomeClass m_arrayOfClasses; };
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.