calling public sub in Form

S

Strahimir Antoljak

Why can't I call a public function or sub defined in the main form of my
application from within a module or a class, all being a part of the
same assembly?

When I define public function in a class, I can call it from the form,
but if I define public function in the form and try to call it from the
class it does not work. Any idea what am I doing wrong?

Thanks,

Strah
 
C

Chris, Master of All Things Insignificant

You can, but you need to have an instance of the class to call it on, unless
it's a shared method.

//SomeModule
Dim F as New Foo
F.SomeSub <--This works fine

//SomeBadModule
F.SomeSub <-- Only works if Shared

Class Foo
Public Sub SomeSub()

End Sub
End Class

Hope this clears it up.
Chris
 
S

strah

Hi Chris,

Your input helped. I was creating new instance of the
Windows.System.Forms.Form instead of my form... it helped me to see my
error by looking at your code. Thanks,

Strah
 
S

strah

Hi Herfried,

This is precisely what I was looking for. One instance of the form per
session, and it is instantiated only in the beginning.

Thousands thanks,

Strah
 
C

Cor Ligthert

Chris,

In my opinion is your solution not using the parent class(form), however a
new form.

(Using this is very easy by the way)
dim frm as new form2
frm.owner = me

Cor
 
C

Cor Ligthert

Srahimir,

You can see the samples from Herfried and there is more, however remember
that is is bad OOP and you will probably get in problems when your project
grows.

Just my opinion.

Cor
 
S

strah

Hi Cor,

since you already put some effort replying, I would appreciate if you
could elaborate why is that bad OOP practice, maybe an example or two.

Thanks for your input in any case. I remember you well helping me in
several occasions so I think of your input highly.

Strah
 
C

Cor Ligthert

Strah,

The answer is in my opinion very simple the goal is to make classes
independent from each other.

In this case is your form2 is completely dependend from the properties on
form1

To say it in other words a change in a "name" of form1 will give an error in
form2 (and all other forms where you do it like this).

Cor
 
C

Cor Ligthert

Strah,

Looking at Herfrieds sample, I saw I misunderstood your question completly
wrong.

Sorry

Cor
 
C

Cor Ligthert

Chris,

I am not sure, however seeing Herfrieds answer (I did not open it, normally
I know them) than we both have interpretted Strahs answer wrong, so forget
my message. And when you had interpretted it right, than forget this as
well.

:)

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
The answer is in my opinion very simple the goal is to make classes
independent from each other.

Communication (passing data) between classes will always make them "depend"
on each other, independent from the fact that in this case we are dealing
with forms. It's good to reduce dependencies, but I don't undetstand why my
solution is considered "bad" -- there are solutions that are worse than the
one described in my article.

Just my 2 Euro cents...
 
S

strah

Herfried,

I appreciate everybody's input as this helps me (or anyone else
involved) to learn a thing or two. However this time, your solution was
only that really helped me out and that I applied with a great success!

... so, to me, it was like much more the 2 Euro cents,... more like a 2
thousand Euros :)

Thanks again ... and I agree about your comment concerning dependencies.
A form is just another class and it should behave (at least as far as
functions sharing) as any other 'regular' class. As long as one class
call for another they are dependent, whether they are forms or not. With
the clever use of constants, one should not ever have big time problems
changing names of the class and its name in the code accordingly.

regards,

Strah
 
C

Cor Ligthert

Herfried,

I have said sorry for my misunderstanding of the problem from Strah more
than an hour ago before you have sent this message.

I made that as well too Chriss.

What you want me to do, go to Wien to show you that and kneel at your feets?

Cor
 
H

Herfried K. Wagner [MVP]

Cor,

Cor Ligthert said:
I have said sorry for my misunderstanding of the problem from Strah more
than an hour ago before you have sent this message.

OK, it seems that I missed that message for some reason :-(.
What you want me to do, go to Wien to show you that and kneel at your
feets?

Definitely no. Nevertheless, if you plan to come to Vienna, feel free to
contact me.
 

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