Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the variable "can_Count" is getting the value of 5.but still this code causes for an out of range exception.The purpose of this code is to create a number of dynamic buttons(it should create no of buttons which the can_Count variable holds.) I can't find what is wrong with this code.can anyone help me?

public static int can_Count = 0;
//there is a function which count the no of rows in a table n return it to can_Count

static int a_size = (can_Count + 1);
public Button[] bu1 = new Button[a_size];

private void Form9_Load(object sender, EventArgs e)
{
 x = 40;
 for (int i = 1; i <=can_Count; i++)
 {
  bu1[i] = new Button();
  bu1[i].Name = "BI" + i;
  bu1[i].Text = "1";
  bu1[i].Location = new Point(20, x);
  x = x + 75;
  bu1[i].Click += new EventHandler(button1_Click);
 }
this.Controls.AddRange(bu1);
}
Posted
Updated 1-Jul-11 10:14am
v3

As mr Simmons stated, this is where you're wrong:
for (int i = 1; i <=can_Count; i++)


It should be:

for (int i = 1; i < can_count; i++)


You're getting the system out of range exception because you're trying to access data in the array where there is none.

Example, say your array has a count of 4. The first element in the array is located in array[0] and the last in array[3]. array[4] does not exist and if you're trying to access information in it, you will get the system out of range exception.
0,1,2,3 is actually 4 elements in the array

This happens when your looping for i <=can_Count, it should be i <can_Count.
 
Share this answer
 
v2
Indexes are 0-based in .Net, so you need to do this (assuming bu1 is already initialized to the proper dimensions):

C#
for (int i = 1; i < can_count; i++)
{
    ....
}


EDIT

I would do it this way:

C#
for (int i = 1; i < bu1.Length; i++)
{
    Button button = new Button() {
                                   .Name     = string.Format("BI{0}", i),
                                   .Text     = i.ToString(),
                                   .Location = new Point(20, x)
                                 }
    x += 75;
    bu1[i] = button;
}


And notice that in your version, you're trying to add an integer value to a string (where you set the Name property of the button). That will also throw a range exception.
 
Share this answer
 
v9
Comments
Member 7779792 1-Jul-11 13:33pm    
no i need to fill the array starting from index 1
#realJSOP 1-Jul-11 13:47pm    
Revised answer
Member 7779792 1-Jul-11 13:56pm    
still it gives the same exception.i need to fill the array starting from 1 and up to the size of can_Count.since can_Count is holding the value of 5, the for loop should iterate 5 times.which means the bu1 array should be filled from 1 to 5.this is what i need to do.but this code won't execute as i was expected,please can any one help me??
Christian Graus 1-Jul-11 19:11pm    
You do NOT need to start an array from one, but if there's some insane reason why you want to create arrays with a 0 index object that is never used, you just need to iterate over the items you need. Your error is caused by the 'off by one' issue you are creating, so create an array that's too big, in order to hold the extra element you insist on creating. That you say this code MUST start at 1, tells me you really are lost and confused. There's no reason for it. I suggest you learn to use a debugger, read some basic books, and try again.
I'am not sure if i get you right, but this code creates 5 buttons without any exception generated:

C#
public static int can_Count = 5;
       //there is a function which count the no of rows in a table n return it to can_Count

       static int a_size = (can_Count + 1);
       public Button[] bu1 = new Button[a_size];



       private void button1_Click(object sender, EventArgs e)
       {
           int x = 40;
           for (int i = 1; i <= can_Count; i++)
           {
               bu1[i] = new Button();
               bu1[i].Name = "BI" + i;
               bu1[i].Text = "1";
               bu1[i].Location = new Point(20, x);
               x = x + 75;

           }
           this.Controls.AddRange(bu1);

       }
 
Share this answer
 
If canCount is updated then a_size should probably not be static. Are you sure that a_size is updated when a_count is updated?

You could add some call to Assert to validate that your code is correct. For example, you could add:

System.Diagnostics.Debug.Assert(a_size == canCount + 1)
before the new and variation of that in your loop.

That way when running the code (debug), an exception would be thrown if something was done incorrectly. Using assertions properly help a lot to find errors as soon as possible.
 
Share this answer
 
I believe that Philippe is correct. That is, a_size is getting initialized to 1. Changing canCount after that has no effect on a_size. If you run this in the debugger, I think you will find that a_size == 1, so you have a one-element array. Therefore, the only valid index is 0.

Furthermore, even if you fix that, you are leaving bu1[0] uninitialized (probably null). That may cause an exception at the statement 'this.Controls.AddRange(bu1)'.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900