Class accessing form objects

S

SamIAm

Hi

I have a form named form1. I have a class called class1. I want to call a
recursive method in class1 called MyMethod()

I would like log info to a listbox, listbox1, on the form as MyMethod
performs its recursion. How do I pass an instance into class1 so that it can
add items to the listbox?

Should be easy I guess.

S
 
C

Christian Schwendtner (CS)

Hi

I have a form named form1. I have a class called class1. I want to call a
recursive method in class1 called MyMethod()

I would like log info to a listbox, listbox1, on the form as MyMethod
performs its recursion. How do I pass an instance into class1 so that it can
add items to the listbox?

Should be easy I guess.

S

Hi.

public class class1 {
public void MyMethod(ListBox listBox) {
...
}
}

or you pass the ListBox to the constructor of class1.

hth,
Chris
 
S

Simon Trew

I would prefer to make the reference to the list box a member variable, and
not pass it at each recursion, which is wasteful on the stack:

public class class1 {
{
private ListBox listBoxForLogging;

public void MyMethod(ListBox listBoxForLogging)
{
this.listBoxForLogging = listBoxForLogging;
MyRecursiveMethod(1);
}

private Log(string message)
{
ListBoxForLogging.Items.Add(message);
}

private void MyRecursiveMethod(int depth) // trivial example
{
Log("depth = " + depth.ToString());
if (depth < 10)
{
MyRecursiveMethod(depth + 1);
}
}
}

This is a typical pattern for recursion, with a public "shell" method that
performs some initialisation and calls a private method that actually does
the recursion. Not sure if this pattern has a common name. In Pascal, which
allows procedures to be nested, it is common to define the private recursing
method *inside* the public shell method; the inner method gets access to the
shell method's variables because of the scoping rules.

S.
 
S

SamIAm

Thanks for the replies


Simon Trew said:
I would prefer to make the reference to the list box a member variable, and
not pass it at each recursion, which is wasteful on the stack:

public class class1 {
{
private ListBox listBoxForLogging;

public void MyMethod(ListBox listBoxForLogging)
{
this.listBoxForLogging = listBoxForLogging;
MyRecursiveMethod(1);
}

private Log(string message)
{
ListBoxForLogging.Items.Add(message);
}

private void MyRecursiveMethod(int depth) // trivial example
{
Log("depth = " + depth.ToString());
if (depth < 10)
{
MyRecursiveMethod(depth + 1);
}
}
}

This is a typical pattern for recursion, with a public "shell" method that
performs some initialisation and calls a private method that actually does
the recursion. Not sure if this pattern has a common name. In Pascal, which
allows procedures to be nested, it is common to define the private recursing
method *inside* the public shell method; the inner method gets access to the
shell method's variables because of the scoping rules.

S.

call it
 

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