"Current" item in Array?

  • Thread starter Thread starter Davids
  • Start date Start date
D

Davids

darn stuck again!
in my string array
string[] myArray = string[26];

Let's say I've put a string field into the first 3 items and I guess the
other 23 must then be null, right? OK so is there any C# method/property to
find the next empty (null) item? The Length property always returns the
total items ie 26!
 
Hi,

Why don't you use the ArrayList to put strings into?
Standard arrays (e.g. string[]) have constant length,
so you can't use "Length" to find empty elements.
Real problem starts with value type's arrays that
initialize its values (e.g. int[])

Other solution is to keep "current index" in a variable.

HTH
Marcin
 
exactly that just thought maybe there was some built in function to do
this...


James Curran said:
int HighestUsed(string[] ary)
{
for(int i = 0; i< ary.Length; ++i)
if (ary == null)
return i;
return ary.Length;
}


--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com


Davids said:
darn stuck again!
in my string array
string[] myArray = string[26];

Let's say I've put a string field into the first 3 items and I guess the
other 23 must then be null, right? OK so is there any C# method/property to
find the next empty (null) item? The Length property always returns the
total items ie 26!
 
Davids,
Have you tried Array.IndexOf?

Something like:
string[] myArray = string[26];

int index = Array.IndexOf(myArray, null);

Hope this helps
Jay

Davids said:
darn stuck again!
in my string array
string[] myArray = string[26];

Let's say I've put a string field into the first 3 items and I guess the
other 23 must then be null, right? OK so is there any C# method/property
to find the next empty (null) item? The Length property always returns the
total items ie 26!
 
Back
Top