如何在课堂外访问该属性

时间:2016-07-08 21:44:17

标签: c#

这是我拥有财产的地方:

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为空。我试图强制转换(proj)schemaPath,它仍然是null。每当我保存项目时,schemaPath都没有价值。

1 个答案:

答案 0 :(得分:0)

    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()(确保单击按钮时发生按钮点击事件,只需要那里的消息框)然后是的,字符串的值应该改变

相关问题