ArrayList in CSharp

G

Greg

I''ve been working with a CSharp web-site (and am new to it). I have a
javascript function that accepts variables from my CSharp code. It all works
fine, with the exception of one variable type. I created an ArrayList
variable in my CSharp program and it collects the information i need with no
problem. I pass it to my javascript with no problem. Where I'm having a
problem is retreiving the ArrayList values once it's been passed to the
javascript. Since I am not familiar with CSharp and Javascript I am not sure
where to turn for help.

It appears to me that the ArrayLlist was successfully passed to my
javascript function without error, since the remainder of my javascript
works fine. But, when I try to retrieve anything from my array list I have no
luck.

So, If I had the function below, that was accepting an array list as such,
and the array list contains 4 items, how could I display the results in a
window. (My actually purpose is not to display in a window, but I just want
to get somethign working right now).

function CalcTotal(myArrayList)
(
for (i = 1; i < 5; i++)
{
window.alert('myArrayList Value: ' + i + ' - ' + myArrayList(i) );
}
}

My ulimate goal is to scan through the results of the ArrayList to perform
some calculations on a web-page. I've got this part working, but I need to
pass in my answers via an ArrayList.

Any help would be greatly appreciated.
 
M

Marc Gravell

I'm actually surprised it lets you carry an ArrayList into javascript!
I've never tried... I generally consider an ArrayList (or rather, a
List<T>) to be a server-side object, not a client-side one.

However, doesn't javascript use square brackets for indexers? Try
myArrayList, like you would in C#

Marc
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,

I created an ArrayList
variable in my CSharp program and it collects the information i need with no
problem. I pass it to my javascript with no problem.

How????
I think you are making a mistake here, that is no possible, AFAIK.
Can you post your page? I'm interested in seeing how you do it.

Besides in your code is myArrayList a function or an array?
I ask because you are using () as in a function
 
G

Göran Andersson

Greg said:
I''ve been working with a CSharp web-site (and am new to it). I have a
javascript function that accepts variables from my CSharp code. It all works
fine, with the exception of one variable type. I created an ArrayList
variable in my CSharp program and it collects the information i need with no
problem. I pass it to my javascript with no problem. Where I'm having a
problem is retreiving the ArrayList values once it's been passed to the
javascript. Since I am not familiar with CSharp and Javascript I am not sure
where to turn for help.

It appears to me that the ArrayLlist was successfully passed to my
javascript function without error, since the remainder of my javascript
works fine. But, when I try to retrieve anything from my array list I have no
luck.

So, If I had the function below, that was accepting an array list as such,
and the array list contains 4 items, how could I display the results in a
window. (My actually purpose is not to display in a window, but I just want
to get somethign working right now).

function CalcTotal(myArrayList)
(
for (i = 1; i < 5; i++)
{
window.alert('myArrayList Value: ' + i + ' - ' + myArrayList(i) );
}
}

My ulimate goal is to scan through the results of the ArrayList to perform
some calculations on a web-page. I've got this part working, but I need to
pass in my answers via an ArrayList.

Any help would be greatly appreciated.

You can't call a Javascript function directly from C#, as they don't
exist in the same place, and they don't exist at the same time.

To call a Javascript function from C# you would create Javascript code
that makes the call and put it in the page. As the page is sent as text
to the browser, you can only use data that can be represented as text
when you create the Javascript code that makes the call.

There is no ArrayList object in Javascript, so what you would use on the
client side is an array. An arrayList is a list of objects, and that can
not be represented as text as is, you would have to represent each item
in the list as text.

Unless you are stuck with framework 1.x, you shouldn't use ArrayList at
all. You should use a type safe list like List<string>, or if you
absolutely need to put object references in the list (which is rare as
you usually have some more specific type that the data has in common),
use List<object>.
 

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