pass multiple lists to javascript using invokescript using c# - c#

I use a webbrowser control to call a javascript function in a html file using InvokeScript(). I would like to pass four lists as parameters so I can use the data in the javascript function.
pseudocode:
List<string> list1 = new List<string>();
list1.Add("foo");
list1.Add("bar");
List<string> list2 = new List<string>();
list2.Add("foo");
list2.Add("bar");
List<string> list3 = new List<string>();
list3.Add("foo");
list3.Add("bar");
List<string> list4 = new List<string>();
list4.Add("foo");
list4.Add("bar");
maps_webbrowser.Document.InvokeScript("initialize", list1.ToArray(), list2.ToArray() ,list3.ToArray() ,list4.ToArray());
I have read a post where a list is passed using a argument variable
How should an array be passed to a Javascript function from C#?
Here's an example JavaScript function:
function foo()
{
var stringArgs = [];
for (var i = 0; i < arguments.length; i++)
stringArgs.push(arguments[i]);
// do stuff with stringArgs
}
And you'd call it from C# like this:
List<string> arguments = new List<string>();
arguments.Add("foo");
arguments.Add("bar");
webBrowser.InvokeScript("foo", arguments.ToArray());
However, in this way only one list is passed.
The pseudocode I've wrote down does not work....

After a good night of sleep I've basically got it working :)
According to the msdn an object array has to be passed as parameter:
http://msdn.microsoft.com/en-us/library/cc452443(v=vs.110).aspx
C# code:
List<string> lat_waypoints = new List<string>();
lat_waypoints.Add("1.11111");
lat_waypoints.Add("2.12112");
List<string> lon_waypoints = new List<string>();
lon_waypoints.Add("34.1234");
lon_waypoints.Add("34.2345");
string lat_string = string.Join(",", lat_waypoints.ToArray());
string lon_string = string.Join(",", lon_waypoints.ToArray());
Object[] objArray = new Object[2];
objArray[0] = (Object)lat_string;
objArray[1] = (Object)lon_string;
maps_webbrowser.Document.InvokeScript("test", objArray);
Javascript:
<HTML>
<SCRIPT>
function test(lat, lon) {
var lat_split = lat.split(",");
var lon_split = lon.split(",");
alert("Lat: " +lat_split[0] + " lon: " + lon_split[0]);
}
</SCRIPT>
<BODY>
</BODY>
</HTML>
This solution works but it is not the nicest solution in my opinion......
The lat and lon values are originally stored in an list with doubles. However, I don't know how to pass an array of doubles directly without converting it to string first.
Anyone else with some ideas?

Related

How to use items of an array as names for other variables c#

I have a list of names in an array and i would like to use these names to assign them to new lists like bellow:
var list = new string[]{"bot1","bot2","bot3"};
List<string> list[0] = new List<string>();
but i am getting the error: a local variable or function named 'list' is already defined in this scope.
is there a work around !!?
your input will be greatly appreciated.
I think you can store your bots in dictionary:
var bots = new Dictionary<string,List<string>>();
bots[name] = new List<string>();
bots[name].Add("some str");
If you only need a integer as key than you can also use this solution.
List<List<string>> list = new List<List<string>>();
list.Add(new List<string>{ {"A"} });
list[0][0] = "..";

C# list of array type

I want to create a list of array type.
I want to create array containing values :
array = [a,b];
Then i want to put this array in list :
List<Array> list = new List<Array>( );
I am able to do this with list of string type but no luck with array type :
List<String> list = new List<String>( );
I am from javascript background, not much familiar with concept of collections in c#.
Also how can i create array in c# like we do in javascript :
var arrTemp = ["a", "b"];
Well, since your array is string[]:
var arrTemp = ["a", "b"];
you have to declare the required list as List<string[]>:
// list of string arrays
List<string[]> list = new List<string[]>() {
new string[] {"a", "b"}
};
In case you want to be able to put any array into the list declare it as loose as possible (List<object[]>):
// list of abitrary arrays
List<object[]> list = new List<object[]>() {
new string[] {"a", "b"},
new double[] {123.45, 789.12, 333.55},
new object[] {'a', "bcd", 1, 244.95, true},
};
Hope this can help you
var test = new List<int[]>();
You can actually create a list of arrays:
var listOfArrays = new List<Array>();
The problem with this is that it's difficult to use the arrays themselves, as the Array type doesn't support array syntax. (e.g. You can't do listOfArrays[0][0]) Instead, you have to use the GetValue method to do your retrieval:
var obj = listOfArrays[0].GetValue(0);
But this has another problem. The GetValue method returns object, so while you could always cast it to the desired type, you lose your type safety in choosing this approach.
Alternatively, you could just store object[] arrays:
var listOfArrays = new List<object[]>();
var obj = listOfArrays[0][0];
But while this solves the issue of the array notation, you still lose the type safety.
Instead, if at all possible, I would recommend finding a particular type, then just have arrays of that type:
var listOfArrays = new List<string[]>();
string s = listOfArrays[0][0];
for example, an array of strings would be
var arrayOfString = new string[]{"a","b"};
// or shorter form: string[] arrayOfString = {"a","b"};
// also: var arrayOfString = new[] { "a", "b" }
And then creating a list-of-arrayOfString would be
var listOfArrayOfString = new List<string[]>();
This works with any type, for example if you had a class MyClass
var arrayOfMyClass = new MyClass[]{ ... }; // ... is you creating instances of MyClass
var list = new List<MyClass[]>();

Add two array list values to arraylist c#

I'm completely new to c# sorry if asked here anything meaningless for you guys but I would like to know how can I solve this type of situation.
I having two arraylist's as shown below:
ArrayList OldLinks = new ArrayList();
ArrayList NewLinks = new ArrayList();
ArrayList mylist = new ArrayList();
foreach (string oldlink in OldLinkArray)
{
OldLinks.Add(oldlink);
}
foreach (string newlink in NewLinkArray)
{
NewLinks.Add(newlink);
}
Now I need to get them as single arraylist with two items each
I need to get it as
ArrayList NewList = new ArrayList();
NewList.Add(oldlink, newLink);
ArrayList NewList = new ArrayList();
NewList.AddRange(OldLinks);
NewList.AddRange(NewLinks);
You can use AddRange() method or AddAll() method to accomlish this.
NewList.AddAll(OldLinks);
NewList.AddAll(NewLinks);
Or
To create multidimensional arrayList you can use dictionary
public class MultiDimList: Dictionary<string, string> { }
MultiDimList NewList = new MultiDimList ();
for(int i; i<OldLinks.Count ; i++)
{
NewList.Add(OldLinks[i].ToString(), NewLinks[i].ToString());
}
provided both ArrayLists have the same count
Xou could do something like this.. The string Version is problaby not the best solution but can work. Sorry Code is note tested
public class Link
{
public string Version {get;set;}
public string Value {get;set;}
}
Use it Like
List<Link> linkList = new List<Link>();
linkList.AddRange(OldValues)
linkList.AddRange(OldValues)
var oldList = linkList.Where(l => l.Version.Equals("old")).ToList();
var newList = linkList.Where(l => l.Version.Equals("new")).ToList()
As you need both oldlink and newlink together as an item in resulted arraylist, you could use Zip Linq extension and do this.
ArrayList NewList = new ArrayList();
NewList.AddRange(OldLinks.Cast<string>()
.Zip(NewLink.Cast<string>(), (x,y) => string.Format("{0},{1}",x,y))
.ToArray()
);
Result ArrayList contains both (oldlink, newlink).

How to add data from a list to a new[]

I have a list o that has strings:
"Hist 2368#19:00:00#20:30:00#Large Conference Room",
"Hist 2368#09:00:00#10:30:00#Large Conference Room",
I want to add those strings to this:
var lines = new[]
{
"Meeting#19:00:00#20:30:00#Conference",
};
How would I use the data from the list o and insert it into lines?
Array are be nature, fixed-length. You need to create a new array, and assign it to lines.
lines = lines.Concat(o).ToArray();
Alternately,
lines = o.AddRange(lines).ToArray();
UPDATE: Fixed dumb mistake.
Since Lines is already an array, you'll need to merge the values into your list first:
foreach (var item in lines)
o.Add(item);
Then change o to an Array:
o.ToArray(); ///returns String[] with all three values.
You can also use .concat() as others have pointed out, which will internally do the same.
An additional thing to consider is the lines variable has an Array(T) type which is a fixed size, so you must either allocate enough space to hold all the data or copy the data into a new construct.
If you allocated enough space for lines to hold all the data then it looks something like this:
var o = new List<string>
{
"Hist 2368#19:00:00#20:30:00#Large Conference Room",
"Hist 2368#09:00:00#10:30:00#Large Conference Room",
};
var lines = new string[3] { "Meeting#19:00:00#20:30:00#Conference", null, null };
// Copy the data from o to the end of lines
o.CopyTo(lines, 1); // Start a 1 to not overwrite the existing data
See also:
CopyTo(T[] array)
Otherwise if you have two different data sources that you want to pool into a new construct then I recommend using the Concat method. This will combine the IEnumerable(T) types which you can use ToArray or ToList to give the data the right container.
var o = new List<string>
{
"Hist 2368#19:00:00#20:30:00#Large Conference Room",
"Hist 2368#09:00:00#10:30:00#Large Conference Room",
};
var lines = new[]
{
"Meeting#19:00:00#20:30:00#Conference",
}.Concat(o).ToArray();
Be sure you know which container you want to use.
You can concatenate the values using Enumerable.Concat :
lines = O.Concat(lines).ToArray();
Simply this
lines = lines.Concat(o).ToArray();
Try this:
List<string> myList = new List<string>()
{
"My First String in List",
"My Second String in List"
};
string[] myArray = new string[] { "My Array First String" };
List<string> myArrayList = new List<string>();
foreach (var item in myArray)
{
myArrayList.Add(item);
}
foreach (var item in myList)
{
myArrayList.Add(item);
}
foreach (var item in myArrayList)
{
Console.WriteLine(item);
}
Console.Read();

how to access arraylist, string array, or list from javascript?

I want to call one javascript function from my button click on server side and pass the arraylist, string array or list as a parameter to the function.
I would then need to iterate through all the elements of these collection and perform some action on the data.
Can you tell me the javascript code to access these element
Consider the following structure that I have in the Javascript
Function ABC(_Arraylist/List/String Array){
//I would like to perform some action on the collections data here.
}
Thanks.
Server:
// this could also be a string[] if you prefer
var array = new List<string> { "foo", "bar", "baz" };
var serializer = new JavaScriptSerializer();
var script = string.Format("foo({0});", serializer.Serialize(array));
ClientScript.RegisterStartupScript(GetType(), "call", script, true);
Client:
function foo(array) {
for (var i = 0; i < array.length; i++) {
alert(array[i]);
}
}

Categories