簡體   English   中英

我如何在課外訪問屬性

[英]How can I access the property outside of the class

這是我的財產:

public partial class NewProjectForm : Form
{
private string mSchemaPath = string.Empty;
public string SchemaPath
    {
        get
        {
            return mSchemaPath;
        }
        set
        {
            mSchemaPath = value;
        }
    }
private void OkayBtn_Click(object sender, EventArgs e)
    {
        mSchemaPath = SchemaPathTB.Text;
//SchemaPathTb is where I get the path for the schema.
    }

我想訪問Form1類中的SchemaPath屬性。 這就是我所做的,並且我不斷獲得空引用。 不過也沒有例外,只是null。

public partial class Form1 : Form
{
public void saveProject()
{
NewProject proj = new NewProject();
XmlDocument doc = new XmlDocument();
XmlNode root = doc.CreateNode(XmlNodeType.Element, "testXml", null);
XmlElement schemaPath = doc.CreateElement("Schema");
schemaPath.SetAttribute("Path", proj.schemaPath);
root.AppendChild(schemaPath);
}

這里proj.schemaPath為null。 我嘗試轉換(proj)schemaPath,但它仍然為null。 每當我保存項目時,schemaPath都沒有值。

    public partial class Form1 : Form
{
public void saveProject()
{
NewProject proj22 = new NewProject(); //Create new with emtry string
proj22.SchemaPath = "cfasf"; //Dow whatever you want to the string
proj = proj22; //Set it equal to the class you havel already played with the string
XmlDocument doc = new XmlDocument();
XmlNode root = doc.CreateNode(XmlNodeType.Element, "testXml", null);
XmlElement schemaPath = doc.CreateElement("Schema");
schemaPath.SetAttribute("Path", proj.schemaPath);
root.AppendChild(schemaPath);
}

我並沒有真正獲得要實現的目標,但是...如果要打開一個新的表單窗口並從該表單上的元素獲取文本,則必須首先打開它,當然是formname.show() (確保單擊按鈕時發生按鈕單擊事件,只需在其中放置一個消息框),然后是,字符串的值應更改

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM