I'm coming from a Javascript background which possibly is a good thing although so far it's proving to be a bad thing.
In Javascript, I have:
function doStuff(){
var temp = new Array();
temp.push(new marker("bday party"));
alert(temp[0].whatsHappening); // 'bday party' would be alerted
}
function marker(whatsHappening) {
this.whatsHappening = whatsHappening;
}
I would now like to do the same thing in C#. I have set up a class:
class marker
{
public string whatsHappening;
public marker(string w) {
whatsHappening = w;
}
}
and add a new object, but I can't seem to call the data in the same way:
ArrayList markers = new ArrayList();
markers.Add(new marker("asd"));
string temp = markers[0].whatsHappening; // This last bit isn't allowed
Use a generic List<T>
instead:
List<marker> markers = new List<marker>();
markers.Add(new marker("asd"));
string temp = markers[0].whatsHappening;
ArrayList is weakly typed. That means, all items in the array are objects. You need to cast them to your class:
string temp = ((marker)markers[0]).whatsHappening;
You should use a List<marker>
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With