Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何从对象中访问彼此_C#_Winforms - Fatal编程技术网

C#如何从对象中访问彼此

C#如何从对象中访问彼此,c#,winforms,C#,Winforms,在下面的代码中,我试图找出这些表单如何可以自由访问它们的两个对象。在这些代码中 ITEMCount可以访问SIMSProduct的对象 另一方面,尽管SIMSProduct可以看到ITEMCount的对象 (没有给出与“SIMSProduct.SIMSProduct(ITEMCount)”的必需形式参数“view”相对应的参数) 访问流如何工作: public partial class MyFirstForm : Form { private string SomeString

在下面的代码中,我试图找出这些表单如何可以自由访问它们的两个对象。在这些代码中

  • ITEMCount可以访问SIMSProduct的对象
  • 另一方面,尽管SIMSProduct可以看到ITEMCount的对象 (没有给出与“SIMSProduct.SIMSProduct(ITEMCount)”的必需形式参数“view”相对应的参数)


访问流如何工作:

public partial class MyFirstForm : Form
{
    private string SomeString = "Some private variable";
    public string PublicString = "Some public variable";

    public MyFirstForm()
    {
        InitializeComponent();            
    }

    private void OnButtonClick(object sender, EventArgs e)
    {
        using(Form2 f = new Form2(this))
        {
            f.ShowDialog();
        }
    }
}

public class Form2 : Form
{
    private MyFirstForm form;

    public Form2(MyFirstForm form)
    {
        InitializeComponent();

        if(form == null)
            throw new Exception("Form you passed is not assigned!");

        this.form = form;
        form.SomeString = "I just changed string in first form";
    }
}

在评论中,您问我
如果我以表单2:private-string SomeoneString=“Some-private-variable”创建它会怎么样;我怎样才能在表格1中访问它?
-答案是您不能-不能用简单的方法,这是因为
私有
变量不能直接从其他类访问。有3种解决方案。首先是将该变量公开并按如下方式使用:

public partial class TestForm : Form
{
    TestForm2 tf2; //Create global Variable for Second form which will be accessible from whole First Form Class

    public string StringInFirstForm = "Blank"; //Need to be public

    public TestForm()
    {
        InitializeComponent();

        tf2 = new TestForm2(this); //Assign new object(form) to globally created variable(Second form)
        tf2.Show(); //Important to use Show not ShowDialog since if you use ShowDialog it will stop further code from executing
    }
}

public partial class TestForm2 : Form
{
    TestForm tf;

    public TestForm2(TestForm tf)
    {
        InitializeComponent();
        this.tf = tf;

        tf.StringInFirstForm = "I have changed string";
    }
}
访问私有变量的第二种方法是使用属性:

public partial class TestForm : Form
{
    TestForm2 tf2; //Create global Variable for Second form which will be accessible from whole First Form Class

    public string StringProperty
    {
        //Since there is no get{ } user can only SET property and when he does that property gets passed value and put that value into private variable in this class
        set
        { 
            StringInFirstForm = value;
            MessageBox.Show("You have changed value of StringInFristForm using property named StringProperty");
        }
    }
    private string StringInFirstForm = "Blank";

    public TestForm()
    {
        InitializeComponent();

        tf2 = new TestForm2(this); //Assign new object(form) to globally created variable(Second form)
        tf2.Show(); //Important to use Show not ShowDialog since if you use ShowDialog it will stop further code from executing
    }
}

public partial class TestForm2 : Form
{
    TestForm tf;

    public TestForm2(TestForm tf)
    {
        InitializeComponent();
        this.tf = tf;

        tf.StringProperty = "I have changed string";

        string Test = tf.StringProperty; // This will return error which will say that property could only be set but not get
    }
}
在类中设置私有值的第三个选项是通过public方法

public partial class TestForm : Form
{
    TestForm2 tf2;

    private string StringInFirstForm = "Blank";

    public TestForm()
    {
        InitializeComponent();

        tf2 = new TestForm2(this);
        tf2.Show();
    }

    public void ChangeVariable(string Whatever)
    {
        StringInFirstForm = Whatever;
        MessageBox.Show("You have changed StringInFirstForm value using method ChangeVariable");
    }
}

public partial class TestForm2 : Form
{
    TestForm tf;

    public TestForm2(TestForm tf)
    {
        InitializeComponent();
        this.tf = tf;

        tf.ChangeVariable("Some String");
    }
}

访问变量的可能性很小。希望有帮助:)

此代码甚至不应该运行,因为
SIMSProduct
需要
ITEMCount
作为参数hmmm。。你能给我一个正确的方法吗。例如。。访问彼此对象我将在几分钟内完成,但这并不能解决此问题。有时,一个好的建议/示例可以帮助我自己解决此问题。谢谢,如果我在表单2中创建它:private string SomeoneString=“Some private variable”;我如何才能在表格1中访问此内容?@Anonymous查看编辑。我已经尽了我所能来解释更多的事情,所以你们知道这是为了将来:)非常感谢你们
public partial class TestForm : Form
{
    TestForm2 tf2;

    private string StringInFirstForm = "Blank";

    public TestForm()
    {
        InitializeComponent();

        tf2 = new TestForm2(this);
        tf2.Show();
    }

    public void ChangeVariable(string Whatever)
    {
        StringInFirstForm = Whatever;
        MessageBox.Show("You have changed StringInFirstForm value using method ChangeVariable");
    }
}

public partial class TestForm2 : Form
{
    TestForm tf;

    public TestForm2(TestForm tf)
    {
        InitializeComponent();
        this.tf = tf;

        tf.ChangeVariable("Some String");
    }
}