Home All Groups Group Topic Archive Search About

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

Author
7 Jun 2006 1:32 PM
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.

Author
7 Jun 2006 1:51 PM
tomb
Bmack500 wrote:

Show quoteHide quote
>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
Author
7 Jun 2006 1:55 PM
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!

tomb wrote:
Show quoteHide quote
> Bmack500 wrote:
>
> >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
Author
7 Jun 2006 2:06 PM
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.

Bmack500 wrote:
Show quoteHide quote
> 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!
>
> tomb wrote:
> > Bmack500 wrote:
> >
> > >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
Author
7 Jun 2006 2:20 PM
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


Show quoteHide quote
"Bmack500" <brett.m***@gmail.com> wrote in message
news:1149689186.760273.260620@c74g2000cwc.googlegroups.com...
> 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.
>
> Bmack500 wrote:
>> 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!
>>
>> tomb wrote:
>> > Bmack500 wrote:
>> >
>> > >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
>
Author
7 Jun 2006 2:45 PM
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.

RMT wrote:
Show quoteHide quote
> 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
>
>
> "Bmack500" <brett.m***@gmail.com> wrote in message
> news:1149689186.760273.260620@c74g2000cwc.googlegroups.com...
> > 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.
> >
> > Bmack500 wrote:
> >> 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!
> >>
> >> tomb wrote:
> >> > Bmack500 wrote:
> >> >
> >> > >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
> >
Author
7 Jun 2006 1:54 PM
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.



Show quoteHide quote
"Bmack500" <brett.m***@gmail.com> wrote in message
news:1149687126.934687.182100@h76g2000cwa.googlegroups.com...
> 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.
>
Author
7 Jun 2006 3:11 PM
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

Show quoteHide quote
"Bmack500" <brett.m***@gmail.com> schreef in bericht
news:1149687126.934687.182100@h76g2000cwa.googlegroups.com...
> 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.
>