array scope

J

jan82

Hi everybody

I declared a private static array of textboxes at the top of a class,

then i generate the array elements (new textbox()) in a separate
function (private void in the same class),

then i try to call these elements from another function in the same
class (protected void)

and that causes a nullreference exception. Can anybody tell me what
i'm doing wrong?

Thnx!

Jan
 
M

Michael C

jan82 said:
Hi everybody

I declared a private static array of textboxes at the top of a class,

then i generate the array elements (new textbox()) in a separate
function (private void in the same class),

then i try to call these elements from another function in the same
class (protected void)

and that causes a nullreference exception. Can anybody tell me what
i'm doing wrong?

Either the function hasn't been called or something set the array back to
null.

Or maybe you are expecting the new TextBox[] to create textboxes? All it
does it create an array of nulls, you need to create each textbox
individually.
 
J

jan82

Michael C schreef:
jan82 said:
Hi everybody

I declared a private static array of textboxes at the top of a class,

then i generate the array elements (new textbox()) in a separate
function (private void in the same class),

then i try to call these elements from another function in the same
class (protected void)

and that causes a nullreference exception. Can anybody tell me what
i'm doing wrong?

Either the function hasn't been called or something set the array back to
null.

Or maybe you are expecting the new TextBox[] to create textboxes? All it
does it create an array of nulls, you need to create each textbox
individually.

I create each textbox individually, and I show them on the screen, so
they are created. Then when trying to get the "text" property of these
textboxes from another function (in the same class), I get the error.
 
T

TheSteph

Don't you forget to Add the TextBoxes you create in the Array ?

TextBox Mt1 = new TextBox();
TextBox Mt2 = new TextBox();
//Create Array and Add the TextBox's
TextBox[] MyArray = new TextBox[] { Mt1, Mt2 };


Michael C schreef:
jan82 said:
Hi everybody

I declared a private static array of textboxes at the top of a class,

then i generate the array elements (new textbox()) in a separate
function (private void in the same class),

then i try to call these elements from another function in the same
class (protected void)

and that causes a nullreference exception. Can anybody tell me what
i'm doing wrong?

Either the function hasn't been called or something set the array back to
null.

Or maybe you are expecting the new TextBox[] to create textboxes? All it
does it create an array of nulls, you need to create each textbox
individually.

I create each textbox individually, and I show them on the screen, so
they are created. Then when trying to get the "text" property of these
textboxes from another function (in the same class), I get the error.
 
J

jan82

I do it this way:

private static TextBox[] orderCTNTextBox;

private void buildTable()
{
while (blahblah)
{
orderCTNTextBox = new TextBox(); //i is a counter
orderCTNTextBox.Text = "blah blah";
}
}

protected void btnSave_Click(object sender, EventArgs e)
{
for(blahblah)
{
somevar = orderCTNTextBox.Text; //and here it goes
wrong...
}
}

TheSteph schreef:
Don't you forget to Add the TextBoxes you create in the Array ?

TextBox Mt1 = new TextBox();
TextBox Mt2 = new TextBox();
//Create Array and Add the TextBox's
TextBox[] MyArray = new TextBox[] { Mt1, Mt2 };


Michael C schreef:
Hi everybody

I declared a private static array of textboxes at the top of a class,

then i generate the array elements (new textbox()) in a separate
function (private void in the same class),

then i try to call these elements from another function in the same
class (protected void)

and that causes a nullreference exception. Can anybody tell me what
i'm doing wrong?

Either the function hasn't been called or something set the array back to
null.

Or maybe you are expecting the new TextBox[] to create textboxes? All it
does it create an array of nulls, you need to create each textbox
individually.

I create each textbox individually, and I show them on the screen, so
they are created. Then when trying to get the "text" property of these
textboxes from another function (in the same class), I get the error.
 
C

chrisp

Hi Jan,

I came across something similar to this with a static array where it was
being reset to its initialised state after it had been previously
referenced.

Initialising the array in a static constructor rather than in the array's
declaration solved the problem I was experiencing. Maybe someone with more
indepth knowledge can explain why, but this may be the solution whatever the
reason.

HTH, Chris
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


jan82 said:
Hi everybody

I declared a private static array of textboxes at the top of a class,

Are you sure this is the best way?
A textbox (in a window form) is an instance component. All the instances of
that forms ahve their own instances of textboxes.

Not a good idea have it static!

Why are you doing this?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

jan82 said:
I do it this way:

private static TextBox[] orderCTNTextBox;

private void buildTable()
{
while (blahblah)
{
orderCTNTextBox = new TextBox(); //i is a counter
orderCTNTextBox.Text = "blah blah";
}
}


Are you inserting this textboxes in the Controls collection of the page?

protected void btnSave_Click(object sender, EventArgs e)
{
for(blahblah)
{
somevar = orderCTNTextBox.Text; //and here it goes
wrong...
}
}


What is the value of i ?
why dont you modify the code to something like

while( i < orderCTNTextBox.Length)
somevar = orderCTNTextBox[i++].Text;
 
S

Sericinus hunter

Can you show where and how you create the array?
I do it this way:

private static TextBox[] orderCTNTextBox;
private void buildTable()
{
while (blahblah)
{
orderCTNTextBox = new TextBox(); //i is a counter
orderCTNTextBox.Text = "blah blah";
}
}

protected void btnSave_Click(object sender, EventArgs e)
{
for(blahblah)
{
somevar = orderCTNTextBox.Text; //and here it goes
wrong...
}
}
 
T

Thomas T. Veldhouse

Sericinus hunter said:
Can you show where and how you create the array?

Yes, and why is the array static? Multiple instances of that class will reset
the same array if it is a static scope.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top