943,882 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 6711
  • C# RSS
Jan 19th, 2009
0

String Array

Expand Post »
Is there any method to find the number of elements that are filled in an array

lets say i have the following array defined

string[] text = new string[50]

and i fill the first 8 elements/indexes

Is there any method that could return the number of elements/indexes within an string array that are filled?

I tried using text.lenght() but that returns the total size of array. In above case it would return 50 which is not what i want...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gallian99 is offline Offline
77 posts
since Jan 2009
Jan 19th, 2009
0

Re: String Array

Hello, gallian99.
I haven't heard about such standard functions I can offer two alternative ways for you:
1. Using reference types (like ArrayList). You'll able to add elements dynamically and get the actual size of your array.
2. Create your own structure/class, that will contain your array, the full array size (in your case it's 50) and number of added elements.
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Jan 19th, 2009
0

Re: String Array

Thanks Anteka,

I have tackled it using an alternate already. But I should better try ArrayList as well. Haven't work around with it yet

Click to Expand / Collapse  Quote originally posted by Antenka ...
Hello, gallian99.
I haven't heard about such standard functions I can offer two alternative ways for you:
1. Using reference types (like ArrayList). You'll able to add elements dynamically and get the actual size of your array.
2. Create your own structure/class, that will contain your array, the full array size (in your case it's 50) and number of added elements.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gallian99 is offline Offline
77 posts
since Jan 2009
Jan 19th, 2009
0

Re: String Array

If you are running on .NET 3, you can use Lists, which are very easy:

C# Syntax (Toggle Plain Text)
  1. List<String> strings = new List<String>(); //initialise the list
  2. strings.Add(str); //Add an item
  3. int length = strings.Count; //how many strings are in the list.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Jan 19th, 2009
0

Re: String Array

hi
u can do this

C# Syntax (Toggle Plain Text)
  1. int counter=0; // global value
  2. while(true)
  3. {
  4.  
  5. if (s[counter] != null)
  6. {
  7. comboBox1.Items.Add(s[counter]);
  8. counter++;
  9. }
  10. else
  11. {
  12. break;
  13. }
  14. }

bye
Reputation Points: 28
Solved Threads: 5
Light Poster
BlackSun is offline Offline
46 posts
since Feb 2008
Jan 19th, 2009
1

Re: String Array

You should follow scru's advice and use a List<T> if you want to have a resizable container.
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Jan 21st, 2009
0

Re: String Array

An ArrayList is also resizable, still usable but a bit "out of date".
If I where you follow the advise off scru and Rashakil Fol.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,740 posts
since Oct 2008
Jan 21st, 2009
0

Re: String Array

Thanks!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
gallian99 is offline Offline
77 posts
since Jan 2009
Jan 22nd, 2009
0

Re: String Array

try this ??? is easy way to find

C# Syntax (Toggle Plain Text)
  1. int I = 0;
  2. foreach (string S in Text)
  3. {
  4. int I += 1;
  5. }
  6. Console.WriteLine(I.ToString());
cVz
Reputation Points: 29
Solved Threads: 7
Junior Poster
cVz is offline Offline
139 posts
since Mar 2008
Jan 22nd, 2009
2

Re: String Array

Click to Expand / Collapse  Quote originally posted by cVz ...
try this ??? is easy way to find
Well this is about the dumbest reply I've read on this forum. Did you even read the question?
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Platform Builder and C#
Next Thread in C# Forum Timeline: Help with RegEx and matchcollection screen scraping





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC