Array length

  • Thread starter Thread starter raulavi
  • Start date Start date
R

raulavi

vs 2005 c#
How do I get the number of filled item from an array?
string[] myArr = {"","","",""};

myArr[0] = "one";

myArr.Length = 4;

How do I get the number of filled item from an array in this case 1.

or is any other way to define arrays as unlimited ?

Thanks
 
Arrays are fixed length. It sounds like you want a List<string>, and
Add("one");

Marc
 
vs 2005 c#
How do I get the number of filled item from an array?
string[] myArr = {"","","",""};

myArr[0] = "one";

myArr.Length = 4;

How do I get the number of filled item from an array in this case 1.

or is any other way to define arrays as unlimited ?

Thanks

If you have VC# 2008, you can use this:

string[] myarray = { "", "", "", "" };
myarray[0] = "One";

int count = myarray.Count(s => (s != null && s != ""));

MessageBox.Show("Number of filled items: " +
count.ToString());

Although for a large array, this might not be the best way.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top