无法看到变量

时间:2015-12-27 07:02:55

标签: c# variables visibility

愚蠢的问题,但仍然。我有一些变量,让它为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();
    }
}

1 个答案:

答案 0 :(得分:1)

在你的根最namespace

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

然后,您就可以从任何地方访问它。