Moving data to & from form/class

B

Blip

I am still struggling with a pattern that can be used to move data
from a class to a related form, and from the class method results to
text boxes, listboxes, etc on the form. I usually get it working by
adding enough contructors and instances (but I'm sure that I don't
always point at the form or class instant that I am expecting).

This is frustrating because it should be so simple. BTW, I tried
putting:

Tester t = new Tester(); on the form1 load event, and
Form1 f = new Form1(); at the top of the class the class.

This resulted in an infinite loop. I hope that this code describes my
issue, but if it dowsn't please let me know what you want to see.
Thanks very much for taking some time with this.


tester.cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Test
{
public class Tester
{

public int total;

public void Foo()
{
string b = "bar"
}

public void Bar() {
{
int i = 0;

int j = 0;
int k = 0;
int m = 0;
int n = 0;
int p = 0;

for (i=0;i<entries<i++)
int sum1 = j + k;
sum2 = m+n+p;
total = sum1 + sum2;
}



Here is the form1.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Test

public partial class Form1 : Form
{
public Tester t = new Tester();
public Form1()
{
InitializeComponent();
}

private void buttonBegin_Click(object sender, EventArgs e)
{
listBox1.Items.Add(t.b);
}

private void PopulateListBoxFields (string message)
{
listBox1.Items.Add(t.total);
}
}
}
 
P

Peter Duniho

I am still struggling with a pattern that can be used to move data
from a class to a related form, and from the class method results to
text boxes, listboxes, etc on the form.

The exact pattern will depend on your specific needs in a particular
situation. However, the basic idea is always the same: instantiate the
objects as is appropriate for the lifetime of the object, not specifically
for the purpose of exposing one object to another. Then, make a different
(but possibly related) design choice regarding exposing one object to
another.

Usually this latter step involves passing a reference for the instance of
one class to the instance of another. Occasionally, some other mechanism
will be used, such as a singleton or some sort of static variable or
collection.

What's correct just depends on your specific situation.
I usually get it working by
adding enough contructors and instances (but I'm sure that I don't
always point at the form or class instant that I am expecting).

IMHO, you should go about it in a more directed way. A sure path to
terrible, buggy, difficult-to-maintain code is to just add stuff until it
works. You should get out of that habit asap.
This is frustrating because it should be so simple. BTW, I tried
putting:

Tester t = new Tester(); on the form1 load event, and
Form1 f = new Form1(); at the top of the class the class.

This resulted in an infinite loop.

Yes, of course it would. Fortunately in that case, "adding enough
constructors and instances" just blew up. But it's a good demonstration
of why a more directed approach is better.
I hope that this code describes my
issue, but if it dowsn't please let me know what you want to see.

Well, the one thing I don't understand in the code you posted is what
exactly you're trying to show. I would say that the biggest issue is that
your Form1 class tries to reference a non-existent member "b" of the
Tester class. You've got a "b" in the Tester class, but it's a local
variable. It would have to be a public field or property for Form1 to get
at it.

Other than that problem I would think that the code would work fine as
written (not counting missing braces and whatnot, which I'll assume is
just a mistake that happened when posting the code).

Maybe you can be more specific about what in the code you posted isn't
working, and how you'd like it to work.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top