473,774 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling public sub in Form



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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #1
15 9604
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
"Strahimir Antoljak" <sa*******@lang an.com> wrote in message
news:em******** ******@TK2MSFTN GP10.phx.gbl...


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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #2
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #3
"Strahimir Antoljak" <sa*******@lang an.com> schrieb:
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?


You can do that if you make a refernece to the main form's instance
available to the module.

Providing a reference to an application's main form
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=accessmainf orm&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #5
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
Nov 21 '05 #6
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

Nov 21 '05 #7

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #8
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
Nov 21 '05 #9

Thanks Cor!
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9783
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect towards this approach such as how would it works in multi-thread. Thanks Tony
6
3229
by: Gary Miller | last post by:
Does anyone know how to detect a modeless form on closing by the form that invoked the modeless form? form.Show();
2
6612
by: mark | last post by:
I am developing an application in .Net C# that needs to restore a number of tool windows to some previous location and size. The problem I have is that when I create the form and set the Location and Size properties before calling the Show method, the form is not created in the Location that I set. If I call Show first everything works fine but there is excesive flicker when the Location and Size properties are set. I tried calling...
6
1709
by: Amjad | last post by:
Hi, I want to make a project that calls and executes a function (VB code) made in a seperate file in the Application Folder. I know I can create the function in my project and call it internally, but I want to put the function's code in an external file, so that future updates to the function will require a replacing the function file instead of re-installing the whole project. Can you give me ideas on how I can make small updates to my...
12
7095
by: Ron | last post by:
Greetings, I am trying to understand the rational for Raising Events instead of just calling a sub. Could someone explain the difference between the following 2 scenarios? Why would I want to raise an event instead of just calling the sub? Scenario1 -- Using Events ------------------------------------------------------ Public Class Form1
7
4987
by: Christian Wilhelm | last post by:
Hi! I'm trying to call a Java WebService out of a .net Client. There are two Methods, one Method requires one Parameter of type Parameter, the other Method requires one Parameter of type Parameter. I can call the first Method without Problems, the Parameter can be deserialized by the WebService. But if I want to call the second Method and give it an Array of Parameters, then the following exception is thrown by the WebService:...
2
27265
by: Macca | last post by:
Hi, I have a windows form project. The form class has a number of methods in it that i want to call from another class I have just created. This class is a finite state machine and is created and kicked off in same method where the applications' Run method is called. i.e main method in Program.cs. I'd appreciate suggesstions for calling methods in the form class from this
5
2248
by: Stephen Plotnick | last post by:
I'm very new to VB.NET, or any VB. I need to have form1 call form2 which calls form3, etc. I am able to use oledb in form1 to get the data. I build all the data for form2 and form3 from form1 I can call form2 from form1 without any probles, all the data is there. I can change to call to form2 to form3 and all the data is there and I can see
4
2255
by: Bugs | last post by:
Hi, I wonder if anyone can help me out. I'm building a vb.net application that has a form with a panel that contains several other sub forms (as a collection of controls). What I'm wanting to do is call a generically named public sub in the top-most sub form in the panel. I have the name of the top-most form as a string (eg. g_TopForm = "frmCustomers") and I can reference the form with:
1
1752
by: SAL | last post by:
Hello, I'm developing this remoting app (.net 2.0) and I need to bring the server app's form to the front often. Since you can not do cross-thread communication on a form or control without using Invoke, I declared a delegate to handle the cross-thread call to bring the form forward. Here's my code: Delegate Sub BringMeFront() Public myDelBringFormFront As BringMeFront
0
10267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.