469,297 Members | 2,144 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,297 developers. It's quick & easy.

Class accessing form objects

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
3 3694
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
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
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.

Similar topics

2 posts views Thread by pc | last post: by
37 posts views Thread by JohnGoogle | last post: by
reply views Thread by David | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.