|
Greetings again!
I have a question concearning delegates:
How to use a delegate which is passed by another class.
Here the exemple code, which shoould explain my question:
<br />
delegate void dSendParameters(string para);<br />
and in the Method
<br />
dSendParameters dSend = delegate(string para) { };<br />
Classes.OtherClass otherclass = new Classes.OtherClass(dSend);<br />
and her the code in the other class, which should "catch" the delegate
<br />
public class OtherClass<br />
{<br />
public OtherClass(Delegate dSend)<br />
{<br />
}<br />
<br />
private void buttonClickme_Click(object sender, EventArgs e)<br />
{<br />
}<br />
}<br />
With Parameters it´s easy, just pass a ref and create a Parameter which get´s the value of the ref Parameter. But how to make a delegate available this way??
I need this, because I need to make referenced Assemblies able to call Methods of the
executing Assembly.
Thanks in advance,
ezazazel
|
|
|
|
|
Your code can benefit from a better approach at the problem but I will give you a simple solution
public class Foo{
public delegate void dSendParameters(string para);
public event SendParameters;
protected virtual OnSendParameters(parm list){
if(SendParameters != null){
SendParameters(someString);
}
}
private void buttonClickMe_Click(object sender, EventArgs e){
OnSendParameters(someparams);
}
}
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
|
|
|
|
|
Thx for your sample code.
One more question if I may:
how to make the delegate from the exe to the delegate in your foo class
<br />
public delegate void dSendParameters(string para); <br />
public event EventHandler SendParameters;<br />
public OtherClass(Delegate dSend)<br />
{<br />
<br />
}<br />
Or did I missunderstand your code? If so, I´m sorry.
Thx in advance
-- modified at 16:00 Thursday 24th August, 2006
|
|
|
|
|
This maybe? I don't really understand the question.
public class SomeOtherCode{
public void SampleMethod(){
Foo foo = new Foo();
foo.SendParameters += new dSendParameters(SomeMethod);
}
public void SomeMethod(string param){
...
}
}
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
|
|
|
|
|
I mean this:
public delegate void dSendParameters(string para);
public event SendParameters;
public OtherClass(Delegate dSend)
{
//this.SendParameters += dSend;
//or
//dSendParameters += dSend
}
which doesnt´t work
What I want is:
Assembly a (executable), Assembly b(dll)
b is referenced in a
a has a method which needs to be called by b
so I send a delegate to be when I call the class
the sent delegate is invoked inside b
Thx
|
|
|
|
|
public delegate double DFunction(double d);
public double Sin(double d){
return Math.Sin(d);
}
public double PerformAction(DFunction d){
double e = d(5d);
return e;
}
private void button1_Click(object sender, EventArgs e) {
PerformAction(new DFunction(Sin));
}
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
|
|
|
|
|
You can hold the delegate as a field like any other object:
public class OtherClass {
private dSendParameters _dSend;
public OtherClass(dSendParameters dSend) {
_dSend = dSend;
}
public void SomeOtherMethodCallingDSend() {
_dSend("SomeParam");
}
}
|
|
|
|
|
public class OtherClass <br />
{<br />
Delegate _dSend;<br />
public OtherClass(Delegate dSend)<br />
{<br />
_dSend = dSend; <br />
}
works!!
Thanks Robert for your suggestion. Can now send and catch delegates in every direction.
|
|
|
|
|
What I want to do is say
"Only Add classes that implement interfaceA or interfaceB"
so saying
public List<T> where T: interfaceA, interfaceB {}
but that says they must implement both interface A and B.
How can I say A or B?
--------------------------------------------------------
1 line of code equals many bugs. So don't write any!!
My mad coder blog
|
|
|
|
|
You can't. Probably the best option would be to use a factory method or a singleton, with a derived class for each interface. This is the same method used by the Comparer<T>.Default property.
For example:
public abstract class MyClass<T>
{
private static readonly MyClass<T> _instance = CreateInstance();
public static MyClass<T> Instance
{
get { return _instance; }
}
private static MyClass<T> CreateInstance()
{
Type itemType = typeof(T);
Type resultType = null;
if (typeof(InterfaceA).IsAssignableFrom(itemType))
{
resultType = typeof(<span>MyClassA<></span>).MakeGenericType(itemType);
}
else if (typeof(InterfaceB).IsAssignableFrom(itemType))
{
resultType = typeof(<span>MyClassB<></span>).MakeGenericType(itemType);
}
if (null == resultType) throw new ArgumentException("Type parameter \"T\" must implement InterfaceA or InterfaceB.");
return (<span>MyClass<T></span>)Activator.CreateInstance(resultType);
}
}
internal sealed class MyClassA<T> : MyClass<T> where T : InterfaceA
{
...
}
internal sealed class MyClassB<T> : MyClass<T> where T : InterfaceB
{
...
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
|
That both A and B implement then you can check for it instead.
public interface IShape;
public interface IEvenPolygon : IShape;
public interface IOddPolygon : IShape;
public List<t> where T: IShape;
The problem is what you want isn't type safe.
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
|
|
|
|
|
Do you have the source code to interfaceA and interfaceB? If so:
interface IAmDaddy
{
}
interface interfaceA : IAmDaddy
{
}
interface interfaceB : IAmDaddy
{
}
public List<T> where T : IAmDaddy
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Dumbest. Movie. Title. Evaaar.
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|
Hi
could any one show me the best way to extract the info i need from the file below. Basicly the files are hand historys from a poker site and i would like to keep track of how often a player calls checks folds and so on. The file is in html (hoping it posts ok) , or if any one could tell me if it would be better to convert the file to xml (if possible) and extract the data that way?
well here is a short part of the file .....
Real Money Ring Game | Table Name | Hand ID | Game | Stakes | Casanova | 10474702-12327 | Holdem Limit | $0.25/$0.50 | [Jul 31 16:03:20] : Hand Start. | [Jul 31 16:03:20] : Seat 1 : davebass has $31.99 | [Jul 31 16:03:20] : Seat 2 : PoKaBoT has $15.21 | [Jul 31 16:03:20] : Seat 4 : WC2006 has $9.82 | [Jul 31 16:03:20] : Seat 6 : xbambamx has $8.02 | [Jul 31 16:03:20] : Seat 8 : drurylane has $23.05 | [Jul 31 16:03:20] : Seat 9 : modeselect has $3.34 | [Jul 31 16:03:20] : xbambamx is the dealer. | [Jul 31 16:03:21] : drurylane posted small blind. | [Jul 31 16:03:21] : modeselect posted big blind. | [Jul 31 16:03:21] : Game [12327] started with 6 players. | [Jul 31 16:03:21] : Dealing Hole Cards. | [Jul 31 16:03:21] : Seat 2 : PoKaBoT has 5s Ts | [Jul 31 16:03:25] : davebass folded. | [Jul 31 16:03:26] : PoKaBoT folded. | [Jul 31 16:03:29] : WC2006 called $0.25 | [Jul 31 16:03:32] : xbambamx folded. | [Jul 31 16:03:34] : drurylane called $0.13 | [Jul 31 16:03:35] : modeselect checked. | [Jul 31 16:03:36] : Dealing flop. | [Jul 31 16:03:36] : Board cards [As Kc 4d] | [Jul 31 16:03:38] : drurylane checked. | [Jul 31 16:03:38] : modeselect checked. | [Jul 31 16:03:42] : WC2006 checked. | [Jul 31 16:03:42] : Dealing turn. | [Jul 31 16:03:42] : Board cards [As Kc 4d 8s] | [Jul 31 16:03:45] : drurylane bet $0.50 | [Jul 31 16:03:45] : modeselect folded. | [Jul 31 16:03:52] : WC2006 called $0.50 and raised $0.50 | [Jul 31 16:03:53] : drurylane called $0.50 | [Jul 31 16:03:53] : Dealing river. | [Jul 31 16:03:53] : Board cards [As Kc 4d 8s Js] | [Jul 31 16:03:56] : drurylane checked. | [Jul 31 16:03:57] : WC2006 bet $0.50 | [Jul 31 16:03:58] : drurylane called $0.50 | [Jul 31 16:03:58] : Showdown! | [Jul 31 16:04:00] : Seat 4 : WC2006 has 8d Ac | [Jul 31 16:04:00] : WC2006 has Two Pair: Aces and 8s | [Jul 31 16:04:01] : WC2006 wins $3.57 with Two Pair: Aces and 8s | [Jul 31 16:04:10] : Hand is over. |
Table of contents |
if i havent been very clear or this file has posted terribly and you would like to help i could always email a file to you to take a look @ ... many thanx
The top part of the file which looks extreemly messy in the post is not all that important its just the hand numbers , the main part is where you can see "Hand Start" .
|
|
|
|
|
Have you tried opening this up with an XML parser? It looks well-formed enough that you could do that. In this case, you could use an XmlDocument object and start grabbing data out of it.
Logifusion[^]
If not entertaining, write your Congressman.
|
|
|
|
|
Hi,
Use Regex to search for specific patterns.
<br />
"(\\w+)\\s*=\\s*"<br />
"\"\\s*(.*?)\\s*\""
These will match all the attributes in the html like name = "value"
Try forming the proper regex for your situation.
<marquee>"A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder
Anant Y. Kulkarni
|
|
|
|
|
Hi
When I'm trying to Add something to my ListvView (multiple columns), I receive this strange error in debug mode:
A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
The code (*):
lstProps.Items.Add(new ListViewItem(new string[] {"one", "two" }));
Which resides in private void receivedIt(RemovableStorage r, EventArgs e) .
The strange thing is that the same code (*) does work in the Form_Load method, or some Botton_Click method! Am I doing something totally wrong?
I am searching for 2 days now and all the online samples are doing the same - and I'm getting a bit of dissapointed in the ListView control Hopefully someone can help me?
Thanks a lot!
|
|
|
|
|
You are probably trying to access the ListView control from a non-UI thread. You can use the InvokeRequired property to determine whether you are on the UI thread, and the Invoke method to transfer execution to the UI thread.
For example:
private delegate void ReceivedItDelegate(RemovableStorage e, EventArgs e);
private void receivedIt(RemovableStorage r, EventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(
new ReceivedItDelegate(receivedIt),
new object[2] { r, e });
}
else
{
lstProps.Items.Add(...);
...
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
|
Hi Richard
Indeed! It works! Jiha!
Thanks a lot!
Something I never should found on my own!
Greetings
|
|
|
|
|
Hi there:
As you know we can compile a C Sharp program at DOS prompt like
csc MyProgram.cs
Where MyProgram.cs is the source code file.
Now i want to perform the same compilation through another C Sharp Program. Like i made a program named ShellExecution, which tries to compile another C Sharp Source Code File called "First.cs" lying in my c directory. The program runs fine without any exceptions; it opens the dos window and runs csc, but nothing happens and seems like the file name "First.cs" is not being passed to the csc.exe as a parameter. I have used the following code for executing the compilation process:
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\CSC.exe";
process.StartInfo.Arguments = "C:\\First.cs";
process.StartInfo.UseShellExecute = true;
process.StartInfo.RedirectStandardOutput = false;
process.StartInfo.CreateNoWindow = false;
process.Start();
process.WaitForExit();
Please help me out!!!!
|
|
|
|
|
Check out the CSharpCodeProvider. It can compile from a CodeDom model, from files, or from strings.
Link[^]
Logifusion[^]
If not entertaining, write your Congressman.
|
|
|
|
|
i'm working with MenuItem that i attached to a notifyIconTray... i want 2 add to on of the Menuitem meber new members "on the fly" from combobox, so when i add 2 combobox, i want it 2 be added to the member in the menuitem.the problem is .. that it works only once,,, after i maximize and minimize it back, it wont show me the other members of the Member of the menuitem....
any ideas y it happens ?
it looks like that:
private void Form1_Resize(object sender, System.EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
{
menuItemEntireCat.MenuItems.Clear();
MenuItem[] CatMenuItems= new MenuItem[catcombobox2.Items.Count];
for(int i=0;i
|
|
|
|
|
It seems like you have some error here :
vedmack wrote: MenuItem[] CatMenuItems= new MenuItem[catcombobox2.Items.Count];
for(int i=0;i{
CatMenuItems[i]=new MenuItem();
And why would you do somethinglike this in the resize event ?
------------------------------ "The Soapbox has been so ..."
|
|
|
|
|
ok solved it....
altho its nothing 2 do with the resize event...
its just i need 2 re-add it to the root menuitem...
|
|
|
|
|
Hi, I need to work with windows system process(e.g closing IE explorer). so how shall I go about doing it.
|
|
|
|
|