Populating and Viewing Arrays
-
Hi All, I am currently creating an application that needs to populate a number of arrays and then I need to be able to call them again in different parts of the application. At the moment I have figured out how to Populate an array if I know how many items I am going to have but how do popualte and array when I don't know how many items there are. And :) How do I find out how many items there are in an array once it is populated? This is what I have at the moment ==================== string[] ClinicNames; ClinicNames = new string[99]; int j = 0; while (dReader.Read()) { ClinicNames[j] = dReader.GetString(1); j = j + 1; } ====================
-
Hi All, I am currently creating an application that needs to populate a number of arrays and then I need to be able to call them again in different parts of the application. At the moment I have figured out how to Populate an array if I know how many items I am going to have but how do popualte and array when I don't know how many items there are. And :) How do I find out how many items there are in an array once it is populated? This is what I have at the moment ==================== string[] ClinicNames; ClinicNames = new string[99]; int j = 0; while (dReader.Read()) { ClinicNames[j] = dReader.GetString(1); j = j + 1; } ====================
To find the length of an array, use the Length property:
String[] arr = new String[5];
Console.WriteLine("The array length is {0}.", arr.Length);When you exceed the capacity of your array, you will have to allocate a new array and copy the contents of the old array to the new. You can automate this process by using the System.Collections.ArrayList class which will resize the array when necessary. -- Peter Stephens