473,779 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pass parameters through delegate?

Hi at all,
Is possible to pass a parameter though a delegate or to override it? (I'm
newbie and I'm trying to understand delegates and their use in a real
scenario)

In my scenario I need to override
System.Text.Reg ularExpression. MatchEvaluator delegate passing it another
parameter.
For a concrete sample I paste some lines of code :
//my regexpression pattern;
Regex regexer= new Regex("myBeatif ulPattern", RegexOptions.Mu ltiline);

//Class MatchEvaluator can be created with a delegate to a custom replacing
function
MatchEvaluator myEvaluator = new MatchEvaluator( CommentMatchHan dler);

//this replace the occurrences based on regexer patterns
string result = regexer.Replace (code, myEvaluator);

public string MatchHandler(Ma tch match)

{
//DO SOMETHING on MYVALUE
return MYVALUE
}

Well,
in the above sample I'd like to have "MatchHandl er" function like :
public string MatchHandler(Ma tch match, string MYPARAMETER)

but is possible? :)
If I declare the function with the new parameter the code wouldn't compile
(and it's in right... it cannot take parameters in new
MatchEvaluator( CommentMatchHan dler);
Thanks,
Bob
Aug 3 '07 #1
3 7186
Bob Speaking wrote:
[...]
Well,
in the above sample I'd like to have "MatchHandl er" function like :
public string MatchHandler(Ma tch match, string MYPARAMETER)

but is possible? :)
If I declare the function with the new parameter the code wouldn't compile
(and it's in right... it cannot take parameters in new
MatchEvaluator( CommentMatchHan dler);
You are correct, you can't do it in exactly the way you want.

However, there are a couple of alternatives you can use to have a
delegate have access to data that you would otherwise pass as a parameter.

One is to take advantage of the fact that a delegate includes an
instance reference, if the delegate is an instance method. So you can
create a class that holds the data you want, along with a delegate
method in that class that you use for the delegate, and when called the
delegate will have access to that data.

For example:

class DelegateWrapper
{
private string _strParm;

public DelegateWrapper (string strParm)
{
_strParm = strParm;
}

public string MatchHandler(Ma tch match)
{
// do something that uses _strParm
// return whatever
}
}

Where you create an instance of DelegateWrapper and then pass the
MatchHandler of the instance as your delegate.

Another is to use anonymous delegates, which can be placed inline where
you initialize the delegate, allowing the use of whatever data is
visible in that code.

For example:

string strParm;
MatchEvaluator myEvaluator = delegate(Match match)
{
// do something that uses strParm;
// return whatever
};

Making sure, of course, that at some point before the delegate is
called, the strParm variable is initialized to whatever you want.

These are not the only ways to do what you want, but they are IMHO a
couple of the simpler approaches available.

Pete
Aug 3 '07 #2
Hello Bob,
Hi at all,
Is possible to pass a parameter though a delegate or to override it?
(I'm
newbie and I'm trying to understand delegates and their use in a real
scenario)
In my scenario I need to override
System.Text.Reg ularExpression. MatchEvaluator delegate passing it
another
parameter.
For a concrete sample I paste some lines of code :
//my regexpression pattern;
Regex regexer= new Regex("myBeatif ulPattern", RegexOptions.Mu ltiline);
//Class MatchEvaluator can be created with a delegate to a custom
replacing
function
MatchEvaluator myEvaluator = new MatchEvaluator( CommentMatchHan dler);
//this replace the occurrences based on regexer patterns string result
= regexer.Replace (code, myEvaluator);

public string MatchHandler(Ma tch match)

{
//DO SOMETHING on MYVALUE
return MYVALUE
}
Well,
in the above sample I'd like to have "MatchHandl er" function like :
public string MatchHandler(Ma tch match, string MYPARAMETER)
but is possible? :)
If I declare the function with the new parameter the code wouldn't
compile
(and it's in right... it cannot take parameters in new
MatchEvaluator( CommentMatchHan dler);
Thanks,
Bob
Not directly. But you could do this:

public class MatchEvaluatorW ithParameter
{
string _parameter;
public MatchEvaluatorW ithParameter(st ring parameter)
{
_parameter = parameter;
}

public string MatchEvaluator( Match match)
{
// use _parameter for your string parameter
// use match to get the info
}
}
Now from the code executing the regex:

Regex regexer= new Regex("myBeatif ulPattern", RegexOptions.Mu ltiline);
MatchEvaluatorW ithParameter mwp = new MatchEvaluatorW ithParameter("y our param
here");
MatchEvaluator myEvaluator = mwp.MatchEvalua tor;
//this replace the occurrences based on regexer patterns string result
regexer.Replace (code, myEvaluator);

Jesse
Aug 4 '07 #3
Thanks guys :)
Your suggestions and methods fit to my needs and help me understand delegate
etc!

Thank you,
Bob
Aug 4 '07 #4

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

Similar topics

9
1476
by: Wiktor Zychla | last post by:
Hello, I wonder why the delegate declaration needs named parameters? public delegate void MyDelegate( int a, int b ); // ok public delegate void MyDelegate( int, int ); // compiler error C allows to define both: typedef void (*MyDelegate)(int); // ok
4
21760
by: yoramo | last post by:
hello can I pass a static method as a parameter to a method? if the answer is yes how do I do that ? how do I call the method ? yoramo.
6
2920
by: VM | last post by:
I'm trying to add multithreading to my win application but I'm having trouble with the code since the method to be threaded has parameters. How can I add multithreading to a method with parameters? MSDN says ThreadStart cannot take parameters, so how can I do it? public System.Threading.Thread Thread_LoadAZM; public void LoadFile(string sFileName) { Thread_LoadAZM=System.Threading.Thread(new...
1
1800
by: VM | last post by:
How can I pass a delegate to another method? In my code (win app), i update my datagrid with the datatable after the method loadAuditFileToTable is finished executing. Instead, I'd like to be able to to update the grid continuously (while the table's filling) and someone suggested I pass a callback to loadAuditFileToTable and run it every X records. How can I do this? This is a summary of my code: loadAuditFileToTableDelegate...
8
4545
by: Phill | last post by:
All the event handlers seem to pass an Object and an EventArgs object. If the event doesn't need this info why pass them anyway? It is inefficient.
7
8485
by: SB | last post by:
What is the proper way to pass a character array (char *) from a "C" dll to a C# method (delegate) in my app? Getting the dll (which simulates a third party dll) to call my delegate works fine. However, as soon as I try to add any "char *" parameters, I start getting exceptions in my C# app. So far I've tried (among others): public delegate int Test(char version); and public delegate int Test(string version); // won't work because...
4
2430
by: KC Eric | last post by:
Hi all, I have a dll file, it has a class, say: class Temp, this class has a function which has a delegate as a parameter, say: public void Test(GameOverHandler _overHandler)
24
55237
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new EventHandler(Onbutton_click); I want to pass more information related that event. & want to use that
12
11111
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
9636
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
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10139
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
10075
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
8961
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
7485
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
5373
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
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
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

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.