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

W

wASP

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
 
G

Guest

That code works perfectly for me... well... I had to remove some stuff:

public void Page_Init (object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
UInt32[] int_array = new UInt32[11];

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

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


ViewState["int_array"] = int_array;
}
else
{
Label1.Text = " 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++)
Label1.Text = Label1.Text + int_array[nn].ToString() + "|";

}
}

Also, what do you mean by "immediate array"?

wASP said:
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
 
G

Guest

To be honest I can't see where there is a problem. The code looks fine....
the error is saying you are trying to add 1 to an object, which would be an
error, but I can't see where it is happening and the code I tested (the one I
posted in my inital response) works fine... I have it in a code behind
file... however, even if you have it inline it should still work.

If line 128 is giving you the error, i.e. "int_array[nn] = int_array[nn] +
1;" then the only thing I can think of is that int_array[nn] is actually
returning an object instead of a UInt32... I can't see why this would be the
case but it seems like it is... so, you could try this changing that line to:

int_array[nn] = ((UInt32) int_array[nn]) + 1;

and if that doesn't work you could try this

int_array[nn] = UInt32.Parse(int_array[nn].ToString()) + 1;


Anyway, seems like you are having a weird problem. As regards your question
about arrays. Unfortunately .NET arrays, even array of value types, are
stored on the heap. The only thing stored on the stack is a reference to the
array. I don't know if there is a way around this. I doubt it. By the way, do
you mind if I ask why you want it on the stack? Is it just for performance
reasons?

I hope I've helped somehow.

Brian Delahunty
Ireland

http://briandela.com/blog


wASP said:
On Wed, 10 Aug 2005 14:38:05 -0700, Brian Delahunty

Hi Brian,

I have a problem with the ELSE case:

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
else
{
Label1.Text = " 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++)
Label1.Text = Label1.Text + int_array[nn].ToString() + "|";
}
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */



It threw the following exception:
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

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

Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: CS0019: Operator '+' cannot be applied to
operands of type 'object' and 'int'

Source Error:

Line 127: for (Int32 nn = 0; nn < 11; nn++)
Line 128: int_array[nn] = int_array[nn] + 1;
Line 129:
Line 130: for (Int32 nn = 0; nn < 11; nn++)

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */



Do I need to set some kind of a switch or something somewhere?

Also, what do you mean by "immediate array"?

One that allocates a block of memory from the program stack,
instead of dynamically from the heap.

IOW, C# creates arrays dynamically - much like doing the following in C++:

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


... instead of allocating from the program stack in C++ like this:

int int_array[11];

I don't see anything in any docs on C# that will let you do anything
other than to allocate array-space from the heap.


- wASP
 
W

wASP

On Wed, 10 Aug 2005 14:38:05 -0700, Brian Delahunty

Hi Brian,

I have a problem with the ELSE case:

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
else
{
Label1.Text = " 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++)
Label1.Text = Label1.Text + int_array[nn].ToString() + "|";
}
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */



It threw the following exception:
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

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

Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: CS0019: Operator '+' cannot be applied to
operands of type 'object' and 'int'

Source Error:

Line 127: for (Int32 nn = 0; nn < 11; nn++)
Line 128: int_array[nn] = int_array[nn] + 1;
Line 129:
Line 130: for (Int32 nn = 0; nn < 11; nn++)

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */



Do I need to set some kind of a switch or something somewhere?

Also, what do you mean by "immediate array"?

One that allocates a block of memory from the program stack,
instead of dynamically from the heap.

IOW, C# creates arrays dynamically - much like doing the following in C++:

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


... instead of allocating from the program stack in C++ like this:

int int_array[11];

I don't see anything in any docs on C# that will let you do anything
other than to allocate array-space from the heap.


- wASP
 
W

wASP

OK gang.

Someone on another forum told me that I cannot access the viewstate
from the Page_Init handler, and I've also been told that the object
must be "serialized" (whatever THAT means) before it can be stored
in the viewstate.

So, I've replaced the UInt32[] type with an ArrayList object,
and tried to store it in the Session state. I've gotten the
SAME compilation error:
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

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

Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: CS0019: Operator '+' cannot be applied to operands
of type 'object' and 'int'

Source Error:

Line 116: {
Line 117: for (Int32 nn = 0; nn < 11; nn++)
Line 118: int_array[nn] = int_array[nn] + 1;
Line 119:
Line 120: for (Int32 nn = 0; nn < 11; nn++)

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


My Page_Init function is now as follows:
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void Page_Init (object sender, EventArgs e)
{
if (!Page.IsPostBack)
{ string msg1;
ArrayList int_array = new ArrayList();
// UInt32[] int_array = new UInt32[11];

msg1 = " at first: |";

for (Int32 nn = 0; nn < 11; nn++)
// int_array[nn] = 100 - nn;
int_array.Add (100 - nn);

for (Int32 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;
Session["int_array"] = int_array;
}
else
{ string msg1 = " On postback: |";

ArrayList int_array = (ArrayList) Session["int_array"];
// ArrayList int_array = (ArrayList) ViewState["int_array"];


if (int_array == null)
msg1 = "int_array is null<br>";
else
{
for (Int32 nn = 0; nn < 11; nn++)
int_array[nn] = int_array[nn] + 1;

for (Int32 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>") );
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */



THIS is getting to be extremely exasperating!

Anyway, seems like you are having a weird problem.

.... and it gets stranger by the moment.

As regards your question
about arrays. Unfortunately .NET arrays, even array of value types, are
stored on the heap. The only thing stored on the stack is a reference to the
array. I don't know if there is a way around this. I doubt it. By the way, do
you mind if I ask why you want it on the stack? Is it just for performance
reasons?

Mostly for that, but I just think it's a bit strange that C# won't let you
allocate space like that. I thought it could do almost everything that C++
(and classic C) could do.

Oh well ...


I hope I've helped somehow.

If nothing else, you've given me a shoulder to cry on. <sob>

I appreciate your attention to this.


- wASP
 
W

wASP

OK gang - I'm an idiot.

I forgot about the CAST that Brian suggested:
int_array[nn] = ((UInt32) int_array[nn]) + 1;

After making that cast, IT WORKED!!!

... only with Session state - ViewState wouldn't work.

... so apparently I cannot access ViewState in Page_Init.

My gratitude to Brian Delahunty - he's my hero!


Take Care All,

-wASP
=============================



To be honest I can't see where there is a problem. The code looks fine....
the error is saying you are trying to add 1 to an object, which would be an
error, but I can't see where it is happening and the code I tested (the one I
posted in my inital response) works fine... I have it in a code behind
file... however, even if you have it inline it should still work.

If line 128 is giving you the error, i.e. "int_array[nn] = int_array[nn] +
1;" then the only thing I can think of is that int_array[nn] is actually
returning an object instead of a UInt32... I can't see why this would be the
case but it seems like it is... so, you could try this changing that line to:

int_array[nn] = ((UInt32) int_array[nn]) + 1;

and if that doesn't work you could try this

int_array[nn] = UInt32.Parse(int_array[nn].ToString()) + 1;


Anyway, seems like you are having a weird problem. As regards your question
about arrays. Unfortunately .NET arrays, even array of value types, are
stored on the heap. The only thing stored on the stack is a reference to the
array. I don't know if there is a way around this. I doubt it. By the way, do
you mind if I ask why you want it on the stack? Is it just for performance
reasons?

I hope I've helped somehow.

Brian Delahunty
Ireland

http://briandela.com/blog


wASP said:
On Wed, 10 Aug 2005 14:38:05 -0700, Brian Delahunty

Hi Brian,

I have a problem with the ELSE case:

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
else
{
Label1.Text = " 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++)
Label1.Text = Label1.Text + int_array[nn].ToString() + "|";
}
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */



It threw the following exception:
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

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

Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: CS0019: Operator '+' cannot be applied to
operands of type 'object' and 'int'

Source Error:

Line 127: for (Int32 nn = 0; nn < 11; nn++)
Line 128: int_array[nn] = int_array[nn] + 1;
Line 129:
Line 130: for (Int32 nn = 0; nn < 11; nn++)

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */



Do I need to set some kind of a switch or something somewhere?

Also, what do you mean by "immediate array"?

One that allocates a block of memory from the program stack,
instead of dynamically from the heap.

IOW, C# creates arrays dynamically - much like doing the following in C++:

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


... instead of allocating from the program stack in C++ like this:

int int_array[11];

I don't see anything in any docs on C# that will let you do anything
other than to allocate array-space from the heap.


- wASP

- wASP
 
G

Guest

Wohoo! I'm someone's hero!

No problem. Glad I could help.
--
Brian Delahunty
Ireland

http://briandela.com/blog


wASP said:
OK gang - I'm an idiot.

I forgot about the CAST that Brian suggested:
int_array[nn] = ((UInt32) int_array[nn]) + 1;

After making that cast, IT WORKED!!!

... only with Session state - ViewState wouldn't work.

... so apparently I cannot access ViewState in Page_Init.

My gratitude to Brian Delahunty - he's my hero!


Take Care All,

-wASP
=============================



To be honest I can't see where there is a problem. The code looks fine....
the error is saying you are trying to add 1 to an object, which would be an
error, but I can't see where it is happening and the code I tested (the one I
posted in my inital response) works fine... I have it in a code behind
file... however, even if you have it inline it should still work.

If line 128 is giving you the error, i.e. "int_array[nn] = int_array[nn] +
1;" then the only thing I can think of is that int_array[nn] is actually
returning an object instead of a UInt32... I can't see why this would be the
case but it seems like it is... so, you could try this changing that line to:

int_array[nn] = ((UInt32) int_array[nn]) + 1;

and if that doesn't work you could try this

int_array[nn] = UInt32.Parse(int_array[nn].ToString()) + 1;


Anyway, seems like you are having a weird problem. As regards your question
about arrays. Unfortunately .NET arrays, even array of value types, are
stored on the heap. The only thing stored on the stack is a reference to the
array. I don't know if there is a way around this. I doubt it. By the way, do
you mind if I ask why you want it on the stack? Is it just for performance
reasons?

I hope I've helped somehow.

Brian Delahunty
Ireland

http://briandela.com/blog


wASP said:
On Wed, 10 Aug 2005 14:38:05 -0700, Brian Delahunty

Hi Brian,

I have a problem with the ELSE case:

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
else
{
Label1.Text = " 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++)
Label1.Text = Label1.Text + int_array[nn].ToString() + "|";
}
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */



It threw the following exception:
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

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

Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: CS0019: Operator '+' cannot be applied to
operands of type 'object' and 'int'

Source Error:

Line 127: for (Int32 nn = 0; nn < 11; nn++)
Line 128: int_array[nn] = int_array[nn] + 1;
Line 129:
Line 130: for (Int32 nn = 0; nn < 11; nn++)

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */



Do I need to set some kind of a switch or something somewhere?


Also, what do you mean by "immediate array"?

One that allocates a block of memory from the program stack,
instead of dynamically from the heap.

IOW, C# creates arrays dynamically - much like doing the following in C++:

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


... instead of allocating from the program stack in C++ like this:

int int_array[11];

I don't see anything in any docs on C# that will let you do anything
other than to allocate array-space from the heap.


- wASP

- wASP
 
G

Guest

Wohoo! I'm someone's hero!

No problem. Glad I could help.
--
Brian Delahunty
Ireland

http://briandela.com/blog


wASP said:
OK gang - I'm an idiot.

I forgot about the CAST that Brian suggested:
int_array[nn] = ((UInt32) int_array[nn]) + 1;

After making that cast, IT WORKED!!!

... only with Session state - ViewState wouldn't work.

... so apparently I cannot access ViewState in Page_Init.

My gratitude to Brian Delahunty - he's my hero!


Take Care All,

-wASP
=============================



To be honest I can't see where there is a problem. The code looks fine....
the error is saying you are trying to add 1 to an object, which would be an
error, but I can't see where it is happening and the code I tested (the one I
posted in my inital response) works fine... I have it in a code behind
file... however, even if you have it inline it should still work.

If line 128 is giving you the error, i.e. "int_array[nn] = int_array[nn] +
1;" then the only thing I can think of is that int_array[nn] is actually
returning an object instead of a UInt32... I can't see why this would be the
case but it seems like it is... so, you could try this changing that line to:

int_array[nn] = ((UInt32) int_array[nn]) + 1;

and if that doesn't work you could try this

int_array[nn] = UInt32.Parse(int_array[nn].ToString()) + 1;


Anyway, seems like you are having a weird problem. As regards your question
about arrays. Unfortunately .NET arrays, even array of value types, are
stored on the heap. The only thing stored on the stack is a reference to the
array. I don't know if there is a way around this. I doubt it. By the way, do
you mind if I ask why you want it on the stack? Is it just for performance
reasons?

I hope I've helped somehow.

Brian Delahunty
Ireland

http://briandela.com/blog


wASP said:
On Wed, 10 Aug 2005 14:38:05 -0700, Brian Delahunty

Hi Brian,

I have a problem with the ELSE case:

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
else
{
Label1.Text = " 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++)
Label1.Text = Label1.Text + int_array[nn].ToString() + "|";
}
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */



It threw the following exception:
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

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

Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: CS0019: Operator '+' cannot be applied to
operands of type 'object' and 'int'

Source Error:

Line 127: for (Int32 nn = 0; nn < 11; nn++)
Line 128: int_array[nn] = int_array[nn] + 1;
Line 129:
Line 130: for (Int32 nn = 0; nn < 11; nn++)

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */



Do I need to set some kind of a switch or something somewhere?


Also, what do you mean by "immediate array"?

One that allocates a block of memory from the program stack,
instead of dynamically from the heap.

IOW, C# creates arrays dynamically - much like doing the following in C++:

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


... instead of allocating from the program stack in C++ like this:

int int_array[11];

I don't see anything in any docs on C# that will let you do anything
other than to allocate array-space from the heap.


- wASP

- wASP
 

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