Moving data to & from form/class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Blip

    #1

    Moving data to & from form/class


    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.Collecti ons.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.Collecti ons.Generic;
    using System.Text;
    using System.Windows. Forms;

    namespace Test

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

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

    private void PopulateListBox Fields (string message)
    {
    listBox1.Items. Add(t.total);
    }
    }
    }






  • Peter Duniho

    #2
    Re: Moving data to &amp; from form/class

    On Sat, 15 Dec 2007 17:43:27 -0800, Blip <blip@krumpli.c omwrote:
    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

    Comment

    Working...