如何将变量从一个类传递到另一个类。 [英] How to pass a variable from one class to another.

查看:109
本文介绍了如何将变量从一个类传递到另一个类。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要用inputinfo.cs输入的用户输入替换form2中的word文档中的一些单词,我不能引用它。



所以在Form2.cs中应该出现以下内容但是我不能引用变量sowname,cuslongname,cusaddress,engagementnum,quotenum,todaysdate:





Hi,

I need to replace some words in a word document in "form2" with user input which he inputs in "inputinfo.cs" and I cannot reference it.

so in Form2.cs the below should appear but i cannot reference the variables sowname, cuslongname,cusaddress,engagementnum,quotenum,todaysdate:


// Find Place Holders and Replace them with Values.
             this.FindAndReplace(wordApp, "<name>", sowname);
             this.FindAndReplace(wordApp, "<address>", cuslongname);
             this.FindAndReplace(wordApp, "<city>" , cusaddress);
             this.FindAndReplace(wordApp, "<state>", engagementnum);
             this.FindAndReplace(wordApp, "<zip>", "todaydate");
             this.FindAndReplace(wordApp, "<company>", quotenum);







谢谢!




thanks!

推荐答案

那是因为它们是局部变量:

That's because they are local variables:
public void Submit_Click(object sender, EventArgs e)
    {
    string sowname;
    string cuslongname;
    string cusaddress;
    string engagementnum;
    string quotenum;
    string todaydate;
    string serviceagr;
    string serviceagrdate;
    string sename;
    string senum;
    string seemail;
    string setitle;
    ;
    
    sowname = SoWName.Text;
    cuslongname = CusLongName.Text;
    cusaddress = CusAddress.Text;
    engagementnum = EngagementNum.Text;
    quotenum = QuoteNum.Text;
    todaydate = TodayDate.Text;
    serviceagr = ServiceAgr.Text;
    serviceagrdate = ServiceAgrDate.Text;
    sename = SEName.Text;
    senum = SENum.Text;
    seemail = SEemail.Text;
    setitle = SETitle.Text;
    }



所有这些变量都是按钮点击方法的本地变量:这意味着它们仅在方法体内可用,并在方法结束。

相反,将它们变为属性,您将能够从其他形式访问它们:


All of those variables are local to the button click method: which means they are available only within the body of the method, and are discarded when the method ends.
Instead, make them into Properties and you will be able to access them from the otehr form:

public string SoWName { get; private set; }
public string CusLongName; {get; private set; }
...
public void Submit_Click(object sender, EventArgs e)
    {
    SoWName = SoWName.Text;
    CusLongName = CusLongName.Text;
    ...
    }


这篇关于如何将变量从一个类传递到另一个类。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆