Reputation:
I think i have some problem try to explain what i mean so i gonna explain what i do and what my code turn to from begining.
I have this code that save my data to json, it work fine, it print json perfectly
Script: GiftValue
[SerializeField]
public class Gift
{
public int GValue
public string GName
public int GAmount
public float GWeight
}
Script: EditAddGift
[SerializeField]
public List<Gifts> _Gift = new List<Gifts>();
///those code above is what i missing before.
Gifts NewGift = new Gifts
{
GValue = DropDownValueGift,
GName = ifGiftDetailName.text,,
GAmount = iAmount,
GWeight = fWeight,
};
_Gift.Add(NewGift);
SaveToJson(_Gift, DATABASE_NAME);
This is my code before I realise that my class need to split to GiftDetail and Gift. After that I realise that I need to split "Gifts" to 2 part, "Gifts" and "GiftDetail". Soo i google and found about nested class, after some time my Script GiftValue become this
[System.Serializable]
public class GiftDetail
{
public int GDetailValue
public string GDetailName
public double GDetailWeight
public int GDetailAmount
}
[System.Serializable]
public class Gift
{
public int GValue
public string GName
public List<GiftDetail> GiftDetail
}
I have trouble write code after change because i ONLY KNOW HOW TO call normal class like this :
Gifts NewGift= New Gifts
{ ..... }
This is what i try to do
GiftDetail NewGiftDetail = new GiftDetail
{
GDetailValue = DropDownValueGiftDetail,
GDetailAmount = iAmount,
GDetailName = ifGiftDetailName.text,
GDetailWeight = Fweight,
};
Gifts NewGift = new Gifts
{
GValue = DropDownValueGift,
GName = txtDropDownGift,
//What i want to know is how to change NewGiftDetail
//or change class Gift or class GiftDetail or....
// so i can put GiftDetail in here
//GiftDetail = NewGiftDetail,// let just delete this,
// because i don't know how to call this
};
So that finally i will have my json file look like this
[
{
"GValue": 1,
"GName": "Gift 3",
"GiftDetail":
[
{
"GDetailValue": 3,
"GDetailName": "2",
"GDetailWeight": 3.0,
"GDetailAmount": 4
},
{
"GDetailValue": 3,
"GDetailName": "2",
"GDetailWeight": 3.0,
"GDetailAmount": 4
}
],
},
]
I try someway before like change Gift to public class Gift<T>
blah blah.. but i think i should show my code from before it downfall to help you guy more clear on what i want to achive
Code i use to delete before i change to Gift and GiftDetail
if (_Gift.Exists(x => x.GValue== DropDownValuePhanQua ))
{
int i = 0;
while (i <_Gift.Count && _Gift[i].GValue != DropDownValuePhanQua)
i++;
_Gift.RemoveAt(i);
}
_Gift.Add(NewGift);
SaveToJson(_Gift, DATABASE_NAME);
I change it to this
if (_Gift.Exists(x => x.GValue== DropDownValuePhanQua ))
///still don't know how to get GDetailValue , it only let me access to x.GiftDetail
{
GiftDetail NewGiftDetail3 = new GiftDetail
{
GDetailValue = DropDownValuePhanQua,
GDetailAmount = iSoLuongPhanQua,
GDetailName = ifTenPhanQua.text,
GDetailWeight = fTileRaPhanQua,
};
int i = 0;
while (i <_Gift.Count && _Gift[i].GValue != DropDownValuePhanQua)
i++;
NewGift.GiftDetails.Add(NewGiftDetail3);
_Gift.RemoveAt(i);
}
_Gift.Add(NewGift);
SaveToJson(_Gift, DATABASE_NAME);
If i use code about it will return json but it keep replace the second GiftDetail
[ {
"GValue": 1,
"GName": "Phần Quà 1",
"GiftDetails": [
{
"GDetailValue": 5,
"GDetailName": "3",
"GDetailWeight": 2.0,
"GDetailAmount": 4
},
{
"GDetailValue": 6,
"GDetailName": "Old One",
"GDetailWeight": 33.0,
"GDetailAmount": 3
}
]
}
]
to this
[
{
"GValue": 1,
"GName": "Phần Quà 1",
"GiftDetails": [
{
"GDetailValue": 5,
"GDetailName": "3",
"GDetailWeight": 2.0,
"GDetailAmount": 4
},
{
"GDetailValue": 3,
"GDetailName": "new Gift",
"GDetailWeight": 33.0,
"GDetailAmount": 3
}
]
}
]
Upvotes: 0
Views: 150
Reputation: 35105
There are few ways. Here is one.
Having
[System.Serializable]
public class GiftDetail
{
public int GDetailValue;
public string GDetailName;
public double GDetailWeight;
public int GDetailAmount;
}
[System.Serializable]
public class Gift
{
public int GValue;
public string GName;
public List<GiftDetail> GiftDetails; // Added 's'
}
you can
GiftDetail NewGiftDetail = new GiftDetail
{
GDetailValue = DropDownValueGiftDetail,
GDetailAmount = iAmount,
GDetailName = ifGiftDetailName.text,
GDetailWeight = Fweight,
};
GiftDetail NewGiftDetail2 = new GiftDetail
{
//(...)
};
Gift NewGift = new Gift //Gift - not gifts
{
GValue = 1,
GName = "One",
GiftDetails = new List<GiftDetail>() { NewGiftDetail, NewGiftDetail2 }
};
GiftDetail d3 = new GiftDetail
{
//(...)
};
NewGift.GiftDetails.Add(d3);
// NewGift has now 3 details
//Access first detail
NewGift.GiftDetails[0]; // Or .First() with 'using System.Linq`
Upvotes: 2
Reputation: 161
List<GiftDetail> GiftDetailList =new List<GiftDetail>()
var GD1 = new GiftDetailList ()
{
GDetailValue= 3,
GDetailName= "2",
GDetailWeight= 3.0,
GDetailAmount= 4
}
similarly create as many Details you want and then
Gifts NewGift = new Gifts
{
GValue = DropDownValueGift,
GName = txtDropDownGift,
GDteails=GiftDetailList
};
Upvotes: 0