- Home /
How can I create an array of objects in C#?
Hi there. I am moving over from javascript to C# and am having difficulty understanding some of the same concepts I used in javascript. for example, In javascript I have created this code;
var array = new Array ();
array[0] = { "x": 1,
"y": 10,
"dialogue": "stuff",
"show": true
}
array[2] = { "x": 1,
"y": 10,
"dialogue": "stuff",
"show": false
}
array[10] = {"x": 1,
"y": 10,
"dialogue": "more stuff",
"show": true
}
as you can see, I have put an object into the indexes of 0, 2, and 10. from what I understand, you can only have variables of one type in an array in C#. could anyone show me how I could create this code in C#?
on a side note, in this code in javascipt I would iterate through the array searching for indexes which contained information by using the conditional
if (array[i] != undefined)
could this work in C#?
What was going into that array in JS?
It seems from your code you were using the equivalent of an Arraylist.
But here is an example of creating an array of integers in C#:
public int[] intarray = new intarray[*size goes here*];
Here is a good link for understanding arrays in C#: C# Arrays
Anyways, good luck
$$anonymous$$y experience with objects has mainly been with json calls in sites that use ajax calls. In this case, I would be able to access the x, y, dialogue and show properties of entries in an array. I'm using them to contain the properties of 10 separate labels in a scene, who's contents could be different at different times.
For example, I could store a piece of dialogue in two different labels that render simultaneously, and still be able to change their properties individually later on. When OnGUI runs, it loops through each entry in the array and sets it's size to x and y (though I have more properties in my script) and the contents of the box to dialogue.
The problem is, it needs to be an object, and from what I've seen C# only supports integers, floats and strings
Answer by paulaceccon · Feb 21, 2013 at 06:09 PM
It seems that you could create a class like:
public class Item
{
int x;
int y;
string dialogue;
bool show;
public Robot( int _x, int _y, string _dialogue, bool _show )
{
x = _x;
y = _y;
dialogue = _dialogue;
show = _show;
}
}
Then, you could create getters to access these variables.
So, you could create a list like this:
public class myClass: MonoBehaviour
{
public List<Item> items;
void Start (){
items = new List</item>();
items.Add (new Item (1, 10, "stuff", true));
items.Add (new Item (1, 10, "stuff", false));
}
}
it says that robot needs a return type, but I'm not sure what it is
This was usefull? If so, could you accept it or voted it up? Thks.
It seems "Robot" should be the constructor of the class, but you called your class "Item". So either change the class name to "Robot" or the constructor should be called "Item"
it should be like that
public Item ( int _x, int _y, string _dialogue, bool _show )
{
x = _x;
y = _y;
dialogue = _dialogue;
show = _show;
}
public Item (type parameterName){
//your code goes here
}
is by default return it's self as constructor overload
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Array empties values on RunTime? 1 Answer
Array with two values per element 1 Answer
Algorithm for loading sounds 1 Answer