无法查看变量

本文关键字:变量 | 更新日期: 2023-09-27 18:28:05

这个愚蠢的问题,但仍然存在。我有一些变量,让它成为MyVariable,它在代码背后的一个方法中获得它的值,例如在MouseClick中,但MyVariable的值在另一个类的方法中使用(我使用不同的模式)。当然,在这个类中,MyVariable是不可见的,但做什么更好呢?

public partial class MainWindow : Window
{
    Hero Hero1 = new Hero();
    Notify newNotify = new Notify();
    public MainWindow()
    {
        InitializeComponent();
        Collections coll = new Collections();
    }
    private void weapons_DragLeave(object sender, DragEventArgs e)
    {
        Hero1.Attack = ((Weapon) listweapons.SelectedItem)._attack;
        newNotify.ThrowHero().Attack  = Hero1.Attack;
        newNotify.Dropped += Show_Message;
        newNotify.DroppedEquipment();
        TextBox2.Text = newNotify.ThrowHero().Description;//this gets its value from attack
    }
    public void Show_Message()
    {
        Console.WriteLine(newNotify.ThrowHero().Description =
             "Защита:" + newNotify.ThrowHero().Protection + "атака:" + newNotify.ThrowHero().Attack);
    }
}

然后我在另一类中有另一种方法

public class SavingInWord : IWordSaving
{
    public void ExportToWord()
    {
        var wordApp = new Word.Application();
        wordApp.Visible = false;
        Word.Document doc = wordApp.Documents.Add();
        doc.Select();
        wordApp.Selection.TypeText("Description:" + newNotify.ThrowHero().Description); //newNotify.ThrowHero().Description) can't be seen here, but i need to have the value here which i got from weapons_DragLeave method
        doc.SaveAs(@"D:...doc");
        wordApp.Visible = true;
    }
}

另一类:

public class Notify
{
    Hero hero1 = new Hero();
    public Hero ThrowHero()
    {
        return hero1;
    }
    public delegate void MyEventhandler();
    public event MyEventhandler Dropped;
    public void DroppedEquipment()
    {
        if (Dropped != null)
            Dropped();
    }
}

无法查看变量

在您的根目录中,大多数namespace

public static class Globals
{
    Notify newNotify = new Notify();
}

然后你就可以从任何地方访问它了。