|
hi there
i saw a lot of Example and Article for Delegate and Events
i need to know where to use delegate and events
let's say for example
user need 2 insert in args 3 parameter
//static void Main(string[] args)
like add 2 3 //program will output 5
or sub 3 2 //program will output 5
can i use delegate here ???
without ask if add or sub
can somebody give me example from life?
thanks in advance
|
|
|
|
|
To start with -
A Delegate is a pointer to a function
An Event is a collection of delegates.
<br />
<br />
public void Run()<br />
{<br />
int i = Add(a,b);<br />
Console.Write(i);<br />
}<br />
<br />
public int Add(int a, int b)<br />
{<br />
return a + b;<br />
}<br />
<br />
will do exactly the same as
<br />
delegate void Adder(int a, int b);<br />
<br />
public void Run()<br />
{<br />
Adder a = new Adder(Add);<br />
<br />
int i = a(a,b);<br />
<br />
Console.Write(i);<br />
}<br />
<br />
public int Add(int a, int b)<br />
{<br />
return a + b;<br />
}<br />
<br />
For more information on events, create a windows form application and do a tutorial.
Hope that helps
Tris
|
|
|
|
|
hi tris
thanks for the answer
i know how to use delegate and events
all the article explain how to use delegate and events
but not when or where to use it
i found some example for delegate
let's say if we need to sort some array once Ascending
and once descending without change the function BubbleSorter
we use delegate
delegate bool CompareOp(object lhs, object rhs);
class MainEntryPoint
{
static void Main()
{
Employee [] employees =
{
new Employee("Bugs Bunny", 20000),
new Employee("Elmer Fudd", 10000),
new Employee("Daffy Duck", 25000),
new Employee("Wiley Coyote", (decimal)1000000.38),
new Employee("Foghorn Leghorn", 23000),
new Employee("RoadRunner'", 50000)};
CompareOp employeeCompareOp = new CompareOp(Employee.RhsIsGreater);
BubbleSorter.Sort(employees, employeeCompareOp);
PrintEmployee(employees);
CompareOp employeeCompareOp1 = new CompareOp(Employee.RhsIsSmall);
BubbleSorter.Sort(employees, employeeCompareOp1);
PrintEmployee(employees);
Console.ReadLine();
}
private static void PrintEmployee(Employee[] employees)
{
for (int i=0 ; i<employees.length ;="" i++)
="" console.writeline(employees[i].tostring());
="" }
="" }
="" class="" employee="" :="" object
="" {
="" private="" string="" name;
="" decimal="" salary;
="" public="" employee(string="" name,="" salary)
="" this.name="name;
" this.salary="salary;
" override="" tostring()
="" return="" string.format(name="" +="" ",="" {0:c}",="" salary);
="" static="" bool="" rhsisgreater(object="" lhs,="" object="" rhs)
="" emplhs="(Employee)" lhs;
="" emprhs="(Employee)" rhs;
="" (emprhs.salary=""> empLhs.salary) ? true : false;
}
public static bool RhsIsSmall(object lhs, object rhs)
{
Employee empLhs = (Employee) lhs;
Employee empRhs = (Employee) rhs;
return (empRhs.salary < empLhs.salary) ? true : false;
}
}
class BubbleSorter
{
static public void Sort(object [] sortArray, CompareOp gtMethod)
{
for (int i=0 ; i
|
|
|
|
|
hi again
sory for long code before
events
scenario for events
driving in the race car
need to alert driver
when the fuel gone finish
with red lamp on display
how my program ready for this scenario??
how can i fire event for this
thanks in advance
roni
|
|
|
|
|
Hello,
I have HtmlElementCollection object. Althought it is a collection, it does not have the Sort() method. How can I sort that collection using IComparer interface? Same goes for FileInfo[] object, it does not have Sort() method, and I need to sort that as well.
Thank you.
|
|
|
|
|
Don't have a solution for the collection problem, but to sort an array you can use an overload of the static Array.Sort method.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
See this[^] article.
/ravi
|
|
|
|
|
Hello
I'm creating an application which use the Media.Soundplayer function.
Currently i'm using a Media.Soundplayer funtions that Play when i press on different buttons, it works perfectly.
I tried to create a new Media.SoundPlayer and load a file which should be looping while the application is running, but when i use the Media.Soundplayer it stop the previous file and start play the new one.
So can anyone tell me how to play a file without it stop play unless i tell it to stop?
This is how i load the files:
private System.Media.SoundPlayer TrykPaaKnap = new SoundPlayer(Geografika.Properties.Resources.tryk);
private System.Media.SoundPlayer BGMusik = new SoundPlayer(Geografika.Properties.Resources.Fjederdansen);
Thanks
|
|
|
|
|
Hi
I have been working on a windows application. Now I want to Internationalize the application. I want to add a new languages (Chienese). For this purpose I have created the resource file.
My question is how do I type the chinese words in the resource file. I have tried and failed to either copy or write anything the resource file(in chinese) in Visual Studio IDE.
If any one knows about it please tell me
Thanks
Avik
Avik
|
|
|
|
|
Perhaps it is due to the unusual Chinese characters? The string resource editor will xml-encode things such as < but may be not handle Chinese characters. You can open a string resource file in Notepad and enter them that way, but you may need to xml-encode the following characters
"
&
<
>
and change all apostrophes to ' before pasting them in.
Sorry if this doesn't help you.
|
|
|
|
|
Hi there,
I have a problem which I expect to solve here.. Here is what I want to do :
I have got 5 sub panels and 1 main panel (all of them containing various controls) .. I want to do simple drag&drop operations on them. To be more clear I will drag the main panel over one of the sub panels.
Now, where I stuck is that when I drop the main panel anywhere in the Form a method should check wether there is a sub panel in the region of the main panel. If yes the main panel will transfer its controls to that sub panel. If no the main panel will return to its original position.
But also, in the region there may be two panels. In this case the main panel will transfer its contents to the closest one.
How can I do such region checks and also how can I compute the closest one ?
Thanks for your helps and best regards...
.:: Something is Wrong ::.
|
|
|
|
|
Hey guys,
My application performs a backup of some files, add them into a single archive and then uploads them to a one click hoster such as Yousendit.com or sendspace.com. If you open the main page of YouSendIt.com you will a browse file button similar to the ones found on other webpages. I'm am able to upload using the website but i wanted to do so from my application. How do i do this? The website doesnt require any authentication.
|
|
|
|
|
Have you looked at the response a file browse sends? You'll probably have to POST a response. If you're using the .Net Web name space you should be able to use the WebClient.Upload file method.
You may need to set various other fields as well before uploading the file.
- Eitsop
What we do not understand we do not possess. - Goethe.
|
|
|
|
|
I'm really new to we stuff using .net so it'd be a great help if you could gimme an example. i couldn't find much resources about this.
|
|
|
|
|
Hi!
Basically i want to serialize an array of objects Booking.
That class has public fields of basic types like string,int.
A very simple class containg information of the booking.
I have managed to serialize/deserilize the array before but when
i added a public field object of type 'MyButton' which inherits the System.Windows.Forms.Button class i got Reflection error of type Booking[] ?
I tried using an [XmlIgnore] on the object not to serializr this but it doesn't work.
Booking class:
<br />
<br />
string name;<br />
...;<br />
...;<br />
<br />
public Booking(string name,...,...)<br />
{<br />
this.name = name;<br />
...;<br />
...;<br />
<br />
myButton = new MyButton();<br />
}<br />
<br />
...;<br />
<br />
I have a booking manager class that takes care of every booking and also instantiate the xmlSerializer object with the following line
<br />
xmlSer = new XmlSerializer(typeof(Booking[]));<br />
As I said, it worked fine until I added the MyButton object to the Booking class!
Why can't I add an object to my class with the property of XmlIgnore on it and serialize that object ?
Can't get a grip of what reflection error really means too!
Thankful for suggestion on how to solve this. I want to use this XmlSerializer cause it makes the job a lot easier but if i cant workaround this problem I may have to build an xmldocument by hand and save it and i try to avoid it.
|
|
|
|
|
When you say XmlIgnore, do you mean that you have applied the XmlIgnoreAttribute? i.e.
[XmlIgnoreAttribute]
public string Comment;
- Eitsop
What we do not understand we do not possess. - Goethe.
|
|
|
|
|
What I have on my "public MyButton myBut;" is an
[XmlIgnore]
Could it be that the XmlSerializer have trouble serializing objects like Button,Label classes because they have to do something with Windows?
Or perhaps that my object class is inheriting the Button object?
From one confused guy..
|
|
|
|
|
I have tried out this code. This ignores the button altogether and seems to work just fine:
using System;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;
namespace SerialTest
{
public class Group
{
[XmlIgnoreAttribute]
public System.Windows.Forms.Button myButton;
public string GroupName;
public string Comment;
public void SerializeObject(string filename)
{
XmlSerializer xSer = new XmlSerializer(typeof(Group));
TextWriter writer = new StreamWriter(filename);
xSer.Serialize(writer, this);
writer.Close();
}
}
}
To run this the example I used was a form with a button and the following code on the OnClick event:
Group a = new Group();
a.Comment="A sample comment";
a.GroupName="A sample group name";
a.myButton = this.button1;
a.SerializeObject("c:\\test.xml");
That worked.
- Eitsop
What we do not understand we do not possess. - Goethe.
|
|
|
|
|
Yeah this works I know.
I think(hope i did) i mentioned it that mybutton is an object of
<br />
class MyButton:System.Windows.Forms.Button <br />
What you have created is a "standard" button and i have a custom button that inherits System.Windows.Forms.Button.
So i wonder if there is any problems with inherited classes using XmlSerializer ?
|
|
|
|
|
Eitsop wrote: When you say XmlIgnore, do you mean that you have applied the XmlIgnoreAttribute? i.e.
[XmlIgnoreAttribute]
public string Comment;
Indeed you are right
rakhi2020[.]
|
|
|
|
|
Well, I do not immediately see any problem but why is that button declared as public anyway?
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
|
|
|
|
|
Is there any way of eliminating the need of having the Dlls my program is dependent of, in the same folder the executable is? I mean, I have a program that uses some Dlls, but I don't like the need of having to copy them along with the program, if I need to move it. Isn't there any way of embedding the Dlls as a resource or something like that?
Thanks in advice (and sorry if my English is too bad.. )
|
|
|
|
|
Hi
You can register your dlls in Global Assembly Cache (GAC)But if you want to deploy your program you should have a setup program that register your assmeblies in GAC.
Regards
Nima H
|
|
|
|
|
I have created a cliecnt server application using .Net Remoting.
How can i check whether a connection is established or not.
presently, i am checking it by catching the ServerException thrown whenever i tries to access my remotable class. Its a time consuming process.
is there any API through which i can check this like we have in socket programmming.
Thanks in advance
Praveen Raghuvanshi
Software Engineer
|
|
|
|
|
I need a simple example of a Stack in C#.
|
|
|
|