1

I got a little question that causes me some problems, I am sure its not difficult, but for me right now it is.

I got two classes, one main-class and the class of my winform.

 foreach (EA.Element theElement in myPackage.Elements) { foreach (EA.Attribute theAttribute in theElement.Attributes) { attribute = theAttribute.Name.ToString(); value = theAttribute.Default.ToString(); AddAttributeValue(attribute, value); } } 

Here I get the values and try to write them into an Datagrid, via this method:

private void AddAttributeValue(string attribute, string value) { int n = dataGridView1.Rows.Add(); dataGridView1.Rows[n].Cells[0].Value = attribute; dataGridView1.Rows[n].Cells[1].Value = value; } 

But the compiler tells me, that AddAttributeValue is not in the current context and I cannot call it. I got the values I want, but cannot pass them over to the form. I know it sounds trivial, but I just can't get it.

2
  • 1
    Standard OOP question, you need a reference to the form object. And make the method public. Commented Jul 18, 2013 at 13:44
  • I had it on public at the beginning, thats not the problem and not the solution, it must be something different, but thankyou anyway. Commented Jul 18, 2013 at 13:54

2 Answers 2

1

If I did understood, the code snippets provided are in diferent classes.

In this case the method should be public.

Like that:

public void AddAttributeValue(string attribute, string value) { int n = dataGridView1.Rows.Add(); dataGridView1.Rows[n].Cells[0].Value = attribute; dataGridView1.Rows[n].Cells[1].Value = value; } 
Sign up to request clarification or add additional context in comments.

Comments

1

Make 'AddAttributeValue' public:

public void AddAttributeValue(string attribute, string value) 

Addendum:

As per my comment below, here's how you'd implement a callback, to allow your main-class to call a method within your winform when it doesn't otherwise have an instance member to refer to:

Your MainClass will look something like this:

public static class MainClass { public delegate void AddAttributeValueDelegate(string attribute, string value); public static void DoStuff(AddAttributeValueDelegate callback) { //Your Code here, e.g. ... string attribute = "", value = ""; //foreach (EA.Element theElement in myPackage.Elements) //{ // foreach (EA.Attribute theAttribute in theElement.Attributes) // { // attribute = theAttribute.Name.ToString(); // value = theAttribute.Default.ToString(); // AddAttributeValue(attribute, value); // } //} // // etc... callback(attribute, value); } } 

then in your Winform class, you'd call the method like so:

MainClass.DoStuff(this.AddAttributeValue); 

That'll mean that when 'DoStuff' completes, the method called 'AddAttributeValue' gets called.

4 Comments

thank you, but thats not the solution, i had it also at public. the problem is something else.
Having another look at your code, it looks like you're trying to call an instance member in another class, but that you don't have an instance to refer to. Your code is looking for a method called 'AddAttributeValue' inside your main-class. You either need to get an instance of your winform and refer to its implementation of 'AddAttributeValue', or make AddAttributeValue static and refer to it via MyWinformClass.AddAttributeValue. (NB: if you do the second one, there may be some issues with accessing instance-specific members of your winform class such as your 'dataGridView1' member).
What I'd recommend instead is passing a callback into your main-class. I'll provide another answer in a minute that demonstrates that idea at work.
Thanks Rachel, in a very long moment of stupidness I referenced wrong, thats why i came up with this trivial question, but thank you for your help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.