473,813 Members | 3,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check if a Public Method exists for a form and execute it

I need a way to do the following and cannot seem to find a solution via
google.

1. Have a method from the main app to get all open forms
2. Check each open form for a public method
3. If this public method exists execute it which will result in the
displayed form being updated. There will be no data loss as the forms
will only contain listviews or readonly fields.

Regards
Jeff
Jun 27 '08 #1
7 1945
On Fri, 09 May 2008 20:59:27 -0700, Jeff <jeff@[_nospam_].hardsoft.com.a u>
wrote:
I need a way to do the following and cannot seem to find a solution via
google.

1. Have a method from the main app to get all open forms
2. Check each open form for a public method
3. If this public method exists execute it which will result in the
displayed form being updated. There will be no data loss as the forms
will only contain listviews or readonly fields.
I don't really understand the last statement, as neither the question of
ListView instances or of readonly fields affects whether there could
potentially be data loss.

However, to address your specific question, you are looking for the
Reflection namespace. You can use GetType() on each form instance to get
its actual type, and then use that Type instance to look for particular
members, including a method to invoke.

That said, using Reflection should be a last resort. As an example here,
it would be much better if each form that might need this method to be
called instead implemented the method as an event handler. Then the
application would define an event each form could subscribe to. When the
event was raised, each form would then have its method executed
automatically, without having to go through all the reflection rigamarole.

If for some reason that and any other potential alternative isn't
possible, then reflection is your answer.

Pete
Jun 27 '08 #2
Thanks Peter

Something new for me to learn and try.

This opens up something I did not know you could do and it seams that
events can be across applications and one application can fire an even
in another if they are both subscribed.

Now to find a good article and sample code.

Jeff

Peter Duniho wrote:
On Fri, 09 May 2008 20:59:27 -0700, Jeff
<jeff@[_nospam_].hardsoft.com.a uwrote:
>I need a way to do the following and cannot seem to find a solution
via google.

1. Have a method from the main app to get all open forms
2. Check each open form for a public method
3. If this public method exists execute it which will result in the
displayed form being updated. There will be no data loss as the forms
will only contain listviews or readonly fields.

I don't really understand the last statement, as neither the question of
ListView instances or of readonly fields affects whether there could
potentially be data loss.

However, to address your specific question, you are looking for the
Reflection namespace. You can use GetType() on each form instance to
get its actual type, and then use that Type instance to look for
particular members, including a method to invoke.

That said, using Reflection should be a last resort. As an example
here, it would be much better if each form that might need this method
to be called instead implemented the method as an event handler. Then
the application would define an event each form could subscribe to.
When the event was raised, each form would then have its method executed
automatically, without having to go through all the reflection rigamarole.

If for some reason that and any other potential alternative isn't
possible, then reflection is your answer.

Pete
Jun 27 '08 #3
On Fri, 09 May 2008 23:10:40 -0700, Jeff <jeff@[_nospam_].hardsoft.com.a u>
wrote:
This opens up something I did not know you could do and it seams that
events can be across applications and one application can fire an even
in another if they are both subscribed.
Well, when I wrote "event" I was speaking of the .NET/C# variety of
events. The unmanaged Windows API also has the concept of "events", but
they aren't subscription based, and while named events can be
cross-application (i.e. cross-process), they are only useful for
communicating a toggle state.

There was nothing in your original question that suggested you needed a
cross-process solution. If you do, then neither reflection nor a C# event
will help.

If you don't need a cross-process solution, then you may want to start
here:
http://msdn.microsoft.com/en-us/library/8627sbea.aspx [event (C#
Reference)]

Be sure to visit this page too:
http://msdn.microsoft.com/en-us/library/awbftdfh.aspx [Events (C#
Programming Guide)]

It contains lots of links to additional useful articles on the topic.

Pete
Jun 27 '08 #4
Maybe I'm misunderstandin g you, but it sounds like using an interface would
be the easiest solution. Require all your forms to implement an interface
with that method defined; then you can loop through all the forms and call
that method on each.

"Jeff" <jeff@[_nospam_].hardsoft.com.a uwrote in message
news:tN******** *************** *******@posted. internode...
>I need a way to do the following and cannot seem to find a solution via
google.

1. Have a method from the main app to get all open forms
2. Check each open form for a public method
3. If this public method exists execute it which will result in the
displayed form being updated. There will be no data loss as the forms
will only contain listviews or readonly fields.

Regards
Jeff
Jun 27 '08 #5
1. Application.Ope nForms will give you all the open forms.
2. Checking for a method could use reflection, but if this is code
internally developed by you, you should be able to cast to the appropriate
form type and know that the method exists.
3. (i wouldn't separate 2 and 3.)

"Jeff" wrote:
I need a way to do the following and cannot seem to find a solution via
google.

1. Have a method from the main app to get all open forms
2. Check each open form for a public method
3. If this public method exists execute it which will result in the
displayed form being updated. There will be no data loss as the forms
will only contain listviews or readonly fields.

Regards
Jeff
Jun 27 '08 #6
On Sat, 10 May 2008 10:16:18 -0700, Nathan
<ms********@mac gregorfamily.ne twrote:
Maybe I'm misunderstandin g you, but it sounds like using an interface
would be the easiest solution. Require all your forms to implement an
interface with that method defined; then you can loop through all the
forms and call that method on each.
This is a fine approach as far as it goes. It's certainly a lot closer to
what the OP specifically asked for, and assuming he has the ability to
modify the form classes, is much better than using reflection.

That said, it's a bit "Java-like", especially with respect to defining an
interface with only one method just so you can call that one method. Not
that there's anything technially wrong with being "Java-like" :). Just
that in .NET, with delegates and events, it's more common to approach this
sort of problem using events.

Even .NET has a handful of interfaces that have just one method. It's not
that it's necessarily bad design to do so. But for me, interfaces are
more about abstraction than about conditional processing. With the "is"
or "as" operator, an interface implementation would definitely work fine,
but if one is going to have to change the original class anyway, an event
is IMHO more in line with the usual .NET paradigms than defining an
interface that has only one method. It also avoids the need to enumerate
all of the open forms, instead taking advantage of the multicast nature of
an event to directly retrieve the group of form instances that is exactly
those that will response to the specific operation.

That said, I think that which is preferable may really come down to what
this operation actually is. Since we don't have that detail, only the OP
can determine the answer to that. If it's a case of an operation that is
best thought of as "these forms implement a certain kind of behavior that
needs to be used at some point in time", then I think the interface
approach lends itself very nicely to that. On the other hand, if it's a
case of an operation that is best thought of as "this particular thing
happens at a particular point in time and these forms need to be informed
about that", then an event is probably paradigmaticall y more appropriate.

Pete
Jun 27 '08 #7
Jeff wrote:
I need a way to do the following and cannot seem to find a solution via
google.

1. Have a method from the main app to get all open forms
2. Check each open form for a public method
3. If this public method exists execute it which will result in the
displayed form being updated. There will be no data loss as the forms
will only contain listviews or readonly fields.
This last part is interesting. Are all these forms using a common data source?
If not, could they be?

It reads like you've got the same data in several places, and it would make more
sense to have a datasource that itself gets updated, and then the forms can
handle dealing with the updated datasource on their own.

Chris.
Jun 27 '08 #8

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

Similar topics

2
11863
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an error message and stop the process every time someone loads the script after the table is created because that would mean the page could only ever run once which of course not the solution I was looking for. I simply want to know how I can check...
2
6038
by: Mike | last post by:
I´ve got a number of SPAN elements named "mySpan1", "mySpan2", "mySpan3" etc, and want to set their "style.display" to "inline". This works (only needs to work on IE5.5+): for (var x = 1; x < 20; x++) { document.all('mySpan'+x).style.display = "inline"; } But I don´t know how many SPAN elements there are, so I need to set x to a
3
2122
by: Caspy | last post by:
I just get stuck on how to check if a user is a member of network (domain). I am building an internal tracking system with ASP.Net with Form authentication. When an user is added into the system, it check if the user is a member of the domain account against Global Catalog. If not, the user is not allowed to added in. If is, get the user's first name and last name and insert into the database. Because the system need access to other...
1
4240
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get the example to work. I created the following two classes, provided with the example: *-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxColumn Class:-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-**-*-*-*
1
7763
by: aaa | last post by:
What is the most efficient way to do a check to see if a webservice exists before attempting to execute. I was thinking of just creating an HTTP object then initiating a request to see if i get a response back from the server. Is this too much overhead, and how would you imlement this?
5
3116
by: rn5a | last post by:
The .NET 2.0 documentation states the following: When using a DataSet or DataTable in conjunction with a DataAdapter & a relational data source, use the Delete method of the DataRow to remove the row. The Delete method marks the row as Deleted in the DataSet or DataTable but does not remove it. Instead when the DataAdapter encounters a row marked as Deleted, it executes its DeleteCommand method to delete the row at the data source. The...
16
2611
by: king kikapu | last post by:
Hi to all, in statically-types languages, let's say C# for example, we use polymorphism through interfaces. So we define an interface I with method M and then a class C that implements I interface and write code for the M method. So, if we have a function that takes a parameter of type I, we know before-hand that it will have an M method to call. But in dynamic languages this is not the case and we can pass whatever
82
10076
by: happyse27 | last post by:
Hi All, I modified the user registration script, but not sure how to make it check for each variable in terms of preventing junk registration and invalid characters? Two codes below : a) html b) perl script (print and inserting into database) Cheers... Andrew
2
14667
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how can I pass null value as parameter to the database stored procedure programattically using C#? Although I can check for empty column (the following code passes string.Empty as parameter but how to pass null value?), I cannot check for null value...
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10408
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...
0
10141
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...
0
9225
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7686
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
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5707
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3886
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3030
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.