PC Review


Reply
 
 
jan82
Guest
Posts: n/a
 
      14th Jul 2006
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

 
Reply With Quote
 
 
 
 
Michael C
Guest
Posts: n/a
 
      14th Jul 2006
"jan82" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

>
> Thnx!
>
> Jan
>



 
Reply With Quote
 
jan82
Guest
Posts: n/a
 
      14th Jul 2006

Michael C schreef:

> "jan82" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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.

 
Reply With Quote
 
TheSteph
Guest
Posts: n/a
 
      14th Jul 2006

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 };


"jan82" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

>
> Michael C schreef:
>
> > "jan82" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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.
>



 
Reply With Quote
 
jan82
Guest
Posts: n/a
 
      14th Jul 2006
I do it this way:

private static TextBox[] orderCTNTextBox;

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

protected void btnSave_Click(object sender, EventArgs e)
{
for(blahblah)
{
somevar = orderCTNTextBox[i].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 };
>
>
> "jan82" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> >
> > Michael C schreef:
> >
> > > "jan82" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > > > 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.
> >


 
Reply With Quote
 
chrisp
Guest
Posts: n/a
 
      14th Jul 2006
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


"jan82" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      14th Jul 2006
Hi,


"jan82" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      14th Jul 2006
Hi,

"jan82" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I do it this way:
>
> private static TextBox[] orderCTNTextBox;
>
> private void buildTable()
> {
> while (blahblah)
> {
> orderCTNTextBox[i] = new TextBox(); //i is a counter
> orderCTNTextBox[i].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[i].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;



--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


 
Reply With Quote
 
Sericinus hunter
Guest
Posts: n/a
 
      14th Jul 2006
Can you show where and how you create the array?

jan82 wrote:
> I do it this way:
>
> private static TextBox[] orderCTNTextBox;


> private void buildTable()
> {
> while (blahblah)
> {
> orderCTNTextBox[i] = new TextBox(); //i is a counter
> orderCTNTextBox[i].Text = "blah blah";
> }
> }
>
> protected void btnSave_Click(object sender, EventArgs e)
> {
> for(blahblah)
> {
> somevar = orderCTNTextBox[i].Text; //and here it goes
> wrong...
> }
> }

 
Reply With Quote
 
Thomas T. Veldhouse
Guest
Posts: n/a
 
      14th Jul 2006
Sericinus hunter <(E-Mail Removed)> wrote:
> 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.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scope of Variant / Array =?Utf-8?B?S2l0Q2F6?= Microsoft Access Form Coding 7 25th Sep 2007 03:20 PM
Property: ReadOnly on public scope while Read-Write on friend or private scope Saran Microsoft VB .NET 2 19th Jul 2006 11:13 AM
Try/Catch Array Scope Problem Matt Harvey Microsoft C# .NET 4 25th Apr 2006 08:15 PM
Array vs arraylist variable scope Peter Microsoft VB .NET 4 19th Oct 2004 05:15 PM
ADSI returning groups in Global scope and Domain local scope instead of Universal scope Maziar Aflatoun Microsoft C# .NET 1 21st Apr 2004 04:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:38 PM.