Live for Speed - LFS Main Site
- LFS World
- LFS News
- LFS Manual
- LFS Merchandise
  http://www.cap-online.com.ar/

Go Back   Live for Speed > Main > LFS Programmer Forum

Reply
 
Thread Tools Search this Thread Display Modes
Old 5th May 2012, 23:29   #1  -   
PoVo
S2 licensed
 
PoVo's Avatar
 
Racername: host
Join Date: Jun 2008
Location: Dublin, Ireland
Posts: 1,817
PoVo's online stats
.NET C# Callbacks

Hi,

I've recently been trying to get a callback system working for buttons in my project.

Anyway so currently I have a simple List<> which stores a class that should contain the button data and the callback.

The only problem is I'm not sure how to implement callbacks. I've been reading up on delegates and I can't come to a solution.

I basically want to be able to:
Code:
list[index].Callback(someMethodInSomeClass);
Basically the same way as it's done in InSimDotNet with packet bindings.

I've read through the InSimDotNet source and I still can't figure it out...

Any ideas?
Reply With Quote
Old 6th May 2012, 15:40   #2  -   
DarkTimes
S2 licensed
 
Racername: Faraday
Join Date: May 2004
Location: Glasgow, Scotland
Posts: 2,353
DarkTimes's online stats
You can store a callback using the .NET Action delegate type.

Code:
void SomeMethod() { }

// Create an action that points to the method.
Action methodToCall = new Action(SomeMethod);
.NET is smart enough to figure out what to do if you leave out the object instantiation.

Code:
// Does the same thing as above.
Action methodToCall = SomeMethod;
Then you can just call the action delegate like you would a normal method.

Code:
methodToCall();
If you want to pass a parameter it would look like this:

Code:
void SomeOtherMethod(string param1, int param2) { }

Action<string, int> otherMethod = SomeOtherMethod;

otherMethod("Hello", 1234);
If you want the method to return a value, you can use the builtin Func delegate type.

Code:
bool MethodWithReturn(string param1) { return true; }

// Use a func if there is a return value.
Func<string, bool> returnMethod = MethodWithReturn;

bool result = returnMethod("Hello");
There's a lot more to delegates, but that's enough to get you through most problems.
__________________
InSim.NET | InSimSniffer | pyinsim
Reply With Quote
Old 6th May 2012, 16:14   #3  -   
PoVo
S2 licensed
 
PoVo's Avatar
 
Racername: host
Join Date: Jun 2008
Location: Dublin, Ireland
Posts: 1,817
PoVo's online stats
Great! Thanks for your help.

The examples I was reading at Microsoft's help site weren't useful at all and made no sense!

Thanks for clearing it up
Reply With Quote
Old 27th May 2012, 09:41   #4  -   
xspeedasx
S2 licensed
 
Join Date: Aug 2011
Posts: 2
xspeedasx's online stats
C# is based on events, so, basically using events is the fastest way of passing objects between classes and methods.

By saying "callbacks" i suppose you had "events" in mind tho. Easy to mix them up, if you had experience in programming on C (or derivatives of C, like PAWN).

So, for example your button class should have something like:
Code:
class SomeClass
{
    public event EventHandler clicked;
    ...
    if(Conditions_for_callback_are_met)
    {
        clicked(this, EventArgs.Empty);
    }
    ...
}
then on some other class where you're creating the object, you can simply:
Code:
SomeClass someObject = new SomeClass();
someObject.clicked += new EventHandler(someEvent_Fired);
followed by:
Code:
private void someEvent_Fired(object sender, EventArgs e)
{
    // add type cast to get the object, that fired the event;
    SomeClass objectPassed = (SomeClass)sender; 
    ...
}
and handle the event.

Hope that helped.

Disclaimer:
I learned this programming C# (not InSim related, since im demo user).
Reply With Quote
Old 27th May 2012, 15:35   #5  -   
PoVo
S2 licensed
 
PoVo's Avatar
 
Racername: host
Join Date: Jun 2008
Location: Dublin, Ireland
Posts: 1,817
PoVo's online stats
Nice. Would there be any real good advantages to switch from actions to event handlers?
__________________
Free 15GB cloud storage + 5GB free for you and me if you use this referral link
Reply With Quote
Old 27th May 2012, 17:20   #6  -   
xspeedasx
S2 licensed
 
Join Date: Aug 2011
Posts: 2
xspeedasx's online stats
Quote:
Originally Posted by PoVo View Post
Nice. Would there be any real good advantages to switch from actions to event handlers?
I believe, that you will find your answer here:
http://stackoverflow.com/questions/1...t-eventhandler
Reply With Quote
Reply

  Live for Speed > Main > LFS Programmer Forum

  • Submit Thread to Digg
  • Submit Thread to del.icio.us
  • Submit Thread to StumbleUpon
  • Submit Thread to Google

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:15.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Live for Speed © 2002-2014 - Scawen Roberts, Eric Bailey, Victor van Vlaardingen