- Home /
c# Unexpected symbol '=' in struct, class.....
I must be doing something wrong. I'm getting the error in the title, but I have no idea why. The error is on line 8 and 9 Here is my code:
public class ItemDatabase {
private List<Item> items = new List<Item>();
private Dictionary<string, Item> itemPresets = new Dictionary<string, Item>();
Item preset;
preset = new Item();
preset.Name = "Rusty Sword";
public void SetPreset(string name, Item preset) {
itemPresets[name] = preset;
}
public Item AddNew(string name) {
Item preset = itemPresets[name];
items.Add(preset);
return preset;
}
}
Item is a class, with the property Name.
Also, the autocopmlete or whatever isn't showing my variables, in case it has anything to do with this. What am I doing wrong??
Thanks
Answer by Coderdood · Jun 25, 2013 at 07:51 PM
The following needs to be in a method.
preset = new Item();
preset.Name = "Rusty Sword";
If you need to have this be predefined you need to modify Item to have a constructor that accepts a name and do the following:
Item preset = new Item("Rusty Sword");
Would you $$anonymous$$d explaining on why it needs to be in a method please?
Because that's just the way c# works. $$anonymous$$aybe this stackoverflow post would help?
Ahhh never$$anonymous$$d, after experimenting a bit more I understand now. Thanks for you answer though, It's all working ok now!