Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

Moving from Event Handlers in vb.net to c#.net

Okay, I've finally given up on trying to do this. I only recently learnt to do it in vb.net and found it really useful. Translating that to C# is proving to be an absolute nightmare.

What I had in vb.net was this. A class that inherited and extended the data table class called xDataTable.

One of the main features of this extension is that the base data table will populate itself from a SQL server database if a populated SQLCommand is given to it.

I then added an event to the xDataTable which fired whenever the base data table was populated. Trying to do this in C# is proofing to be my worst nightmare. Can anyone help please.

Summary:

Class xDataTable will contain an event called hasData

Form fEditor should then add a handler to use this event.

Please could someone help and stop me from going back to vb

Thanks.
0
paul_whiting
Asked:
paul_whiting
  • 2
  • 2
1 Solution
 
DarrenDCommented:
Hi,

If you have a class xDataTable you should create a delegate like so

// Create Event

public delegate void HandleData();

public class xDataTable {

  public event HandleData hasData

  public void Fire() {
    hasData();
  }
}

//Register Event in Form
class registerEvent {

  xDataTable xdata = new xDataTable();

  ImplementationEvent myImpl = new ImplementationEvent();

  xdata.hasData += new HandleData(myImpl.DoSomething);
}

//Register Event in Form
class ImplementationEvent {

  public void DoSomething() {

   console.WriteLine("EventFired");

  }
}

Hope this helps,

Darren
0
 
paul_whitingAuthor Commented:
Thanks very much for your reply. It took a while but I got it to work with my scenario. C# is giving me nightmares, but I shall persevere.

Would there be much difference if I passed values with the events?

Thanks

Paul
0
 
DarrenDCommented:
Hi Paul,

Your delegate will have to change to something like

public delegate void HandleData(string value);

You will have to call the event using a parameter

hasData("Hello");

and your implementation will have to change to

public void DoSomething(string value) {

   console.WriteLine(value);

  }


Think that's it.

Obviously haven't tested it.

Cheers,

Darren
0
 
paul_whitingAuthor Commented:
Thanks,

haven't had a chance to test it, but given what I now know via your previous solution, looks like it will  work. Thank-you so much. You have saved my sanity.

Paul
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now