473,408 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

method with method name as parameter

Hello,

as "exercise" (I needed it) I've built a simple logging/tracing classLibrary and I'm using it to write in a txt what my application is doing, and save the exceptions.

I'm not an expert programmer and I want to improve my library. Actually to trace something I have to do like this:

Expand|Select|Wrap|Line Numbers
  1. Logger log = new Logger();
  2. SomeMehod();
  3. log.Trace("I usually write a comment here, such as: SomeMehod()");
Now, I'd like to add time measurement in my log. I've found an interesting class to do that, the Stopwatch one (using System.Diagnostics).

I can do that as follows:
Expand|Select|Wrap|Line Numbers
  1. Logger log = new Logger();
  2. Stopwatch watch = new Stopwatch();
  3. watch.Start();
  4. SomeMethod();
  5. watch.Stop();
  6. log.Trace("SomeMehod() time: " + watch.Elapsed.ToString() );
  7.  
Now, I'd like to add a new method in my classLibrary, and call it let's say MeasureAndTrace(string methodToCall, string logMessage);

The tricky thing is the "methodToCall". I'm not even sure it's correct to use it as string. My idea is to use the method to launch a method and measure the time, as done before with the Stopwatch, as follows:

Expand|Select|Wrap|Line Numbers
  1. MeasureAndTrace("SomeMethod()", "SomeMethod launched");
And this is how the MeasureAndTrace would look like, in my library.

Expand|Select|Wrap|Line Numbers
  1. MeasureAndTrace(string methodToCall, string logMessage)
  2. {
  3. Logger log = new Logger();
  4. Stopwatch watch = new Stopwatch();
  5. watch.Start();
  6. // PLEASE LOOK HERE
  7. //Here I need the code to launch the methodToCall defined as parameter
  8. watch.Stop();
  9. log.Trace("SomeMehod() time: " + watch.Elapsed.ToString() );
  10. }
My question is, can you help me to correctly adapt my MeasureAndTrace method in my logging classLibrary and tell me what do add in the line "//Here I need the code to launch the methodToCall defined as parameter"?

Note: methodToCall could be something more complex, e.g. a method containing some parameters...

Another note: what happens if the method that I want to pass as methodToCall would return a value, would it be possible to still call it, e.g. like this (in case it returns a string)?

Expand|Select|Wrap|Line Numbers
  1. string resultString = MeasureAndTrace(string methodToCall, string logMessage);
I've already googled for: c# function name parameter and read some suggestions by people to use delegates, or reflection. I'm reading some tutorials about delegates but actually I can't understand how to use them in my practical example above mentioned. If you can give me some code, I would really appreciate.

Thanks a lot,

cheers,

--firepol
Mar 24 '09 #1
2 3047
tlhintoq
3,525 Expert 2GB
My two cents worth...
Don't do it with methods: Do it with events.
With a method you are re-directing program flow. Your working method does part of its job, then gets redirected to the log to write it down, then comes back to do more work.
If you raise an event for things such as "StartStopWatch" "StopStopWatch" "RecordLogEntry" you won't sidetrack your main working method. All it knows is to raise its hand and say "Go!" "Stop!" "Write".

Now you can have any number of classes subscribe to the events and react.
For example if you raise a "MethodStarted" event you can have a stopwatch class start timing, and a LogClass write it in a text file, and a ListBox add it as an on-screen notification. Later when you update the application you take out the listbox, but you don't have to re-write all the other methods because its gone.
Mar 24 '09 #2
tlhintoq: thanks for your 2 cents... Well I spent a few hours reading about delegates and events... but I don't see, in practice, how I can use them in my example explained in the post :-( or... it seems very complicated.

If I've understood your post you suggest me to create a MethodStarted event. Seems a good idea, but then... should I create also a MethodStopped event?

I will check again some events tutorials and see if I can build something and get back here with some fresh code. In the meanwhile if you have more szggestions you are welcome,

cheers
Mar 25 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Lenard Lindstrom | last post by:
I was wondering if anyone has suggested having Python determine a method's kind from its first parameter. 'self' is a de facto reserved word; 'cls' is a good indicator of a class method ( __new__...
2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
1
by: Dotnet Gruven | last post by:
I've posted this in the adonet group, however it was suggested I might have better luck here.... ============================================================= I'm trying to use a typed dataset and...
3
by: Russell Verdun | last post by:
From a vb.net application I'm want to execute an Oracle Stored Procedure via a web service. What would be the best way to approach this? I can't pass and Oracle Command or the parameter objects...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
9
by: Joel Finkel | last post by:
Is there a way to execute a method if all we know is its name as a string? Let's say we have the following class. What is the code for the Execute method? I need a solution that works with the...
0
by: Fred Dag | last post by:
Hi, I have a FormView that uses a ObjectDataSource. It retreives data alright but when updating it gives a ObjectDataSource 'ObjectDataSourceCatalogueItem' could not find a non-generic method...
2
by: Ismail | last post by:
Hello, I have grid view which can go into edit mode. I have fields one of which is primary key I dont want to display this field but will use this field in my update method. If I make the...
1
by: pekbob1 | last post by:
Hi Everybody I have the error (ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'UpdatePre' that has parameters: Comp, Job_Number, Request_Date, Customer, Contact_Name, Tel,...
0
by: Cirene | last post by:
Using Visual Studio I created a DataSet using the GUI (XSD file). Trying to use a tiered methodology I called the functions from my BLL. Namespace Zzz.BusinessLogicLayer #Region "DAL Access"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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...

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.