Relevant code:
Public enum ObjectType {
Drug,//Drug
Equip,//
Mat//material
}
//an item object class (this item in all the attributes)
Public class ObjectInfo {
Public int id;
Public string name;
Public string icon_name;//atlas of the name in the
Public ObjectType type;
Public int HP;
Public int mp;
Public int price_sell;
Public int price_buy;
}
//the contents of the text read to the program of
Public void ReadText () {
//remove all characters in the text file
A string of text=objectsInfoListText. Text;
String [] strArray=text. The Split (' \ n ');//remove the line
ObjectInfo info=new ObjectInfo ();
Foreach (string STR in strArray) {//parsing out a single line of information
String [] proArray=STR. The Split (', ');
//remove the attribute to the
Int id=int. Parse (proArray [0]).
String name=proArray [1].
String icon_name=proArray [2];
String str_type=proArray [3].
//on classifying types determine
The ObjectType type=ObjectType. Drug;
The switch (str_type) {
Case "Drug" :
Type=ObjectType. Drug;
break;
Case "Equip" :
Type=ObjectType. Equip;
break;
Case "Mat" :
Type=ObjectType. Mat;
break;
}
Info. Id=id;
Info. Name=name;
Info. Icon_name=icon_name;
Print (" now want to add the objectInfo icon_name is "+ info. Id + info. Icon_name);
Info. Type=type;
If (type==ObjectType. Drug) {
Int HP=int. Parse (proArray [4]);
Int mp=int. Parse (proArray [5]);
Int price_sell=int. Parse (proArray [6]);
Int price_buy=int. Parse (proArray [7]);
Info. HP=HP;
Info. Mp=mp;
Info. Price_sell=price_sell;
Info. Price_buy=price_buy;
}
ObjectInfoDict. Add (id, info);//have to extract the information from the dictionary, convenient call
//test code
Foreach (KeyValuePair & lt; Int, ObjectInfo> Temp objectInfoDict) in {
Print (" add traversal of the dictionary in the process of "+" Key is "+ temp. Key +" Value part "+ temp. Value. The id + temp. Value. Icon_name);
}
}
}
Test information:
CodePudding user response:
It didn't quite understand what you mean, does that mean to read out the item id is the same, only change the data, in this case, you'd better test this id is not exist in the dictionary, if there is increase in quantity, or increase the idAssuming that you have to prop for id, num
if (! Dic. Either containsKey (id))
Dic. The add (id, 0);
Dic [id] +=num
CodePudding user response:
Different id down different pictures, the error is: the values from the dictionary, the keys are different, but have the same value,Found error source is: when data is added to the dictionary, each adding a new will cover the existing value (the id),,,,,,,
CodePudding user response:
ObjectInfo info=new ObjectInfo ();This sentence outside the loop for instantiation. Should be declared outside the loop variable, instantiated in the loop.
Disease not instantiated in the loop. Every time is to modify the same object,
Such a change.
ObjectInfo info=null;
Foreach (string STR in strArray) {//parsing out a single line of information
Info=new ObjectInfo ();
String [] proArray=STR. The Split (', ');
.
CodePudding user response:
The code of light, you couldn't see what circumstanceCodePudding user response:
You are outside the traversal instantiation ObjectsInfo, so from the beginning to the end you just instantiate a ObjectsInfo, dictionary is like a fridge, you instantiate ObjectsInfo is a cup, the inside of the ObjectsInfo field is something that you put their cups, you continue to replace something in the cup, the last in the fridge or only a glassCodePudding user response:
ObjectInfo info=new ObjectInfo (); Are you sure place to place, you cycle the meaningful? Is an object, value must be the sameCodePudding user response:
It has already been said on theThe final result is a good object Id corresponding to the last one