How do I use a class, when the class requires functions in the main form?

B

Bmack500

I'm using visual studio 2003. I have much of my code in my main form,
called 'frmMain'.

When I create a class to perform string ops and such, it sometimes
needs to refer to functions in the main form.

However, when I use "inherits frmMain" in the class, as soon as I try
to build/run the program it gives me a System.stack overflow error. No
other indication of what's wrong.

Is this bad programming practice, or is there another way to refer to a
function in the main form?

Any help would be greatly appreciated.
 
T

tomb

Bmack500 said:
I'm using visual studio 2003. I have much of my code in my main form,
called 'frmMain'.

When I create a class to perform string ops and such, it sometimes
needs to refer to functions in the main form.

However, when I use "inherits frmMain" in the class, as soon as I try
to build/run the program it gives me a System.stack overflow error. No
other indication of what's wrong.

Is this bad programming practice, or is there another way to refer to a
function in the main form?

Any help would be greatly appreciated.
Pass a reference to the main form, and declare those functions as
public. Or, put the functions in another class, and pass the class to
the new class.

T
 
R

RMT

Yes very bad. The string ops class should be self-contained, ie. the main
form should execute methods on it, not the other way around.

If you really must do this, then give the string ops class a property called
m_MainForm or pass it into the method. However, this is not very OOP, it's
more Visual Basic 6.
 
B

Bmack500

Can you show me how to do the first - pass a reference to the main
form? Pardon my ignorance, I've just had to learn on the fly and could
really use formal training!
 
B

Bmack500

Okay, I've put: Inherits frmMain in the class. For some reason, I'm not
getting the stack overflow now.
So is this an example of what I should not do? I can do without the
functions, but I need access to a structure I've declared that holds
program configuration info derived from an XML file I've created.
 
R

RMT

Well what methods do you have on this class?

Public Class myClassThatUsesMainForm

Public Sub doSomethingWithMainForm ( byval theForm as frmMain )

End Sub

Public Sub doSomethingelseWithMainForm ( byval theForm as frmMain)

End Class



Public Class frmMain

....
....
Public Sub DoingSomething ()

...

Dim myThing as new myClassThatUsesMainForm

myThing.doSomethingWithMainForm ( Me )

....

End Sub

End Class
 
B

Bmack500

Thanks, you've answered my question. I need to improve my OOP
knowledge; I'll try to encapsulate the class so that it doesn't need
anything from the main form, and I'll set properties on the class to
use for the config info.
 
C

Cor Ligthert [MVP]

BMack,

Mostly you create an extra form like this.

dim frm as new Form2


if you do than
frm.Owner = me

Than you can use in your form2 class
Directcast(me.owner, frmMain).Textbox1.Text = "Hello"
to put by instance "Hello" in that textbox on frmMain.

Don't understand the "me" wrong. In the frmMain class tells it that it is
that class, while in the form2 class that it is the form2 class.

In my idea is this one of the easiest solutions for your problem.

I hope this helps,

Cor
 

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