Setting the value of a struct variable in C#

I know this doesnt directly deal with unity but im not sure where else i would ask it so ill give it try here. Im creating a game in which there are multiple types of weapons. Each weapon is defined at the very beginning of the game like so:

    public ItemType shortsword = new ItemType();
	public ItemType blunt = new ItemType();
	public ItemType dagger = new ItemType();
	public ItemType axe = new ItemType();
	public ItemType longsword = new ItemType();
	public ItemType handgun = new ItemType();
	public ItemType rifle = new ItemType();
	public ItemType bow = new ItemType();

public struct ItemType{
		public float xptolevel;
		public float currentxp;
		public float overallxp;
		public float currentlevel;
		public string name;
}

Now my question is how would i go about setting the current weapon type when the player picks up a different weapon. The reason why im confused is because I have the weapon’s type being held as a string (for example the weapon would have a type of “blunt)” so how would i set the current weapon type to that weapons type. I already have another script that handles all the information of “current weapon type” im just not sure how to change it because i cant simply set the current weapon type to the string value that the weapon is holding because of a difference in type. I understand this is a very vague question but i hope im getting the point of my question across… any help would be greatly aprreciated.

You need to define a constructor for your struct. This is a sort of “starting” method that you use when creating an object. So, you’d have something like this:

public ItemType(int xpto, int xpnow, int xptot, int lvl, string nm)
{
xptolevel = xpto;
currentxp = xpnow;
overallxp = xptot;
currentlevel = lvl;
name = nm;
}

Inside the struct definition. This allows you to create values like this:

  public ItemType shortsword = new ItemType(10, 0, 0, 1, "Short Sword");

As an aside, I recommend only using float types when you need non-integer values. Floating point arithmetic is notoriously approximate, so if you’re only ever going to need integer values (like, for example, an XP counter, or a level), use an integer value, int instead.

Now, for setting the type when it’s picked up, it’s a simple case of just creating a variable of type ItemType, which holds your current weapon, and setting it to the new weapon as you pick it up. In general, in your code, you want to keep clear of keeping important data as strings - strings are generally for displaying things to the end user, not for using in your code.

If you absolutely must use string labels for your weapons, I recommend you look up one of any number of key-value stores, like a Dictionary, which allow you to index an item from some pre-defined key, which could easily be a string.

This is where inheritance is good.

Define a new type for each ItemType:

public struct Axe : ItemType
{
}

public struct Bow : ItemType
{
}

etc.

Then these can do anything an ItemType can do like ‘Attack()’ and each sub-type (subclass or substruct so to speak) can implement ‘Attack()’ differently as needed.

You may want to put in ‘Blunt’ or ‘Mele’ as a first inheriter, and the blunt or mele weapons inherit from that (for ‘Swing()’ for example), with others like ‘Projectile’ with methods like Fire() and Reload() etc.