1

I have 2 windows Forms, a parent and a child. The parent is the main form. The child is a dialog where the user can edit their details.

When a button is clicked on the parent form, it loads the child form. Like so:

private void add_account_Click(object sender, EventArgs e)
{
     this.account_add_edit = new Form2();
     account_add_edit.test();
     account_add_edit.ShowDialog();
}

As you can see I've created a new form, tried to call a function from the new form, and then showed the form. The problem is the method from the form is not being called. I am getting an error on the IDE that says Windows.Forms.Form does not contain a method test.

I've created the method in the child form:

public static string test(string val)
{
    this.username = val;
}

Any ideas as to what I'm doing wrong?

3
  • 4
    Well, at least the method should not be static, because: 1 - you are trying to access instance field from it; 2 - you are trying to call it on instance of Form2. Commented May 1, 2011 at 6:55
  • 1
    You're not passing the requisite string parameter to it when you call it either. Commented May 1, 2011 at 6:57
  • Sorry jonsca, I typed it out and forgot to pass the string. Commented May 1, 2011 at 7:00

2 Answers 2

2

your method is defined as static , so its not posiible to call it on an instace. you should eaither not make it static, or call it from as static:

Form2.test();
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, fixed. I was create a class of type Form and not of type Form2. Form2 is the form class for the child (poor naming I know). Works now.
0

Use:

   Form2.test();

static members are associated directly to the class not to its instances. Than means if you need to access a static member you have to access it using it is container type.

More than that, you can not access normal members from static ones. You can only access staticmembers from their peers.

You cannot do the following inside a static method:

this.Member ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.