By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,666 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,666 IT Pros & Developers. It's quick & easy.

Class accessing form objects

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Tue, 8 Jul 2003 16:19:46 +1000, SamIAm wrote:
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
Nov 13 '05 #2

P: n/a
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.

"Christian Schwendtner (CS)" <sc*********@grz.at> wrote in message
news:9s****************************@40tude.net...
On Tue, 8 Jul 2003 16:19:46 +1000, SamIAm wrote:
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

Nov 13 '05 #3

P: n/a
Thanks for the replies
"Simon Trew" <ten.egnaro@werts> wrote in message
news:ux*************@TK2MSFTNGP11.phx.gbl...
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.

"Christian Schwendtner (CS)" <sc*********@grz.at> wrote in message
news:9s****************************@40tude.net...
On Tue, 8 Jul 2003 16:19:46 +1000, SamIAm wrote:
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


Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.