9 Replies - 12977 Views - Last Post: 10 April 2011 - 11:25 AM Rate Topic: -----

#1 mithunk01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 27-March 11

How To Invoke Delegates From Any C# Class

Posted 09 April 2011 - 02:48 AM

Dear Friends,
I have on class for Read and write Data from Com Port..Here I am using Delegate For Reading The Data from Com Port..Now i Used this Class as DLL..Now How i will Invoke or Call this method in another class..Here is My Code..Plz help me..Its Urgent.....
public delegate string SetTextDeleg(string text);
public void ConfigurePort()
        {
            port = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One);
            port.Handshake = Handshake.None;
            port.DataReceived +=new SerialDataReceivedEventHandler(sp_DataReceived);
            
            port.ReadTimeout = 500;
            port.WriteTimeout = 500;
            port.Open();
        }
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(500);
            string data = port.ReadExisting();
            this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
        }
        public void BeginInvoke(SetTextDeleg setTextDeleg, object[] p)
        {
           // throw new NotImplementedException();
        }
        public string si_DataReceived(string data)
        {
            string iPData = data.Trim();
            iPData = ConvertToHex(iPData);
            return data;
        }



Is This A Good Question/Topic? 0
  • +

Replies To: How To Invoke Delegates From Any C# Class

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,674
  • Joined: 02-June 10

Re: How To Invoke Delegates From Any C# Class

Posted 09 April 2011 - 06:40 AM

Before you bother trying to make this a DLL you probably want to do a lot of work making it more robust and able to recover from errors.

Line 14: Why on earth would you sleep the thread for a half second?!?!?!

Line 18: Please tell me that is not an attempt to make your DLL directly place the text that came in from the serial port into a textbox you are passing to the DLL as a parameter or reference.
Was This Post Helpful? 0
  • +
  • -

#3 mithunk01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 27-March 11

Re: How To Invoke Delegates From Any C# Class

Posted 09 April 2011 - 11:45 PM

View PosttlhIn`toq, on 09 April 2011 - 06:40 AM, said:

Before you bother trying to make this a DLL you probably want to do a lot of work making it more robust and able to recover from errors.

Line 14: Why on earth would you sleep the thread for a half second?!?!?!

Line 18: Please tell me that is not an attempt to make your DLL directly place the text that came in from the serial port into a textbox you are passing to the DLL as a parameter or reference.

Dear Sir,
Plz help me on this..its urgent..and i am new on this..I want to pass as parameter and also i want to use as DLL..this code i took from Internet..Plz guide me on this..
Thnks & Regards
Mithun
Was This Post Helpful? 0
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: How To Invoke Delegates From Any C# Class

Posted 10 April 2011 - 02:14 AM

Hi,

I would use the Func<string, string> delegate instead of your own custom delegate. It is defined in the System namespace, and so can be available to all of your projects. So, you can get rid of your delegate definition, change the first parameter type of the BeginInvoke to Func<string, string> and compile your DLL.

Then, open up another project, in add a reference to your DLL via the Solution Explorer window, add a Using statement to the top of your code for the namespace that your class is in, and you can them make use the DLL in that project, instantiate your class and call your methods, passing in a Func<string, string> delegate instance intead of your own custom delegate.

Why are you using a delegate here anyway?

The best advice I could give you is to get a book or do some tutorials and learn the language for yourself properly :).

This post has been edited by CodingSup3rnatur@l-360: 10 April 2011 - 02:54 AM

Was This Post Helpful? 0
  • +
  • -

#5 mithunk01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 27-March 11

Re: How To Invoke Delegates From Any C# Class

Posted 10 April 2011 - 02:54 AM

Thnks For reply sir..Can u send me one example..realy i am new in this..
thnks & regards
mithun

This post has been edited by JackOfAllTrades: 10 April 2011 - 11:25 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#6 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: How To Invoke Delegates From Any C# Class

Posted 10 April 2011 - 03:23 AM

There's nothing to show really...

Get rid of your delegate definition.

Change BeginInvoke()'s signature to this:

public void BeginInvoke(Func<string,string> setTextDeleg, object[] p)



Change the call to BeginInvoke to this:

this.BeginInvoke(new Func<string,string>(si_DataReceived), new object[] { data });




Creating and Using C# DLLs

Because Func<> is defined in the System namespace, not in your class, you easily can create an instance of it outside of your DLL, meaning you can easily pass in the delegate instance to methods in your DLL.

This post has been edited by CodingSup3rnatur@l-360: 10 April 2011 - 03:31 AM

Was This Post Helpful? 0
  • +
  • -

#7 mithunk01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 27-March 11

Re: How To Invoke Delegates From Any C# Class

Posted 10 April 2011 - 03:57 AM

I have these Function..how i will return a value..
port.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(500);
            string data = port.ReadExisting();
            this.BeginInvoke(new Func<string, string>(si_DataReceived), new object[] { data });
//I need data as return value..
        }
//I need this data as return value..if i am making //this funtion as return type then here:
//port.DataReceived += new //SerialDataReceivedEventHandler//(sp_DataReceived); i am getting error..
        public void BeginInvoke(Func<string, string> setTextDeleg, object[] p)
        {

        }
        public void si_DataReceived(string data)
        {
            string iPData = data.Trim();
            iPData = ConvertToHex(iPData);
        }  


THNNS for u reply..if u r felling boring..plz excuse me..
mithun

This post has been edited by JackOfAllTrades: 10 April 2011 - 11:24 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,674
  • Joined: 02-June 10

Re: How To Invoke Delegates From Any C# Class

Posted 10 April 2011 - 04:26 AM

Quote

plz thnx u r i m


Also, dis not b d'hood dawg... You are not here texting your high school posse to come to your kegger after their shift at Taco Bell. You are here asking for help from senior coding professionals who graciously donate their valuable time to helping the next generation of coders with their chosen craft. Please try to show them, yourself and the industry some respect by writing at least at an eighth grade level. (IE: English not ebonics or SMS, real words, punctuation and so on)

If you can't take your own problem/question seriously enough to write like an adult, then why would you expect anyone else to take it seriously?


Standard resources, references and suggestions for new programmers.

I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard experimentation - or you can learn by doing the tutorials and following a good "How to learn C#" book.

May I suggest picking up a basic C# introductory book? There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" type books at your local book seller or even public library.

C# resources. Start here
Intro to C# online tutorial then here...
C# control structures then here.
MSDN Beginner Developer video series
MSDN video on OOP principals, making classes, constructors, accessors and method overloading

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Passing values between forms/classes
Debugging tutorial
Working with environmental variables

Debugging tutorial
Debugging tips
Great debugging tips

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.
Reading a text file tutorial.


These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the 500+ MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.

Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)
  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on

Was This Post Helpful? 0
  • +
  • -

#9 mithunk01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 27-March 11

Re: How To Invoke Delegates From Any C# Class

Posted 10 April 2011 - 04:45 AM

thank you..

This post has been edited by JackOfAllTrades: 10 April 2011 - 11:24 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,437
  • Joined: 23-August 08

Re: How To Invoke Delegates From Any C# Class

Posted 10 April 2011 - 11:25 AM

Time for a lesson in forum etiquette: is NOT necessary to quote the entire post directly before yours, ESPECIALLY for a one-line response.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1