- Home /
how add values to Generic.list ?
Hi...
how can I add these values to the generic.list array
Name = Jone
Age = 29
Photo = image for him
Family
Name = Nadia
Age = 49
Photo = image for her
Name = Saied
Age = 58
Photo = image for him
one person and two family members
public List<PersonsData> Persons;
[System.Serializable]
public struct PersonsData
{
public string Name;
public int Age;
public Texture2D Photo;
public List<FamilyData> Family;
}
[System.Serializable]
public struct FamilyData
{
public string Name;
public int Age;
public Texture2D Photo;
}
thank you so much
Answer by starikcetin · Jun 29, 2015 at 03:33 AM
First of all, you need to declare lists like this:
public List<PersonsData> Persons = new List<PersonsData>();
Then you can add values like this:
PersonsData pd = new PersonsData();
Persons.Add(pd);
Good luck!
Edit: I personally not prefer giving direct code answers but, here take a look at this:
[System.Serializable]
public struct PersonsData
{
public string Name;
public int Age;
public Texture2D Photo;
public List<FamilyData> Family;
}
[System.Serializable]
public struct FamilyData
{
public string Name;
public int Age;
public Texture2D Photo;
}
public class example
{
public List<PersonsData> Persons = new List<PersonsData>();
public void _example ()
{
PersonsData jone = new PersonsData();
jone.Name = "Jone";
jone.Age = 29;
jone.Family = new List<FamilyData>();
FamilyData nadia = new FamilyData();
nadia.Name = "Nadia";
nadia.Age = 49;
jone.Family.Add(nadia);
FamilyData saied = new FamilyData();
saied.Name = "Saied";
saied.Age = 58;
jone.Family.Add(saied);
Persons.add(jone);
}
}
Thank you for answering me ...
I write this code :
PseronsData pd = new PseronsData();
FamilyData fd = new FamilyData();
pd.Name = "Jone";
fd.Name = "Saied";
pd.Family.Add (fd);
Persons.Add(pd);
but I got this error message on pd.Family.Add (fd);
NullReferenceException: Object reference not set to an instance of an object
how can i add family members ?
You have to first create the list itself, before you can add items. (Or create the List with the items)
pd.Family = new List<FamilyData>();
pd.Family.Add(fd);
or
pd.Family = new List<FamilyData> { fd };
I edited the answer with a not-tested code. Take a look at it.
thank you , it's just an example to know how to add values to list , but this not my project .... it's just an example
now I can add it to my project , :)
thank you again ....
Answer by malkere · Jun 29, 2015 at 03:32 AM
you can just do Persons.Add(new PersonsData) to create a new entry in the list and then Persons[0].Name = whatever, etc. but Lists change their index automatically when stuff is input and removed. You might want to try a Dictionary instead so you can store the Value (PersonsData) with a Key (Name) Then you would call the PersonsData with Persons[nameYoureLookingFor].
alternatively if you don't have a lot of entries you can just
for (int i = 0; i < Persons.count; i++) {
if (Persons[i].name == nameYoureLookingFor) {
return Persons[i].Age
}
}
that will loop through all the entries of the list looking for what you're trying to find.
Adding is easy, but make sure you know if you should be using a List, an Array, or a Dictionary before you start. They all have their ups and downs.
Using a foreach statement for code you wrote would make things easier:
foreach(var item in Persons)
{
if(item.Name == nameWeLookFor) return item.Age;
}
thank you for answering me ...
I need to add person and family data to the list array
Piflik has the answer above. A null reference means you are trying to access something that doesn't exist yet. All lists and arrays and dictionaries and what not need to be initialized before they can be used. examples of initilization:
//define the list:
List<string> strings;
//then initialize it:
strings = new List<string>(); //<-- need the ()
//or do it all at once:
List<string> strings2 = new List<string>();
//define the dictionary:
Dictionary<string, int> ints;
//then initialize it:
ints = new Dictionary<string, int>(); //<-- need the ()
//or do it all at once:
Dictionary<string, int> ints2 = new Dictionary<string, int>();
//define the array:
string[] stringArray;
//then initialize it:
stringArray = new string[10]; //<-- no () needed, but must define its dimensions, in this case 10 strings, 0-9
//or do it all at once:
string[] stringArray = new string[10];
can then add freely to any of them now that they are initizlized:
strings.Add("newStrings");
ints.Add("newString", 42);
stringArray[0] = "newString";
Your answer
Follow this Question
Related Questions
How can I check if ALL items in an array/list meet a condition? 2 Answers
A node in a childnode? 1 Answer
Emptying a Generic List / Unexpected Behaviour 1 Answer
Generic List.Count always gives 0 2 Answers
Count selected item in List 1 Answer