cam
cam

Reputation: 9043

Trying to figure how to call function from other class without making it static

I'm trying to change a listbox in my main form from another file (nodes.cs) which contains another class. I created a class in my main form that changes the textbox for me so all I need to do it pass the string to it. Unfortunately, I can't access the function from the other class unless I make the String-changing-class static. If I make it static, I can't change the listbox without getting an error: An object reference is required for the non-static field, method, or property...

I know this means I need to create the object or make it non-static. I find the whole class thing rather confusing. I have to initiate a whole new form object to access it? Anyways.

How do I go about accessing a Listbox from another Class, contained in another file? The two classes are in the same namespace.

there's no real point in adding what I have, it's a huge amount of code, and i erased everything I've tried already...

MAIN.CS
    namespace neuralnetwork
{
    public partial class mainform : Form
    {
yada yada
public static void changetext(string text)
{
listbox1.items.add(text);
}
    }
    }

Secondary.cs

    namespace neuralnetwork
    {
    class lolercopter
    {
    public static void dolol()
    {
    //here is where I want to change the mainforms textbox.
mainform.changetext(s);
    }
    }
    }

This is essentially what I have. I've been reading for over an hour on this...

Upvotes: 0

Views: 1586

Answers (3)

Jamie Ide
Jamie Ide

Reputation: 49301

You can pass a reference to mainform into your method:

public static void dolol(mainform frm)
{
    frm.changetext(s);
}

Your question leads me to suspect that you have some serious architecture issues with this application, but hopefully this solution can work for you.

Upvotes: 1

tvanfosson
tvanfosson

Reputation: 532745

How is your nodes class created? Is it created by the form? If so, you could pass in a reference to the form when you create the nodes class.

For example, say you have this code in a callback in the form.

  var nodes = new Nodes();
  nodes.UpdateSomething( args );

You could change the constructor for the Nodes class so that it takes a reference to form. This is called Dependency Injection, specifically constructor injection. Your class has a dependency on the form, you provide the form when you create the class.

 var nodes = new Nodes( this ); // "this" is a reference to the form
 nodes.UpdateSomething( args );

Your Nodes class would then use the helper as:

 public class Nodes
 {
     private Form TheForm { get; set; }

     public Nodes( Form form )
     {
          this.TheForm = form;
     }

     public void UpdateSomething( EventArgs args )
     {
         ...
         this.Form.ChangeText( newValue );
         ...
     }
}

The basic idea is to provide the class the resources that it needs access to via the constructor so you don't have to make use of long-lived object references and static classes.

EDIT: I've updated this to reflect your code sample. Note that you're not providing a new class in the form to change the list box, but rather a method.

Upvotes: 1

John Weldon
John Weldon

Reputation: 40799

classes are like blueprints.

What you're asking is like asking how to open the door down the hall on the blueprint.

It sounds like you want action on one form to trigger action or changed state on another form. That could be achieved by storing state in a database, or in memory, but ideally by having a reference to the instantiated mainform.

Upvotes: 1

Related Questions