How do you store a stoopid array in the stoopid ViewState?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




I thought it was something relatively simple:

ViewState["SomeObj"] = SomeObj;

Then:

SomeObj = ViewState["SomeObj"];


So, in my own code, I have this on the initial load:
UInt32[] int_array = new UInt32[11];

... and:

ViewState["int_array"] = int_array;



THEN, on postback, I have this:

UInt32[] int_array = (UInt32[]) ViewState["int_array"];

... and:

for (UInt32 nn = 0; nn < 11; nn++)
int_array[nn] += 1;



On the FOR loop, an exception gets thrown:
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Server Error in '/Webfolder01' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference
not set to an instance of an object.

Source Error:


Line 84: for (UInt32 nn = 0; nn < 11; nn++)
Line 85: zxc[nn] += 1;
Line 86:
Line 87: for (UInt32 nn = 0; nn < 11; nn++)

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-



Does anyone know what I need to do to store a stoopid array
in the stoopid ViewState?

It would improve my mental health to know this.



BTW: Is there any way to create an immediate array (from the program stack) in C#?

It seems like the only way to create an array is to dynamically allocate
the space for it and assign the pointer to a reference type.

IOW, In C++, it's sort of like this:

int (*int_array)[] = farmalloc (11);


... instead of like this:

int int_array[11];

I don't see anything in any docs on C# that will let you do that.

Did I miss something?



My Page_Init handler is as follows:
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

public void Page_Init (object sender, EventArgs e)
{
int ndx = 3;

if (!Page.IsPostBack)
{ string msg1;
UInt32[] int_array = new UInt32[11];

msg1 = " first load: |";
for (UInt32 nn = 0; nn < 11; nn++)
int_array[nn] = 100 - nn;

for (UInt32 nn = 0; nn < 11; nn++)
msg1 = msg1 + int_array[nn].ToString() + "|";

msg1 = msg1 + "<br>";

Dyn_Control_Placeholder.Controls.Add
( new LiteralControl(msg1) );

ViewState["int_array"] = int_array;
}
else
{ string msg1 = " On postback: |";

UInt32[] int_array = (UInt32[]) ViewState["int_array"];

for (UInt32 nn = 0; nn < 11; nn++)
int_array[nn] += 1;

for (UInt32 nn = 0; nn < 11; nn++)
msg1 = msg1 + int_array[nn].ToString() + "|";

msg1 = msg1 + "<br>";

Dyn_Control_Placeholder.Controls.Add
( new LiteralControl(msg1) );
}

Dyn_Control_Placeholder.Controls.Add
( new LiteralControl("<br>||| added 1 on init |||<br><br>") );

xPage_Load (sender, e);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */



THANKS!!!

- wASP
.



Relevant Pages

  • Re: Creating & accessing an application wide array.
    ... Which seems to work (well at least doesn't give me an exception). ... "Object reference not set to an instance of an object." ... dynamically create the array once, so it seems that it cannot ... > be defined in either the "Global.asax" module or the "HttpSessionState" ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Jagged array problem
    ... Exception System.NullReferenceException was thrown in debuggee: ... Object reference not set to an instance of an object. ... When you create an array, its elements are initialized to the default ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: arrays and structures - help needed please
    ... > I am getting the exception, "object reference not set to ... you won't have created an actual array. ... If replying to the group, please do not mail me too ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regex question - trying to find ".mp3" in a SELECT box
    ... But `Array' is a Function object reference. ... Establish a new execution context using F's FormalParameterList, ... Since is not a primitive value, the exception should be thrown. ... | 5.2 Algorithm Conventions ...
    (comp.lang.javascript)
  • Re: An isArray test (and IE bugs)
    ... is not 'Array' must not be an Array, ... You have no choice but to catch every exception that is thrown in a - try - block, but only the exceptions that you expected to be catching should be handled in the - catch - block. ... That suggests that each - catch - block should contain the code to identify the exact type and nature of the exception thrown so that all the exceptions that you did not expect to be catching can be re-thrown. ... So while you could examine the strings in an given set of browsers/browser configurations you are going to hard pressed to be in a position to identify the exception in a browser that you have never seen. ...
    (comp.lang.javascript)