hi friends
i tried to create a label array and show label in a button click at runtime .But its not working .it stops execution at the seccon line .If anybody help me ,i would be very thankful to you . . i need a an array of text without a fixed length. just like we declare
in VB.NET [ Dim label () as string ] . is this type of declaration possible in C#
below given is the code i tried
private
void button1_Click(object sender, System.EventArgs e){
Label [] labelnew=
null;int j; for (j=1 ;j<=3;j++)
{
labelnew[j]=
new System.Windows.Forms.Label();labelnew[j].Location=
new System.Drawing.Point(200,j*20);labelnew[j].Name="labelnew"+j;
labelnew[j].TabIndex=0;
}
thanks & regards
pedaammuluPosted Apr 15, 2007, 1:11 PM
In C#, the value of the size of the array marks the number of elements in the array, not the upper bound. Thus, the first element in an array is 0. The following C# statement declares an array of 10 elements, with indices 0 through 9:
string myArray[10];
The upper bound is 9 , not 10, and you can not change the size of the array(that is, there is no equivalent to the VB6 Redim function).
Button[] myButtonArray=new Button[3];
The above statement does not create an array with references to three button objects. Instead, this creates the array myButtonArray with three null referrences.
books teaches three-tier architecture in C# and vb2005
Mike GoldPosted Apr 13, 2007, 1:56 PM
Label[] labelnew = null; needs to be
Label[] labelnew = new Label[3]{null, null, null};
Also, you should start your for loop at 0, because arrays start at 0:
for (j=0 ;j<3;j++)
private void button1_Click(object sender, System.EventArgs e)
{
Label [] labelnew=null;
int j;
for (j=1 ;j<=3;j++)
{
labelnew[j]= new System.Windows.Forms.Label();
labelnew[j].Location= new System.Drawing.Point(200,j*20);
labelnew[j].Name="labelnew"+j;
labelnew[j].TabIndex=0;
}