我有以下课程:
public class CreateNewWeapon : BaseWeapon
{
string[] weaponNames = { "Dragonslayer", "SoopaDoopa", "Mastersword" };
private BaseWeapon newWeapon;
public void CreateWeapon()
{
Random rnd = new Random();
int x = rnd.Next(0, weaponNames.Length);
newWeapon = new BaseWeapon();
newWeapon.ItemName = weaponNames[x];
newWeapon.Strength = rnd.Next(0, 5);
newWeapon.Vitality = rnd.Next(0, 5);
newWeapon.Intelligence = rnd.Next(0, 5);
}
}
并希望以我的主要形式获取值,以第二种形式设置标签:
private void button2_Click(object sender, EventArgs e)
{
newWep = new Items.CreateNewWeapon();
newWep.CreateWeapon();
ShowWeapon shWep = new ShowWeapon();
shWep.Label6 = newWep.Strength.ToString();
shWep.Visible = true;
}
但这些价值观甚至不会转移到第一张表格中,他们会在哪里丢失?
答案 0 :(得分:3)
您的代码存在多个问题:
我认为您应该将代码更改为:
public class CreateNewWeapon : BaseWeapon
{
string[] weaponNames = { "Dragonslayer", "SoopaDoopa", "Mastersword" };
public BaseWeapon CreateWeapon()
{
Random rnd = new Random();
int x = rnd.Next(0, weaponNames.Length);
var newWeapon = new BaseWeapon();
newWeapon.ItemName = weaponNames[x];
newWeapon.Strength = rnd.Next(0, 5);
newWeapon.Vitality = rnd.Next(0, 5);
newWeapon.Intelligence = rnd.Next(0, 5);
return newWeapon;
}
}
并在button2_Click方法中,以这种方式调用它:
var weapon = newWep.CreateWeapon();