How Do You Clear an Array at Runtime?

Jan 25 2005 3:34 AM
Hi all. I have created a Windows application that is linked to a database. (You would think that, if I can do all that, why don't I know this?) (Also note that someone has mentioned to me something about using a 2 dimensional array, although I'm not sure how to do it and is probably a question for another thread.) Anyway, I am using 16 arrays. These arrays hold the primary data for the application. Here are the arrays: double[] billDbl = new double[30], incomeDbl = new double[30]; double[] sda1Dbl = new double[9], sda2Dbl = new double[9]; double[] sda3Dbl = new double[9], cda1Dbl = new double[9]; double[] cda2Dbl = new double[9], cda3Dbl = new double[9]; double[] swa1Dbl = new double[9], swa2Dbl = new double[9]; double[] swa3Dbl = new double[9], cwa1Dbl = new double[9]; double[] cwa2Dbl = new double[9], cwa3Dbl = new double[9]; string[] billDes = new string[30] incomeDes = new string[30]; The index for each array is unique and must also be updated as items are deleted or loaded. For instance, if 3 bills are added, billDbl[0], billDbl[1], and billDbl[2] contain values. If we use the int variable 'b' as the index for billDbl, then 'b' would now have the value of 2. What I'm getting at is that 'b' would have to be set equal to 0, if all array elements were set to 0 (for double arrays) or null (for string arrays). Here are the integer variables for the above arrays: int b = 0, sda1 = 0, sda2 = 0, sda3 = 0, swa1 = 0, swa2 = 0, swa3 = 0; int cda1 = 0, cda2 = 0, cda3 = 0, cwa1 = 0, cwa2 = 0, cwa3 = 0, i = 0; All that being said, I realize that I could clear all the arrays like this: int x = 0; while (x < 30) { billDbl[x] = 0; x = x + 1; } b=0;//Here the integer variable would be set to zero after the array was cleared to prevent any out of range exceptions caused by loops later on, not to mention the fact that if b was still set to 2 it would only allow 27 total bills instead of the 30 possible (lol and that's just a couple of the errors it would cause). All arrays could be cleared in a similar fashion. My question is, (I know you're all saying finally), is there a way that I could set all double array elements to 0 and all string array elements to null when someone clicks on a Menu Item called New Budget without using all those loops? I have seen a method in the msdn library. Here is what it shows: void IList.Clear(); There is also another method there. This is what that one looks like: public static void Clear(Array array, int index, int length); How could I put one of these in my code and actually get it to work? I tried using: IList.Clear(); in my click event, but it wouldn't even bring up clear as an option. (It would bring it up if I used it as its own void. The problem is that I don't know how to connect my click event with that.) I guess the real question is how to actually use this code.

Answers (8)